How to work with selected text in JTextArea ?

Example: user selected text in JTextArea pressed a button and something happened with this selected text, for example, it was replaced with "TEST"

    1 answer 1

    For this, the replaceSelection() method is replaceSelection() :

     JButton butt = new JButton("Replace"); JTextArea area = new JTextArea(); butt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { area.replaceSelection("TEST"); } }); 

    Result of work:

    enter image description hereenter image description here

    • How long ago it was, but thanks, I didn’t wait for an answer. - Denis Kotlyarov