aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Regarding JList and DefaultListModel. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Regarding JList and DefaultListModel." Watch "Regarding JList and DefaultListModel." New topic
Author

Regarding JList and DefaultListModel.

Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Hi guys.

I'm building an application which so far consists of a JFrame with a JScrollPane, and inside the JScrollPane Im using a JList to display objects.

I've taken the approach of using a DefaultListmodel to contain the objects I want displayed, and I have a couple of questions...

1: I'm using the JList.getSelectedIndex() to find out which "row" (and corresponding object) is selected. I have been unable find explicit information on this, but if I want the object connected to a specific entry in the JList, then calling DefaultListModel.elementAt() with the index from JList.getSelectedIndex() seems like the only reasonable approach, so can I fully count on that the index returned from the JList will represent the correct index in the DefaultListModel?

2: The objects which I use to populate the DefaultListModel are first stored in an ArrayList, so when I populate the DefaultListModel I am forced to iterate through the arraylist to get each element. Basically I feel like this is maybe a "clumsy" approach - I would have thought that the DefaultListModel was a Collection of some kind, so that any changes could be easily made to it and would automatically be reflected in the JList. Right now I'm cosidering implementing my own ListModel using a Wrapper around an ArrayList (decorator pattern), but implementing all that might just be unecessary work? I guess what I'm asking is, what's the more standard approach to populating/refreshing a DefaultListModel in a flexible way, do you generally have a collection class like ArrayList and pick out the elements one by one?

3: If I use a DefaultListModel to contain the elements to be displayed in the JList, is there a way to specify which method that should be called on the elements when they are listed in the JList? Ie: can I somehow specify for the DefaultListModel or JList that it shouldn't use toString(), but instead for example getDescription() ?

4: When using JList and DefaultListModel I get warnings that it is a raw type, and should be specified with a generic type. I'm not new to generics, but I'm a little surprised to find that these two classes use them. Is it generally best to utilize generic types for these classes or will it most likely be unecessary?

Thanks in advance, and yes I'm new to building GUI's

// Andreas
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Welcome to the GUI world!

1) Yes
2) Check out the JList constructor(s)
3) Check out the custom renderer section here http://download.oracle.com/javase/tutorial/uiswing/components/list.html#renderer
4) Avoid them and lose the generics features.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
Thel models in the base JDK for JList, JTable, JTree etc. have not been updated to use the new Collections API. They still use Vectors instead of Lists.

To create your own custom model that used a List you could just copy the base code and basically replace all references to the Vector with a reference to a List and then use an ArrayList as the data storage.

If you want to add generics to the model then it would be a little more trouble. I did this with for table when I created a Row Table Model. The list model would be much simpler since it only need to worry about the data.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Thanks for the input guys.

I've just tried modifying my code to use a Vector instead of a DefaultListModel, but I have a problem where it seems the JList will not update - presumably because as the description for the Vector-constructor implies - the ListModel constructed is read-only and will not respond to changes in the Vector it was based on. That kind of defeats the purpose for using a Vector in the first place doesn't it? I guess I'm back to using the DefaultListModel since optimization isn't the highest priority for me right now.

As for the generics, I think I'll just forget about them and suppress the warnings I get from Eclipse.

// Andreas
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Andreas Svenkson wrote:I've just tried modifying my code to use a Vector instead of a DefaultListModel, but I have a problem where it seems the JList will not update - presumably because as the description for the Vector-constructor implies - the ListModel constructed is read-only and will not respond to changes in the Vector it was based on. That kind of defeats the purpose for using a Vector in the first place doesn't it? I guess I'm back to using the DefaultListModel since optimization isn't the highest priority for me right now.

When a Vector is modified it doesn't fire the necessary events. The constructor says that the created list model is read-only. There's nothing wrong with using DefaultListModel; if you need to edit the contents it's even one of the best solutions.

As for the generics, I think I'll just forget about them and suppress the warnings I get from Eclipse.

What warnings are you getting, and what's the code that's generating these warnings?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Um, JList and ListModel have been genericized in Java 7.


luck, db
There are no new questions, but there may be new answers.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179

Well right now I have removed my JList and am using a JTree instead, but the warning came from for example:



...with the warning: "JList is a raw type. References to generic type JList<E> should be parameterized."

// Andreas
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
I've just tried modifying my code to use a Vector instead of a DefaultListModel,


That statement make no sense. A DefaultListModel already uses a Vector to store the data.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Andreas Svenkson wrote:
Well right now I have removed my JList and am using a JTree instead, but the warning came from for example:



...with the warning: "JList is a raw type. References to generic type JList<E> should be parameterized."

// Andreas

As Darryl said (and which is completely new to me), JList has been made generic. All you needed to do is add the generic type - the actual object type you store, or Object to make it behave like the old Java 6 (and before) JList:
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Camick, what I meant was that I was using the JList constructor that takes a Vector, instead of a DefaultListModel...

Spoor, thanks I know. I just was unsure of how useful it would be to use generics in these classes, and was wondering if people generally did specify the generic type or not.

// Andreas
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding JList and DefaultListModel.