如何在焦点丢失时保留JTextField中的选定文本?

时间:2013-08-14 16:32:01

标签: java swing selection jtextfield caret

现在完成我的自定义菜单弹出,但问题是,如果我在JTextField中选择一些文本并单击鼠标按钮以显示弹出菜单,则焦点将转移到弹出窗口,之前不再选择所选文本突出显示。当焦点回到JTextField时 - 所选文本将再次突出显示。如何在焦点丢失时突出显示所选文本?

1 个答案:

答案 0 :(得分:10)

  

然后将焦点转移到弹出窗口,然后选择文本   不再突出显示。当焦点回到JTextField时 - 选中   文字再次突出显示。如何使所选文本保持不变   焦点丢失突出显示?

DefaultCaret的代码示例

class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
    private static final Highlighter.HighlightPainter focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isFocused ? focusedPainter/*super.getSelectionPainter()*/ : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}

带输出

enter image description here

来自代码

import java.awt.*;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class TestTextComponents {

    private static final long serialVersionUID = 1L;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JFrame frame = new JFrame("Default Caret");

    public TestTextComponents() {
        jTextField1 = new JTextField();
        jTextField2 = new JTextField();
        jTextField1.setText("jTextField1");
        jTextField2.setText("jTextField2");
        jTextField1.setCaret(new HighlightCaret());
        jTextField2.setCaret(new HighlightCaret());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(new FlowLayout());
        frame.add(new JLabel("Please skip between text fields and watch persistent selection: "));
        frame.add(jTextField1);
        frame.add(jTextField2);
        frame.setTitle("Text component persistent selection");
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestTextComponents();
            }
        });
    }
}

class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
    private static final Highlighter.HighlightPainter focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        setBlinkRate(500); // otherwise is disabled, stopped
        return isFocused ? focusedPainter/*super.getSelectionPainter()*/ : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}

EDIT必须恢复Caret.setBlinkRate(500);