如何请求JWindow临时关注?(Java 1.7)



是否可以请求JWindow临时焦点?有一个方法protected boolean requestFocusInWindow(boolean temporary),但该方法受到保护。

已经找到了解决方案-在扩展JWindow的类中创建了这个方法:

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

现在我可以为扩展JWindow的组件请求临时焦点。我将其用于JTextField的自定义弹出菜单,如果在弹出可见时选择插入符号颜色,则会更改(当然,textField.setCaret(new HighlightCaret());是必要的):

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

相关内容

  • 没有找到相关文章

最新更新