Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Question on creating a custom DefaultListModel

Posted on 2004-09-10
2
Medium Priority
?
540 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

610 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