[英]Opening JXDatePicker on gaining focus
I am trying to extend JXDatePicker so that it opens up when it gains focus. 我正在尝试扩展JXDatePicker,以便在获得焦点时打开它。 Have searched for suggest that I understand without success.
已搜索过我没有成功理解的建议。 Is there an elegant way of doing this?
这样做有一种优雅的方式吗?
Astonishingly, it's not really possible :-( 令人惊讶的是,它真的不可能:-(
For once, the JXDatePicker itself has no api to show/hide the popup (only BasicDatePickerUI has). 一次,JXDatePicker本身没有api来显示/隐藏弹出窗口(只有BasicDatePickerUI有)。 Plus the ui delegate has some internal magic (read: hacks ... cough) that makes a FocusListener even worse to handle than usually in compound components.
另外,ui委托有一些内部魔法(阅读:黑客......咳嗽),这使得FocusListener处理比通常在复合组件中更糟糕。
A snippet to play with: 一个可以玩的片段:
final JXDatePicker picker = new JXDatePicker();
FocusListener l = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
// no api on the picker, need to use the ui delegate
BasicDatePickerUI pickerUI = (BasicDatePickerUI) picker.getUI();
if (!pickerUI.isPopupVisible()) {
pickerUI.toggleShowPopup();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// opening the popup moves the focus to ... ?
// need to grab it back onto the editor
picker.getEditor().requestFocusInWindow();
}
});
}
@Override
public void focusLost(FocusEvent e) {
}
};
// need to register the listener on the editor
picker.getEditor().addFocusListener(l);
JComponent content = new JPanel();
content.add(new JButton("dummy"));
content.add(picker);
Not really satisfying, as automatic closing of the popup on transfering the focus out again doesn't work reliably, needs two tabs (don't know why) 不太令人满意,因为再次将焦点转移出来时自动关闭弹出窗口并不可靠,需要两个标签(不知道为什么)
I had the same problem. 我有同样的问题。 This worked for me:
这对我有用:
jXDatePicker.getEditor().addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
BasicDatePickerUI pickerUI = (BasicDatePickerUI) jXDatePicker.getUI();
if (!pickerUI.isPopupVisible() && e.getOppositeComponent() != getRootPane() && e.getOppositeComponent() != jXDatePicker.getMonthView()) {
pickerUI.toggleShowPopup();
}
}
@Override
public void focusLost(FocusEvent e) {}
});
This piece of code is used to avoid focus issues: 这段代码用于避免焦点问题:
e.getOppositeComponent() != getRootPane()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.