1

I am trying to select some text programmatically in a JTextPane but it isn't working. I found out the problem but I don't know how to fix it. It works fine if there is no JTextFeild in the JFrame but if I add it, the focus goes to the JTextFeild and the selection unselects.

Here is an SSCCE

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;


@SuppressWarnings("serial")
public class SSCCE extends JFrame {

    JTextPane pane;
    JTextField feild;

    public SSCCE() {
        setSize(300, 200);
        feild = new JTextField("This is a text feild");
        // Run the program then uncomment the next line and run the program again.
        // add(feild, BorderLayout.NORTH);
        pane = new JTextPane();
        pane.setText("This is some text. I am making an SSCCE. This is some additional text.");
        pane.select(2, 30);
        add(pane);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new SSCCE();
    }
}
usama8800
  • 893
  • 3
  • 10
  • 20

2 Answers2

3

The selection works. However, only the text component that currently has focus will display the selection.

All you need to do is hit the Tab key and focus will go to the text pane and you will see the selection.

Or you can add the following after the setVisible(true) statement.

    pane.requestFocusInWindow();

Make sure you create the GUI on the EDT:

EventQueue.invokeLater(new Runnable()
{
    public void run()
    {
         new SSCCE3();
    }
));
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Adding that line isn't working but the tab is. How do I move focus to the pane? – usama8800 May 17 '14 at 15:15
  • It works fine for me using JDK7 on Windows 7. Post your update SSCCE showing where you added the requestFocusInWindow() method. – camickr May 17 '14 at 15:17
  • I tried it a few more times. Sometimes it work but sometimes it doesn't. – usama8800 May 17 '14 at 15:18
  • you MUST add it after setVisible(true) than it works – pL4Gu33 May 17 '14 at 15:19
  • @usama8800, Yes, I notice that as well. One of the rules of Swing is that all updates to the GUI should be done on the Event Dispatch Thread. So make sure your GUI is created on the EDT. See the update: – camickr May 17 '14 at 15:22
  • @usama8800: More on this critical point [here](http://stackoverflow.com/a/19167194/230513). – trashgod May 17 '14 at 15:24
1

Another Solution instead of requestFocusInWindow is a Highlighter like this:

public class SSCCE extends JFrame {

JTextPane pane;
JScrollPane scrollPane;
JTextField feild;

public SSCCE() throws BadLocationException {
    setSize(300, 200);
    feild = new JTextField("This is a text feild");
    // Run the program then uncomment the next line and run the program
    // again.
    add(feild, BorderLayout.NORTH);
    pane = new JTextPane();
    pane.setFocusable(true);
    pane.setText("This is some text. I am making an SSCCE. This is some additional text.");
    pane.getHighlighter().addHighlight(2, 30,
            new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY));
    scrollPane = new JScrollPane(pane);
    add(scrollPane, BorderLayout.CENTER);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                new SSCCE();
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
}
pL4Gu33
  • 2,045
  • 16
  • 38