Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Use JList and DeafultListModel to create a list with multiple variables for each entry?  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use “JList” and “DefaultListModel” to store more than one string for each entry?
For every unique “item” created to the list, I want a name AND a number.
The number could be stored in a string, ideally in an integer but that’s not that important.

I have written some working code with a GUI that let’s the user add a name to the list.
So here’s where I’m stuck. Like I said for every name, I would also want to require the user to input a number for that name.

My code is quite long sorry for that.  Better to post the whole wall of text than to leave out something important I guess.


 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user chooses the number itself or is it a reference to something?
You could use an ListSelectionListener and prompt the user for the number.

Note: your code would be easier to read if you use seperate codetags for each class
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:The user chooses the number itself or is it a reference to something?
You could use an ListSelectionListener and prompt the user for the number.

Note: your code would be easier to read if you use seperate codetags for each class


Yes the user chooses the number.
But the number must also be stored in the same "item" as the corresponding name in the DefaultListModel.

Okay thanks. I will look into it.
So there's no problem for  DefaultListModel to store multiple items?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might have to store the number as an String (which it already will be since you're using a textfield to get it), but I'm not that familiar with swing
 
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Note: your code would be easier to read if you use seperate codetags for each class


I don't see more than one class.  This class has an inner class, but I wouldn't want separate code tag pairs for that.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, you're right, I didn't really read just quickly scanned the code.
Even worse... That's a lot of code to put in your Main.class
 
Rancher
Posts: 3007
20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Better to post the whole wall of text than to leave out something important I guess.



You posted way too much code.

But the number must also be stored in the same "item" as the corresponding name in the DefaultListModel.



Your question is about using a DefaultLiistModel to contain the name and number. All the other code is irrelevant to that requirement.

So first you need to learn how to display an Object with multiple properties in a JList.

If you look at the DefaultListModel you will see that you add an Object to the model, not a String, so you can create a custom Object that contains both the name and number properties.

Then you next need to decide what to display in the JList. Do you want just the name, number or name and number together?

Then you have a couple of option:

1. The default renderer for a JList will simply invoke the toString() method of the Object contained in the ListModel. So you can implement the toString() method of your custom object to display whatever you want.
2. Implement a custom renderer to display the data from your custom object.

Check out Combo Box With Hidden Data for an example implementation of a generic Object used to hold multiple properties. Yes the example is for a combo box, but the concept of using the custom Object with a custom toString() method is the same for a JList and a JComboBox.

Like I said for every name, I would also want to require the user to input a number for that name.



How you get the name and number to create the custom Object would now be an implementation method of your application.

The common approach would be to prompt for the name and number at the same time so you can create your custom Object.

The number could be stored in a string, ideally in an integer but that’s not that important.



Since you are creating the custom Object to be stored in the model, you can store the number however you want. I would store it as an int if that is how you would use it in future processing.

 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:



1. The default renderer for a JList will simply invoke the toString() method of the Object contained in the ListModel. So you can implement the toString() method of your custom object to display whatever you want.




Thank you dude. Yes you are right to much irrelevant code.
How would toString() look if I wanted to display a string and an int?
I wrote something like this right now:



But then eclipse tells me that I need to change the int variable to a string or change the return type to a int.

 
Rob Camick
Rancher
Posts: 3007
20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How would toString() look if I wanted to display a string and an int?



You concatenate the String and int into a single String variable and return the String variable.

This is basic Java for manipulating a String and has nothing specific to do with the toString() method.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you use one of these methods:
   Press Alt + Shift + S + S (double)
   Right click -> Source -> Generate toString() ...
   Go to Source menu -> Generate toString() ...
   Go to Windows menu -> Preferences -> General -> Keys (Write Generate toString on text field)
to let eclipse genrate it for you
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!