2

I want to find out which part of JTextPanel text is selected. Tried to call JTextPane.getSelectionStart() and JTextPane.getSelectionEnd(), but they always return same value that is equal to current caret position. What is my problem with that?

I would be thankful for any code exapmle that gets current selection.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ilya Ivanov
  • 2,379
  • 1
  • 21
  • 24
  • 2
    Document, Caret and viewToModel, but for better help soonet post an [SSCCE](http://sscce.org/) – mKorbel Sep 19 '12 at 10:27
  • 1
    [I would be thankful for any code exapmle that gets current selection.](http://www.java2s.com/Tutorial/Java/0240__Swing/0240__JTextComponent.htm) – mKorbel Sep 19 '12 at 10:30

3 Answers3

8

Have a look at JTextComponent#getSelectedText(). You'd simply call this method on the instance of your JTextPaneand it will return the selected text of your JTextPane. Did a small example:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class JavaApplication101 {

    private JTextPane jTextPane;
    private JButton btnGetSelectedText;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JavaApplication101().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(Container contentPane) {
        jTextPane = new JTextPane();
        btnGetSelectedText = new JButton("Get selected text");

        btnGetSelectedText.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, jTextPane.getSelectedText());
            }
        });
        contentPane.add(jTextPane, BorderLayout.NORTH);
        contentPane.add(btnGetSelectedText, BorderLayout.SOUTH);
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
1
public class TextPaneHighlightsDemo extends JFrame {

public TextPaneHighlightsDemo() {
    super("SplashScreen demo");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final JTextPane textPane = new  JTextPane();
    add(textPane);
    textPane.addCaretListener(new CaretListener() {

        @Override
        public void caretUpdate(CaretEvent e) {
            Highlight[] h = textPane.getHighlighter().getHighlights();
            for(int i = 0; i < h.length; i++) {
                System.out.println(h[i].getStartOffset());
                System.out.println(h[i].getEndOffset());
            }

        }
    });
        }

public static void main (String args[]) {
    TextPaneHighlightsDemo test = new TextPaneHighlightsDemo();
    test.setVisible(true);
}
}
KrHubert
  • 1,030
  • 7
  • 17
0

I found my problem - that was a custom FocusListener which was changing JTextPane content (and so dropping the selection) before I got the keyTyped event.

Anyway, thanks everyone for examples and comments!

Ilya Ivanov
  • 2,379
  • 1
  • 21
  • 24