How to retain selected text in JTextField when focus lost?
Asked Answered
B

1

9

Now finishing my custom menu popup, but the problem is that if I select some text in JTextField and click mouse button to show popup menu, then focus is transferred to popup window, AND selected text before are no longer highlighted. When focus is back to JTextField - selected text become highlighted again. How to make the selected text stay highlighted on focus lost?

Bridle answered 14/8, 2013 at 16:32 Comment(2)
please see my question for more details, better isn't itCaboodle
related: #5902333Unipolar
C
11

then focus is transferred to popup window, AND selected text before are no longer highlighted. When focus is back to JTextField - selected text become highlighted again. How to make the selected text stay highlighted on focus lost?

code example from 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);
        }
    }
}

with output

enter image description here

from code

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 have to restore Caret.setBlinkRate(500);

Caboodle answered 14/8, 2013 at 16:39 Comment(14)
Wow, that simple, I'm impressed :) Thanks again indeed @Caboodle ! Problem solved in a few seconds :)Bridle
@Ernestas Gruodis glad if help youCaboodle
After I set textField.setCaret(new HighlightCaret()); I noticed that caret stops blinking.. Maybe there is some setting in Caret class? Now trying to dig deeper in DefaultCaret class.Bridle
Yes, like I expected - the public HighlightCaret() { setBlinkRate(500); } did the trick. Now caret blinks again :)Bridle
test whats returns getBlinkRate(), but AFAIK this could be another reason to use JTextPane, there is cursor visibleCaboodle
The caret was present, but didn't blink.Bridle
but hmmm now I see that Caret blinks on 3rd focusGained, no idea how to fix thatCaboodle
In the code above setBlinkRate(500); is placed in ` protected Highlighter.HighlightPainter getSelectionPainter()` method. I have place it in HighlightCaret() { setBlinkRate(500); }, so it is set only once. Not really understand what 3rd focusGained means..Bridle
got it, use most complete (you can to compare) DefaultCaret by @kleopatraCaboodle
Found not much difference, except in public void setSelectionVisible(boolean vis) is super.setSelectionVisible(!isSelectionVisible());. Replaced it in my code, works the same, no problems.Bridle
well agree with you, but still I'm not satisfied, I made there important changes and change L&F to Nimbus, then caret blinkind on 3rd. focusGainedCaboodle
I always trying to create my own L&F - like this popup menu from scratch - so always independent from L&F - less problems.Bridle
It works almost... however, when I tab out of a component, it sometimes retains its yellow color. This is with default L&F (Metal). tinybrain.de/1008677 Same result with Substance L&F. tinybrain.de/1008678Trafalgar
Also I would love a solution where the selection is visible even when the text field has never been in focus (I preselect text through the program).Trafalgar

© 2022 - 2024 — McMap. All rights reserved.