java - 如何突出显示 JTextPane 中的所有文本?

标签 java swing text highlight jtextpane

jTextPane1.selectAll();

通过正确共享的事件,该命令允许突出显示 JTextPane 区域中的文本(我有点生疏,我需要不要忘记共享“好的事件焦点优先级”;谢谢你:MadProgrammer)

最佳答案

selectAllJTextComponent 的一种方法,JTextPane 是从它扩展而来的。我大胆猜测一下,可能是的。

五分钟的编码可能会给你带来同样的答案......

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

这可能是因为 JTextPane 没有焦点,请尝试使用 requestFocusInWindow 将键盘焦点带回 JTextPane

JTextComponent 在没有焦点时并不总是突出显示选择内容。

例如...

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);
            }
        });
    }        
}

您还可以使用以下方法进行测试

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

例如...

现在双击

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);
            }
        });
    }
}

关于java - 如何突出显示 JTextPane 中的所有文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624869/

相关文章:

java - Akka 中重启 Actor 的消息持久化和回放

java - double 有理数的最快排序算法

Java UI 程序随机卡住

java - 如何使用自定义 JButton 在 Java (Swing) 中创建 JOptionPane.showOptionDialog 框?

r - R的ASCII绘图功能

vim - 如何在包含回车的插入模式下映射键?

java - 使用 Java 从 PDF 文件中提取大纲(或书签)

java - 读取与精确日期模式 YYYY-MM-DD 匹配的输入

Java:防止覆盖成员变量

java - 键绑定(bind)和按住键