0
jTextPane1.selectAll();

With the correctly shared events, that command permit to highlight the text in a JTextPane area (I am a bit rusty, I need to not forget to share the "good event focus priorities" ; thank you to : MadProgrammer)

RubioRic
  • 2,442
  • 4
  • 28
  • 35
MadMonkey
  • 125
  • 1
  • 1
  • 7
  • What happened when you tried it? `Highlighting not seem to appear in the jTextPane area` - What happened? Are you aware that by default a double click will highlight the current word? Post your [SSCCE](http://www.sscce.org/) that demonstrates the problem. – camickr Mar 25 '14 at 03:26

1 Answers1

4

Since selectAll is a method of JTextComponent, which JTextPane extends from I would take a wild guess and say, probably, yes.

Five minutes of coding probably would have gotten you the same answer yourself...

Highlighting not seem to appear in the jTextPane area (note : I use Java 7)

This is likely because the JTextPane doesn't have focus, try using requestFocusInWindow to bring keyboard focus back to the JTextPane.

The JTextComponents don't always render selection highlighting when they don't have focus.

For example...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextPane {

    public static void main(String[] args) {
        new TestTextPane();
    }

    public TestTextPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JTextPane tp = new JTextPane();
                JButton withFocus = new JButton("Select with focus");
                withFocus.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tp.selectAll();
                        tp.requestFocus();
                    }
                });
                JButton withOutFocus = new JButton("Select without focus");
                withFocus.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tp.selectAll();
                    }
                });


                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(tp));
                JPanel panel = new JPanel();
                panel.add(withFocus);
                panel.add(withOutFocus);
                frame.add(panel, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }        
}

You could also test it by using

textPane.selectAll();
System.out.println(textPane.getSelectedText());

For example...

And now with double clicking

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextPane {

    public static void main(String[] args) {
        new TestTextPane();
    }

    public TestTextPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JTextPane tp = new JTextPane();
                JButton withFocus = new JButton("Select with focus");
                tp.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                            tp.selectAll();
                        }
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(tp));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • **Yes it does** - when the component has focus, the selection will appear, when it doesn't it won't – MadProgrammer Mar 25 '14 at 03:31
  • `I have tried to get the focus by different ways` - Why are you trying to get focus? Your question states you looked for solutions using a mouse double click which means the text pane does have foccus. Post a proper SSCCE so we don't waste time guessing what you are doing! – camickr Mar 25 '14 at 03:39
  • You may want to take a look at [this](http://stackoverflow.com/questions/18243101/how-to-override-defaultcaretsetblinkrate), [this](http://stackoverflow.com/questions/18237317/how-to-retain-selected-text-in-jtextfield-when-focus-lost) and [this](http://stackoverflow.com/questions/18243101/how-to-override-defaultcaretsetblinkrate) which all discuss the problem... – MadProgrammer Mar 25 '14 at 03:39