Solved

adding an icon to a record in a JList

Posted on 2000-03-12
11
784 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 30 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 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

22 Experts available now in Live!

Get 1:1 Help Now