如何突出显示JTextPane中的所有文本? [英] How to highlight all the text in a JTextPane?

查看:111
本文介绍了如何突出显示JTextPane中的所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jTextPane1.selectAll();

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

With the correctly shared events, that command permit to highlight the text in a JTextPane area (I am a bit rusty, I need to not forget to share the "good event focus priorities" ; thank you to : MadProgrammer)

推荐答案

由于

Since selectAll is a method of JTextComponent, which JTextPane extends from I would take a wild guess and say, probably, yes.

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

Five minutes of coding probably would have gotten you the same answer yourself...

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

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

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

This is likely because the JTextPane doesn't have focus, try using requestFocusInWindow to bring keyboard focus back to the JTextPane.

JTextComponent在没有焦点时并不会始终显示选择突出显示.

The JTextComponents don't always render selection highlighting when they don't have focus.

例如...

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

例如...

现在双击

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

这篇关于如何突出显示JTextPane中的所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆