Nulls In JComboBox Arrow Key Stop

There is an error in the code below. After loading the JFrame, click the tab to focus on the JComboBox, and try pressing the down key. He does not do anything.

Inserting Null at position 0 causes this. However, I would still like to be able to select Null. I do not want to force the user to choose an option.

package kobalt.test.colin; import java.awt.*; import javax.swing.*; public class ColinTest extends JFrame { private JTextField mTextField; private JComboBox mComboBox; public ColinTest(){ setLayout(new BorderLayout()); mTextField = new JTextField("Something"); mComboBox = new JComboBox(new String[]{"One", "Two"}); mComboBox.insertItemAt(null, 0); //this line causes the bug add(mTextField, "North"); add(mComboBox, "South"); pack(); setVisible(true); } public static void main(String[] argv) { new ColinTest(); } } 

Is there something I can override in JComboBox to fix this behavior?

I don't really like inserting an empty string at position 0, since I have to deal with this everywhere.

Using a Wrapping object may be an option, but I would prefer to extend and then override something in JComboBox.

+4
source share
2 answers

Zero objects do not play in JComboBox. For example, the combo box getSelectedIndex method that runs when an item is selected will return -1 if the object is null . There may also be other methods that perform null checks and may return incorrect results.

But you can try overriding getSelectedIndex so that it returns 0 instead of -1 if the object is null. Also override selectedItemChanged so that it does not check for zeros. The following seems to work, but there may be other methods that also need to be overridden:

 JComboBox mComboBox = new JComboBox(new String[]{"One", "Two"}){ @Override public int getSelectedIndex() { Object sObject = dataModel.getSelectedItem(); int i,c; Object obj; if(sObject==null){ return 0; } for ( i=0,c=dataModel.getSize();i<c;i++ ) { obj = dataModel.getElementAt(i); if ( obj != null && obj.equals(sObject) ) return i; } return -1; } @Override protected void selectedItemChanged() { fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, selectedItemReminder, ItemEvent.DESELECTED)); selectedItemReminder = dataModel.getSelectedItem(); fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, selectedItemReminder, ItemEvent.SELECTED)); } }; 

However, instead of doing the above, I would recommend using a wrapper object . For instance:

 class StringWrapper{ final String s; public StringWrapper(String s){ this.s=s; } @Override public String toString() { return s; } } JComboBox cb = new JComboBox(new StringWrapper[]{ new StringWrapper("one"), new StringWrapper("two"), new StringWrapper("three")}); cb.insertItemAt(new StringWrapper(null), 0); 
+2
source

Creating a combobox in this way creates a Vector-based DefaultComboBoxModel. Therefore, null values ​​cannot be inserted. You can try to implement ComboBoxModel with zero support.

+1
source

Source: https://habr.com/ru/post/1336443/


All Articles