Problems clearing and adding items to Jlist box.
I have a set of 12 items stored in each record of a database. These record items are named as follows:
LANGUAGE1
LANGUAGE2
.
.
LANGUAGE12

I have no problems in accessing the database.
rs.getString("LANGUAGE1")
and so on....

I need to clear an existing jlist box called LANGUAGES and place the record items listed above sequentially into the jlist box

Any assistance would be most welcome
thanks in advance...... mike

Recommended Answers

All 5 Replies

One final note: this code does not have to be in the form of a function, simply code to be pasted into the the program itself.... thanks

The trick is to get the list box's ListModel (the ListModel maintains all the data that the list displays). That will give you an instance of DefaultListModel which has methods to clear the model, add elements to it etc.

This is my code:

    String[] items = {rs.getString("LANGUAGE1"),rs.getString("LANGUAGE2"),rs.getString("LANGUAGE3"),rs.getString("LANGUAGE4"),rs.getString("LANGUAGE5"),rs.getString("LANGUAGE6"),rs.getString("LANGUAGE7"),rs.getString("LANGUAGE8"),rs.getString("LANGUAGE9"),rs.getString("LANGUAGE10"),rs.getString("LANGUAGE11"),rs.getString("LANGUAGE12")};
    DefaultListModel LIST_MODEL.getModel(JList (NEW_LANGUAGES));
    for(int i = 0;i<=11; i++) {   
        LIST_MODEL.clear();
        LIST_MODEL.addElement(items[i]); 
        System.out.println(items[i]);
        }

The problem that I am having is in line 2. I am not getting the listmodel, therefore nothing is being changed or added to the Jlist box. Data is being stored in the array as shown in the println statement. the error suggests missing";"

Line 2 syntax is a bit garbled. This is the easy way to do it:
1. Create a DefaultListModel
2. Create the JList using that model
3. Add stuff to the model
4. Clear/add to the model as required

Got it figured out.... thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.