Super simple java swing jlist question that I just can't figure out?

You need to create a custom list model that returns objects to put in each row of a JList. TreeMap can't be accessed with an index, so you'll need something else. So the general idea is this: (from JList javadoc ).

Up vote 1 down vote favorite share g+ share fb share tw.

Ok, so I am working on a homework assignment, and I am using SWING to make a GUI for a Java project, and I am running into troubles with JList. I have a customer object that I have made and set attributes to, and I want to add the object into a TreeMap. I want to hook up the Treemap so that any and all objects that are in the map will populate (the name attribute anyway) inside of the JList.

I have done a lot of looking around, and am seeing a lot about coding these things from scratch but little dealing with Swing implementation. I put my customer object into my map and then I would like for my JList to reflect the map's contents, but I don't know how to hook it up. Customers.

Put(c.getName(), c); this.customerList.(What can I do here? Add Customer object? I can't find what I need); Thanks for your help!

Java homework swing jlist link|improve this question edited Oct 3 '10 at 4:11 asked Oct 2 '10 at 15:36Matt83.

You should have a look at the swing tutorial (download.oracle.com/javase/tutorial/uisw...). – tangens Oct 2 '10 at 15:39.

You need to create a custom list model that returns objects to put in each row of a JList. TreeMap can't be accessed with an index, so you'll need something else. So the general idea is this: (from JList javadoc): ListModel bigData = new AbstractListModel() { ArrayList customers; public int getSize() { return customers.size() } public Object getElementAt(int index) { return customers.

Get(index); } }; JList bigDataList = new JList(bigData); this way when you update your collection, just call revalidate() or repaint() on the JList and it will update its contents too.

You've given us your attempted solution, but since we don't know the actual requirement we can't tell if you are on the right track or not. Are you forced to use a TreeMap to store the objects? Because this is not a good collection to use for your ListModel since you can't access the objects directly.

Or is the assignment simply about displaying data from an object in a JList? If so then you can use the DefaultListModel. All you need to do is override the toString() method of you custom object to have the "name attribute" show in the list.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions