Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

adding an icon to a record in a JList

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
twg
Asked:
twg
1 Solution
 
expertmbCommented:
0
 
diakovCommented:
You need to define own ListCellRenderer that returns a JLabel component. JLabel can be set with an icon (Image).

Cheers,
  Nik
0
 
twgAuthor Commented:
Hi diakov, can you give me a code example?
0
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.

 
diakovCommented:
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
 
twgAuthor Commented:
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
 
Jim CakalicSenior Developer/ArchitectCommented:
twg, have you solved this problem or are you still waiting for an answer?
0
 
twgAuthor Commented:
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
 
diakovCommented:
Post your code 'twg'. This seems like a bug in your code.

Cheers,
  Nik
0
 
twgAuthor Commented:
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
 
diakovCommented:
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
 
twgAuthor Commented:
Answer accepted
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now