Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inserting items into a jlist  RSS feed

 
nadia mcday
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I am having a weird problem with my jlist. I am declaring an array of strings, and then I am using List.setListData(array) to display the items. It worked fine in one place, then in another place it just wouldn't work! I am using System.out.println to print the contents of the array, and everything is in order there, but it just won't display to the GUI. What possible reason could there be for this??
Thanks a lot!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting?
 
nadia mcday
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's just the thing! Everything is working normally, there are no exceptions, no errors, but the values of the array just aren't appearing in the GUI list! It's really weird, I just want to know what could be happening, if there is anything that I could possibly be doing that would make a list behave like that! (You don't have to delete the items in a list before you add new ones, right? I just want to make sure, even though nothing is appearing in the first place...)
Thanks
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post a code snippet. It will increase the chances of getting more response in a short period. Its very difficult to guess what you have done in your code.

sat
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Satishkumar.
Especially the code, where you are setting the new values, would be helpful
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nadia mcday wrote:
I am using List.setListData(array) to display the items. It worked fine in one place, then in another place it just wouldn't work! I am using System.out.println to print the contents of the array, and everything is in order there, but it just won't display to the GUI.


It's hard to tell from way over here, but often this is because the items are being added to an instance of JList that is different from the instance that is being displayed by the GUI. That's is why we have asked to see your code.

Otherwise yourJList.setListData(....) should work. If it's only an item or two you want to add, the usual way would be (DefaultListModel(yourJList.getModel()).addElement(...). Or if you don't like the cast, create your JList like this:

DefaultListModel yourListModel = new DefaultListModel();
// ...
JList yourJList = new JList(yourListModel);

then when the time is right call addElement(), insertElementAt(), or set()/setElementAt() on yourListModel.
In this case you would need to maintain a reference to yourListModel, but probably wouldn't need to keep track of yourJList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!