Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding an icon to a record in a JList

Posted on 2000-03-12
11
Medium Priority
?
813 Views
Last Modified: 2008-02-01
I have a JList full of records.
By pressing a certain button, I want to add an icon (to the left side) only to one of the records. How do I do that?
0
Comment
Question by:twg
[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
11 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 2609301
0
 
LVL 8

Expert Comment

by:diakov
ID: 2609394
You need to define own ListCellRenderer that returns a JLabel component. JLabel can be set with an icon (Image).

Cheers,
  Nik
0
 

Author Comment

by:twg
ID: 2609518
Hi diakov, can you give me a code example?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:diakov
ID: 2609652
The SwingSet example that comes with the JDK contains a list box with a custom cell renderer. Their code is much better than my examples :-) You just need to make it more dynamic. Every time you change an element of the list, i.e. show/remove an icon you must call fireContentsChanged() on the default model of the JList.

Cheers,
  Nik
0
 

Author Comment

by:twg
ID: 2627452
First of all, Thank you for the answers.
I read the examples and I did it. In the method I put an if:
if (isSelected)
{
     setIcon(...);
}
The problem is that when I select one record, an icon is set on some other records in the list. (actually on all the records which are below the one that I selected). Do you have any idea why is this hapenning?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2854024
twg, have you solved this problem or are you still waiting for an answer?
0
 

Author Comment

by:twg
ID: 2854982
Hi jim, I'm still waiting for an answer. As you can see, no one answered the problem I introduced in my comment (17.3).
Please help me.
0
 
LVL 8

Expert Comment

by:diakov
ID: 2855062
Post your code 'twg'. This seems like a bug in your code.

Cheers,
  Nik
0
 

Author Comment

by:twg
ID: 2855168
Here is an example of what is happening.
Try to run this code.
When it's running, press on "four", then on "one" and you'll see that also "two" and "three" get the icon. why is this??



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class JListWithImage extends JFrame {
 
  public JListWithImage() {
    super("CheckList Example");
    String[] data = {"one", "two", "three", "four"};
             JList dataList = new JList(data);
             dataList.setCellRenderer(new MyCellRenderer());
        this.getContentPane().add(dataList);
  }//end ctor
        
        class MyCellRenderer extends JLabel implements ListCellRenderer {
        ImageIcon icon = new ImageIcon("dot.gif");
     //final static ImageIcon shortIcon = new ImageIcon("short.gif");

     // This is the only method defined by ListCellRenderer.
     // We just reconfigure the JLabel each time we're called.

     public Component getListCellRendererComponent(
       JList list,
       Object value,            // value to display
       int index,               // cell index
       boolean isSelected,      // is the cell selected
       boolean cellHasFocus)    // the list and the cell have the focus
     {
         String s = value.toString();
         setText(s);
         
         if (isSelected) {
                     setIcon(icon);
            //setBackground(list.getSelectionBackground());
            //setForeground(list.getSelectionForeground());
           }
        /* else {
               setBackground(list.getBackground());
               setForeground(list.getForeground());
           }
           setEnabled(list.isEnabled());
           setFont(list.getFont());*/
         return this;
     }//end method
        }//end class renderer
        

    public static void main(String args[]) {
    JListWithImage frame = new JListWithImage();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    frame.setSize(300, 200);
    frame.setVisible(true);
  }//end main
        
}//end class
0
 
LVL 8

Accepted Solution

by:
diakov earned 120 total points
ID: 2855862
Have in mind 2 things:

1. set the selection mode suits you, e.g. dataList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
2. When doing
         if (isSelected) {
          setIcon(icon);
           }

//do not forget to undo the icon otherwise with
         else {
          setIcon(null);
           }
2 is the main problem.


Try this code:
--------------

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class JListWithImage extends JFrame {
   
  public JListWithImage() {
    super("CheckList Example");
    String[] data = {"one", "two", "three", "four"};
  JList dataList = new JList(data);
  dataList.setCellRenderer(new MyCellRenderer());
  dataList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
 
   this.getContentPane().add(dataList);
  }//end ctor
   
   class MyCellRenderer extends JLabel implements ListCellRenderer {
        ImageIcon icon = new ImageIcon("dot.gif");
     //final static ImageIcon shortIcon = new ImageIcon("short.gif");

     // This is the only method defined by ListCellRenderer.
     // We just reconfigure the JLabel each time we're called.

     public Component getListCellRendererComponent(
       JList list,
       Object value,            // value to display
       int index,               // cell index
       boolean isSelected,      // is the cell selected
       boolean cellHasFocus)    // the list and the cell have the focus
     {
         String s = value.toString();
         setText(s);
         
         if (isSelected) {
          setIcon(icon);
            //setBackground(list.getSelectionBackground());
            //setForeground(list.getSelectionForeground());
           }
         else {
          setIcon(null);
           }
           /*
           setEnabled(list.isEnabled());
           setFont(list.getFont());*/
         return this;
     }//end method
   }//end class renderer
   

    public static void main(String args[]) {
    JListWithImage frame = new JListWithImage();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    frame.setSize(300, 200);
    frame.setVisible(true);
  }//end main
   
}//end class

Cheers,
  Nik
0
 

Author Comment

by:twg
ID: 2857310
Answer accepted
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

704 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