I'm using the SwingX AutoCompleteDecorator
for a JComboBox
. The autocomplete feature works beautifully...
But I have trouble to identify the moment of the final user selection; to persist my data seldom.
Let me try to explain: The combobox fires an "comboBoxChanged"-ActionEvent
for every selection. I have to ignore these events while the user is typing characters and the combobox is auto-matching and selecting items. If the user hits the return-key an "comboBoxEdited"-ActionEvent
is generated and I can save the selected value. Great ;-)
If the mouse is used to open the JComboBox
-PopUp and to select an item, the only fired event is a "comboBoxChanged"-ActionEvent
(like when auto-matching or selecting an item with the cursor-keys). The mouse-clicked-Event is consumed somehow!? That's why I can't identify the final mouse selection.
How can I figure this out? My failed attempts to listen for the mouseClicked-Event are documented in this 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)
? – AnastomosePropertyChangeListener
or aVetoableChangeListener
but I'm stabbing in the dark! Maybe download the source for SwingX and have a look at what they're doing... :P – Anastomose