• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regarding JList and DefaultListModel.

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um, JList and ListModel have been genericized in Java 7.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic