简体   繁体   English

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

[英]How to highlight all the text in a JTextPane?

jTextPane1.selectAll();

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

Since selectAll is a method of JTextComponent , which JTextPane extends from I would take a wild guess and say, probably, yes. 由于selectAllJTextComponent的方法,因此JTextPaneJTextPane ,我会JTextPane猜测并说是的。

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) 突出显示似乎没有出现在jTextPane区域中(注意:我使用Java 7)

This is likely because the JTextPane doesn't have focus, try using requestFocusInWindow to bring keyboard focus back to the JTextPane . 这可能是因为JTextPane没有焦点,请尝试使用requestFocusInWindow将键盘焦点重新带回到JTextPane

The JTextComponent s don't always render selection highlighting when they don't have focus. JTextComponent没有焦点时,它们并不总是呈现选择突出显示。

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

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

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