简体   繁体   English

如何从JTextPane获取选择

[英]How to get selection from JTextPane

I want to find out which part of JTextPanel text is selected. 我想找出选择了JTextPanel文本的哪一部分。 Tried to call JTextPane.getSelectionStart() and JTextPane.getSelectionEnd() , but they always return same value that is equal to current caret position. 试图调用JTextPane.getSelectionStart()JTextPane.getSelectionEnd() ,但是它们总是返回与当前插入符位置相同的值。 What is my problem with that? 我的问题是什么?

I would be thankful for any code exapmle that gets current selection. 我将很感谢获得当前选择的任何代码示例。

Have a look at JTextComponent#getSelectedText() . 看看JTextComponent#getSelectedText() You'd simply call this method on the instance of your JTextPane and it will return the selected text of your JTextPane . 你只需拨打您的实例这种方法JTextPane ,它会回报你的所选文本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);
    }
}
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);
}
}

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. 我发现了我的问题-那是一个自定义FocusListener ,它在得到keyTyped事件之前更改了JTextPane的内容(因此删除了选择)。

Anyway, thanks everyone for examples and comments! 无论如何,感谢大家的例子和评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM