How to add text to a textArea instead of replacing it

前端 未结 3 564
我寻月下人不归
我寻月下人不归 2021-02-07 16:46

How can I add text to a JTextArea instead of replacing all of it?

I know about setText(String) but other than that I\'m a bit lost.

相关标签:
3条回答
  • 2021-02-07 16:59

    To insert string at any position you can use the component's Document.

    public static void main(String[] args) throws BadLocationException {
        JTextField f = new JTextField("foo bar");
        int offset = 7;
        String str = " baz";
        f.getDocument().insertString(offset, str, SimpleAttributeSet.EMPTY);
        System.out.println(f.getText());
    }
    
    0 讨论(0)
  • 2021-02-07 17:14
    void append(JTextArea area, String newText){
            area.setText(area.getText() + newText)
    }
    
    0 讨论(0)
  • 2021-02-07 17:17

    You can use the append method like this:

    textArea.append(additionalText);
    
    0 讨论(0)
提交回复
热议问题