quantum285
quantum285

Reputation: 1032

Selecting / highlighting text in a JTextArea belonging to a JOptionPane

My problem is the following:

In my application the user clicks a button which brings up a dialog box (a custom jOptionPane). This dialog contains a JTextArea in which the user will type a response, which will then be processed by the application, however I would like this JTextArea (which will hold the user's input and currently contains example text like "Write your answer here") to be automatically highlighted.

I can do this normally, by calling requestFocusInWindow() followed by selectAll() on the JTextArea however there seems to be a problem when this is done using a JOptionPane which I'm guessing is to do with the fact that the focus cannot shift to the JTextArea successfully.

I've made a SSCCE to demonstrate this clearly, and hopefully get an answer from one of you guys as to how I can make this possible. Thanks in advance!

Class 1/2 : Main

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main extends JFrame{


    public static void main(String[] args) {
        Main main = new Main();
        main.go();
    }

    private void go() {
        JPanel background = new JPanel();
        JPanel mainPanel = new ExtraPanel();
        ((ExtraPanel) mainPanel).setupPanel();
        JButton testButton = new JButton("Test the jOptionPane");
        testButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                optionPaneTest();
            }
        });
        background.add(mainPanel);
        background.add(testButton);
        getContentPane().add(background);
        pack();
        setVisible(true);

    }

    private void optionPaneTest() {
        JPanel testPanel = new ExtraPanel();
        ((ExtraPanel) testPanel).setupPanel();
        int result = JOptionPane.showConfirmDialog(null, testPanel,
                "This is a test", JOptionPane.OK_CANCEL_OPTION);
    }
}

-----------------------------------------------------------------------------
Class 2/2 : ExtraPanel

import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ExtraPanel extends JPanel{

    public void setupPanel() {
        JTextArea textArea = new JTextArea();
        textArea.setText("Write your response here");
        textArea.requestFocusInWindow();
        textArea.selectAll();
        add(textArea);
    }

}

Upvotes: 0

Views: 1117

Answers (2)

Kevin Condon
Kevin Condon

Reputation: 1728

If you want focus in the TextArea so that the user can immediately start typing, you can trigger the selection using the ancestor added event.

public void setupPanel() {
    final JTextArea textArea = new JTextArea();
    textArea.setText("Write your response here");
    textArea.addAncestorListener(new AncestorListener() {
        public void ancestorRemoved(AncestorEvent event) { }
        public void ancestorMoved(AncestorEvent event) { }
        public void ancestorAdded(AncestorEvent event) {
            if (event.getSource() == textArea) {
                textArea.selectAll();
                textArea.requestFocusInWindow();
            }
        }
    });
    add(textArea);
}

Upvotes: 1

StanislavL
StanislavL

Reputation: 57421

Just add

textArea.getCaret().setSelectionVisible(true)

After textArea.selectAll();

Upvotes: 2

Related Questions