Solved

Question on creating a custom DefaultListModel

Posted on 2004-09-10
2
485 Views
Last Modified: 2011-09-20
I'd like to be able to display a 2D array within a JList.  The array will look like this:

private final String[][] selection =
     { {"value1","display string 1" }, {"value2","display string 2"} ... };

where the goal I'm trying to achieve is to view "the display strings" within the JList.

The first thing I did was to create this class that extends javax.swing.DefaultListModel:

public class ValueListModel extends DefaultListModel {
     private String[][] values;
     
     public ValueListModel(String[][] values) {
          this.values = values;
     }
      
     public Object getElementAt(int index) {
          return values[index][1];
     }
}

Next, I create a custom ListCellRenderer as:

public class ValueListCellRenderer extends JLabel implements ListCellRenderer {
     private String[][] values;
      
     public ValueListCellRenderer(String[][] values) {
          setOpaque(true);
          this.values = values;
     }

     public Component getListCellRendererComponent(JList list, Object value,
                  int index, boolean isSelected, boolean cellHasFocus) {
          setText(values[index][1]);
          setBackground(isSelected ? UIManager.getColor("List.selectionBackground")
                  : UIManager.getColor("List.background"));
          setForeground(isSelected ? UIManager.getColor("List.selectionForeground")
                  : UIManager.getColor("List.foreground"));
          return this;
     }
}

Finally in my GUI code, I instantiate instances of these classes and set the JList's model and cell renderer with the respective instances.  My problem is that the display strings are not being displayed.

Question: Will I have to override more of the methods of the DefaultListModel? If so, which ones?

Thanks in advance for any assistance.
0
Comment
Question by:mwalker
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
Comment Utility
You need

public int getSize() { return values.length; }

in your list model
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XML Paring  Error - Premature end of file. 7 55
unix in java example 9 37
Java and GPO 11 45
recursion example 16 66
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now