Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change JComboBox

Tags:

I am fetching the data values from the database successfully. I have also stored them into a String[] array. I need to load the String array as the items of the JComboBox in response to key actionperformed. How can I reload the items of the JComboBox whenever a key is pressed as the fetched values depend on the key pressed. Rather simply, I need to dynamically refresh the JComboBox items.

like image 475
Suman.hassan95 Avatar asked Jan 06 '11 21:01

Suman.hassan95


People also ask

What is the main difference between JComboBox and Jlistbox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

How do I make JComboBox not editable?

u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.

What is a JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .


2 Answers

DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>( yourStringArray ); comboBox.setModel( model ); 
like image 95
camickr Avatar answered Oct 02 '22 13:10

camickr


You have a couple of options. You can use removeAllItems() (or one of the other remove methods) and addItem(Object anObject) to manipulate the selectable objects. Or you could implement a ComboBoxModeland allow it to serve up data from your array.

like image 36
unholysampler Avatar answered Oct 02 '22 11:10

unholysampler