Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes How to add a String array to JList? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add a String array to JList?" Watch "How to add a String array to JList?" New topic

How to add a String array to JList?

vidu mayur
Ranch Hand

Joined: Nov 13, 2003
Posts: 32
I am trying to learn Swing and this program gives me an error message while trying to add an array element to JList:
cannot resolve symbol method add(java.lang.String) in class javax.swing.JList . Please help me with the command which I have to add there instead of this add method
This is what the program is supposed to do. A phoneDirectory program that stores names and phone numbers will display name and number as labels with name and number text fields on the side. There will be a list in the middle which will be initially empty. As the user adds information to the textFields, the list will display the names. It also has three buttons "Add", "Delete", "Clear". The user can perform the following action:
Entering name & number in the text fields and then pressing the Add button adds the info to the database. The name is displayed at the end of the list and then goes blank. The name and number are stored as parallel arrays.
View a number by clicking the name in the list causes the name & corresponding number to be displayed in the fields
modify a name & number after the name has been selected and displayed. The change goes into effect when the enter key is pressed.
I am really sorry for making the post a long one.

Thank you very much for reading it so farinitially empty
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You can't add elements to a "JList" using the "add()" method. The "add()" method is inherited from "java.awt.Container", and is used to add a UI component to container, such as adding a button to a frame.
But don't despair ... you can do it. There are good overviews of using Swing components on Sun's website, and they show you exactly how to do it. Look at theHow to Use Lists page and you'll see how to add and remove elements from the list. Happy "Swing"-ing.
Simon Pearce

Joined: Jul 22, 2003
Posts: 4
I must confess I find JList one of the most frustrating gadgets in Swing. (Actually I find Swing a pain in the neck, generally, but it has its moments. I quite like JTable, for example).
The DEFAUL behaviouor of JList is pretty useless - you can have a fixed list of stuff. No adds, no changes.
You do this by declaring a JList with an array of Strings like this:
String[] patterns = {
"the other"};
private JList jList = new JList(patterns);
Lo and behold. But you can't CHANGE anything.
Nah, to do that, you have to use a DefaultListModel.
DefaultListModel info = new DefaultListModel();
JList jList = new JList(info); //default used does not allow changes!!! Use a DefaultListModel like this - which does ...

you can add stuff in like this:
info.addElement("new list entry");

That should get you started. Enjoy. Try not to get too cross with the model (I know I did).
Simon (who is calmer, these days)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Simon is correct. However, he is crazy for thinking a JList is more difficult than a JTable. JTables can frustrate me to no end.
Keep in mind that the reason components in swing use a model like DefaultListModel and DefaultTableModel and DefaultTreeModel, etc, is because SWING is built as an MVC architecture. This allows you to keep the View seperate from the model entirely. Which is really good design. So when you need to make a change, you change it on the Model and let the View get updated naturally.
I don't really find too many things about SWING that I don't like. But that is another topic all together.

GenRocket - Experts at Building Test Data
vidu mayur
Ranch Hand

Joined: Nov 13, 2003
Posts: 32
Thanks for helping me out.
Wayne and Simon, the Default List Model helped me update the list. Thanks for taking the time to explain it to me.
Gregg, I think Swing is better than many other topics which I have learnt so far.
I agree. Here's the link:
subject: How to add a String array to JList?
It's not a secret anymore!