【问题标题】:How to highlight all the text in a JTextPane?如何突出显示 JTextPane 中的所有文本?
【发布时间】:2014-05-02 17:20:40
【问题描述】:
jTextPane1.selectAll();

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

【问题讨论】:

  • 你尝试的时候发生了什么? Highlighting not seem to appear in the jTextPane area - 发生了什么事?您是否知道默认情况下双击会突出显示当前单词?发布您的 SSCCE 来证明问题。

标签: java swing text highlight jtextpane


【解决方案1】:

由于selectAll 是JTextComponent 的一个方法,JTextPane 是从它扩展而来的,所以我会大胆猜测,可能是这样。

五分钟的编码可能会让您自己得到相同的答案...

jTextPane 区域似乎没有突出显示(注意:我使用 Java 7)

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

JTextComponents 在没有焦点时并不总是呈现选择突出显示。

例如...

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

【讨论】:

  • 是的 - 当组件有焦点时,选择会出现,如果没有,则不会
  • I have tried to get the focus by different ways - 你为什么要集中注意力?您的问题表明您使用鼠标双击寻找解决方案,这意味着文本窗格确实有焦点。发布适当的 SSCCE,这样我们就不会浪费时间猜测您在做什么!
  • 你可能想看看this、this和this,它们都在讨论这个问题......
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
  • 2015-06-10
  • 2011-11-10
  • 1970-01-01
相关资源
最近更新 更多