Solved

Question on creating a custom DefaultListModel

Posted on 2004-09-10
2
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 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