Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way to populate a JComboBox?

I am currently building an application in Java on Eclipse as a self help guide to programming fundamentals and basic java programming, this is purely educational and for the sole purpose of being able to reference topics easily and practice my programming as I learn them by programming them into this tutorial application.

The content of the application will expand as time goes on and as I learn more components of programming.

So my first question comes down to correct form.

I am using a drop down box (JComboBox) so as to select specific topics from within the GUI. I would like to populate the list and keep the program clean and tidy. So my question is how would one populate the JComboBox so as to limit cluttering code. Perhaps a text file from which I could add topics to separately and edit more efficiently? I am after correct programming procedure as opposed to all the ways I could do it. I know I could use an ArrayList, however I am keen to understand the choices taken when using large amounts of content as opposed to very little.

Thanks,

Simon

like image 353
London Student Avatar asked Jun 25 '12 09:06

London Student


People also ask

How do you populate a JComboBox from an arrayList?

String[] array = arrayList. toArray(new String[arrayList. size()]); JComboBox comboBox = new JComboBox(array); Alternatively, you can also maintain strong typing by just using a for loop.

Which of these is used to determine whether the JComboBox is editable?

SetEditable() - Determines whether the JComboBox field is editable.


2 Answers

I think the cleanest way is to define a custom ComboBoxModel.

This way you can define a data model for your combobox, separating the part in which the combobox is created from the data management itself.

Probably using a text file is a good thing, since you don't have to modify the code when a new entry is inserted. You can define the reading file procedure inside your ComboBoxModel constructor. This way every time you run the program you'll find updated combobox's contents.

ArrayList isn't a good choice if the contents can't be updated by the application itself. If you are hardcoding the contents of an arraylist, you will be forced to modify the code every time you need to add a new entry.

A little example:

class YourModel implements ComboBoxModel{

//implements all interface methods required...
@override
public YourModel(String filename)
{
    comboBoxItemList = new ArrayList<String>();
    // open your file
    // add every entry to the the list
}
@override
public Object getElementAt(int index)
{
    return comboBoxItemList.get(index);
}
List<String> comboBoxItemList;
}

Once written what you need you will not to modify the code anymore. And you can use the same model for several different JComboBox also.

YourModel model = new YourModel("path_to_a_file");
JComboBox box1 = new JComboBox();
box1.setModel(model);
JComboBox box2 = new JComboBox();
box2.setModel(model);
like image 185
Heisenbug Avatar answered Oct 23 '22 09:10

Heisenbug


The easiest way of populating a Combobox is (as Java documentation states) is:

String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(petStrings);

This is, however, not the best option you could go for. Populating your combobox with an array of strings is not the best option that offers flexibility and model/UI decoupling. This is where the MVC model comes into play. The MVC model basically tells you to use a Model (in your case a ComboBoxModel) to back your data out. Having a model offers you the possibility and flexibility of getting your data from anywhere you want (files, sockets, web service...)

like image 30
GETah Avatar answered Oct 23 '22 09:10

GETah