Question

Is it possible to request JWindow temporary focus? There is a method protected boolean requestFocusInWindow(boolean temporary), but the method is protected.

Was it helpful?

Solution

Already found the solution - in class which extends JWindow created this method:

@Override
public boolean requestFocusInWindow(boolean temporary) {
    return super.requestFocusInWindow(temporary);
}

Now I can request temporary focus for component which extends JWindow. I'm using it for my custom popup menu for JTextField where caret color is changed if selected when popup is visible (of course - textField.setCaret(new HighlightCaret()); is necessary):

private class HighlightCaret extends DefaultCaret {

    private final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
    private final Highlighter.HighlightPainter focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(10, 36, 106));
    private boolean isTemporary;

    private HighlightCaret() {
        setBlinkRate(500);
    }

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isTemporary ? unfocusedPainter : focusedPainter;
    }

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

    @Override
    public void focusGained(FocusEvent e) {
        isTemporary = false;
        super.focusGained(e);
    }

    @Override
    public void focusLost(FocusEvent e) {
        isTemporary = e.isTemporary();
        super.focusLost(e);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top