• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add a String array to JList?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 399
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = {
"this",
"that",
"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)
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic