Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

  • I have an problem with Caret, Caret didn't blink without focusGained(see code in Swing Action) to 2nd. JTextField and back to 1st. JTextField

  • how to override DefaultCaret#setBlinkRate() correctly

  • (without override Caret) by default is Caret at the end of Document and blinking on 1st. focusGained



my SSCCE

import java.awt.*;
import java.awt.event.ActionEvent;
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 Timer timer;
    private JTextField jTextField0 = new JTextField();
    private JTextField jTextField1 = new JTextField();
    private JTextField jTextField2 = new JTextField();
    private JFrame frame = new JFrame("Default Caret");
    private JPanel panel = new JPanel();

    public TestTextComponents() {
        jTextField0.setText("jTextField0");
        jTextField1.setText("jTextField1");
        jTextField2.setText("jTextField2");
        jTextField1.setCaret(new HighlightCaret());
        jTextField2.setCaret(new HighlightCaret());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        panel.add(new JLabel("Please skip between text fields and watch persistent selection: "));
        panel.add(jTextField0);
        panel.add(jTextField1);
        panel.add(jTextField2);
        frame.add(panel);
        frame.setTitle("Text component persistent selection");
        frame.pack();
        frame.setVisible(true);
        /*timer = new javax.swing.Timer(250, updateCol());
        timer.setRepeats(false);
        timer.start();*/
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField2.grabFocus();
                jTextField2.requestFocusInWindow();
                jTextField1.grabFocus();
                jTextField1.requestFocusInWindow();
            }
        };
    }

    private class HighlightCaret extends DefaultCaret {

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

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

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

    public static void main(String args[]) {
        /*try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestTextComponents();
            }
        });
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
100 views
Welcome To Ask or Share your Answers For Others

1 Answer

I have found the solution. Override the focusGained method of your HighlightCaret and set the blink rate there as well.

    @Override
    public void focusGained(FocusEvent e)
    {
        isFocused = true;
        super.setBlinkRate(500);
        super.focusGained(e);
    }

This did the trick for me in OS X.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...