Solved

adding an icon to a record in a JList

Posted on 2000-03-12
11
803 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 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