Solved

Question on creating a custom DefaultListModel

Posted on 2004-09-10
2
497 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
ID: 12031276
You need

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

in your list model
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12031330
8-)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

832 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