开发者

Single line JTextArea

开发者 https://www.devze.com 2023-04-12 09:44 出处:网络
When you type into a JTextArea, it automatically adjust it\'s size to fit the text typed in. A JTextField, however, does not seem to do that.

When you type into a JTextArea, it automatically adjust it's size to fit the text typed in.

A JTextField, however, does not seem to do that. Here's an SSCCE which demonstrates the problem:

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    frame.add(new JTextArea("I resize as you type."));
    frame.add(new JTextField("I don't."));
    frame.setSize(200,100);
    frame.setVisible(true);
}

Single line JTextArea

I was wondering if there was a way readily available to make a JTextField adjust it's width. I wish I can just use a J开发者_运维问答TextArea, but I can only accept one line of input.


but I can only accept one line of input

Single Line Text Area shows how you can do this. The relevant code is

textArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);
0

精彩评论

暂无评论...
验证码 换一张
取 消