【发布时间】:2012-10-19 17:59:24
【问题描述】:
我将SwingX AutoCompleteDecorator 用于JComboBox。自动完成功能效果很好......
但我无法确定最终用户选择的时刻;很少保留我的数据。
让我试着解释一下:组合框会为每个选择触发一个“comboBoxChanged”-ActionEvent。当用户输入字符并且组合框自动匹配和选择项目时,我必须忽略这些事件。如果用户点击返回键,则会生成“comboBoxEdited”-ActionEvent,我可以保存所选值。太棒了;-)
如果使用鼠标打开JComboBox-PopUp 并选择一个项目,则唯一触发的事件是“comboBoxChanged”-ActionEvent(如自动匹配或使用光标键选择项目时)。鼠标点击事件以某种方式被消耗!?这就是我无法识别 final 鼠标选择的原因。
我该如何解决这个问题?我尝试侦听 mouseClicked-Event 的失败记录在此 SSCCE 中:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
public class SearchForThePopUpMouseClick extends JPanel
{
private JComboBox<String> comboBox;
public SearchForThePopUpMouseClick()
{
comboBox = new JComboBox<String>(new String[] { "Anna", "Marc", "Maria", "Marten", "Peter" });
add(comboBox);
add(new JTextField("textfield to click"));
AutoCompleteDecorator.decorate(comboBox);
comboBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Action Event with '" + e.getActionCommand() + " " + e.getID() + "'");
};
});
((Component) comboBox.getUI().getAccessibleChild(comboBox, 0)).addMouseListener(new MouseListener()
{
@Override
public void mouseReleased(MouseEvent e)
{
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e)
{
System.out.println(e);
}
@Override
public void mouseExited(MouseEvent e)
{
System.out.println(e);
}
@Override
public void mouseEntered(MouseEvent e)
{
System.out.println(e);
}
@Override
public void mouseClicked(MouseEvent e)
{
System.out.println(e);
}
});
}
public static void main(String[] args) throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
SearchForThePopUpMouseClick autoCompletePanel = new SearchForThePopUpMouseClick();
JFrame frame = new JFrame("SwingX Autocomplete Example");
frame.add(autoCompletePanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
【问题讨论】:
-
addItemListener(ItemListener)时会被解雇什么? -
@Muel - 相同的 DESELECTED- 和 SELECTED-ItemEvents 用于键盘光标导航和鼠标点击:-(
-
我已经很长时间没有做过一些 Swing 并且我从未使用过 SwingX。我猜听一个焦点丢失事件是不可接受的?或者,您可能很幸运拥有
PropertyChangeListener或VetoableChangeListener,但我在黑暗中刺伤!也许下载 SwingX 的源代码,看看他们在做什么......:P -
共有三种方式列出selectionlistener、swingutilities、AWTListener,我这个案例是Actionlistener更好的监听器,对于example for all (mouse & keyboard from derived jlist) event from low_level AWT listeners
-
good catch :-) But not really different compared to a plain undecorated combo when selecting the items by keyboard vs. mouse: the action is always fired when the selection changes, no support to distinguish between "final ”和“中间”的变化。
标签: java swing autocomplete jcombobox swingx