Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selection from JTextPane

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.

like image 607
Ilya Ivanov Avatar asked Jan 30 '26 07:01

Ilya Ivanov


2 Answers

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);
    }
}
like image 194
David Kroukamp Avatar answered Feb 01 '26 20:02

David Kroukamp


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);
}
}
like image 35
KrHubert Avatar answered Feb 01 '26 21:02

KrHubert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!