我的代码中有JCombobox
。我添加了FocusLost event
。但无论如何它并没有被解雇。我已经尝试了很多时间,但没有找到解决方案。
jcbItemType.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jcbItemTypeFocusLost(evt);
}
});
private void jcbItemTypeFocusLost(java.awt.event.FocusEvent evt)
{
// TODO add your handling code here:
System.out.println("name=" + ((Component) evt.getSource()).getName());
System.out.println("index=" + jcbItemType.getSelectedIndex());
}
但是在控制台中没有打印任何内容。请告诉我我做错了什么。
答案 0 :(得分:7)
我找到了一种解决这个问题的简单方法。
JComboBox默认编辑器有一个内部类BasicComboBoxEditor $ BorderlessTextField,它是获取和失去焦点的组件。
只需
即可访问Component component = comboBox.getEditor().getEditorComponent();
if (component instanceof JTextField)
JTextField borderlesstextfield = (JTextField) borderless;
然后像任何JTextField一样添加焦点侦听器
borderlesstextfield.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
}
}});
现在你有了一个FocusListener,它将按预期响应ComboBox的获取和失去焦点
答案 1 :(得分:6)
FocusListener不适合JComboBox的监听器,与另一个Listener(s)一起可以创建无限循环(尤其是可编辑的JComboBox),
FocusListener是异步的,有时难以捕获事件是正确的命令,尤其是在JComponents添加了另一个Listener(s)的情况下
示例如何从派生JTextField / JFormattedTextField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTwo extends JFrame implements ItemListener {
private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
public ComboBoxTwo() {
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
mainComboBox = new JComboBox(items);
mainComboBox.addItemListener(this);
mainComboBox.addFocusListener(fcsListener);
add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox(subItems1);
subComboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXX");
subComboBox.addItemListener(this);
add(subComboBox, BorderLayout.EAST);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == mainComboBox) {
System.out.println("Source : mainComboBox");
} else if (e.getSource() == subComboBox) {
System.out.println("Source : subComboBox");
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
//
private FocusListener fcsListener = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
dumpInfo(e);
}
@Override
public void focusLost(FocusEvent e) {
dumpInfo(e);
}
private void dumpInfo(FocusEvent e) {
System.out.println("Source : " + name(e.getComponent()));
System.out.println("Opposite : " + name(e.getOppositeComponent()));
System.out.println("Temporary: " + e.isTemporary());
final Component c = e.getComponent();//works for editable JComboBox too
if (c instanceof JFormattedTextField) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JFormattedTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JTextField) c).selectAll();
}
});
}
}
private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
}