Selecting an Item From a JComboBox…Again

The default behavior of the JComboBox is sufficient enough in most cases. If you select an item from the pull down list, an action event is generated and sent to any listeners that have interest in that newly selected item. However, suppose you were interested in knowing whether the same entry was chosen again–perhaps to establish settings for other fields on the screen, or even to detect a possible entry error. The JComboBox specifically prevents a new action from being invoked if the currently selected item is selected again from the pull down list. This extension of JComboBox allows new actions to be invoked, even if the same item is selected again:

 import java.util.*;import*;import*;import;public class AlwaysSelectableComboBox extends JComboBox implementsSerializable{    private boolean _alwaysFireOnSelect = true;    public AlwaysSelectableComboBox(ComboBoxModel aModel)  {super(aModel); }    public AlwaysSelectableComboBox(final Object items[])  {super(items); }    public AlwaysSelectableComboBox(Vector items)          {super(items); }    public AlwaysSelectableComboBox()                      { super(); }    public void setAlwaysFireOnSelect(boolean alwaysFireOnSelect)    {        _alwaysFireOnSelect = alwaysFireOnSelect;    }    public void contentsChanged(ListDataEvent e)    {        if (_alwaysFireOnSelect)        {            selectedItemReminder = null;        }        super.contentsChanged(e);    }}

The contentsChanged() method normally checks the selectedItemReminder field that holds the previously selected item. Here, we replace the selectedItemReminder with null to fake out the JComboBox into thinking that nothing had been previously selected. You may call setAlwaysFireOnSelect(false) to revert your new JComboBox extension back to the default behavior.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS