Solved

adding an icon to a record in a JList

Posted on 2000-03-12
11
781 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
Comment Utility
0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
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
Comment Utility
Hi diakov, can you give me a code example?
0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

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

Author Comment

by:twg
Comment Utility
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
Comment Utility
Post your code 'twg'. This seems like a bug in your code.

Cheers,
  Nik
0
 

Author Comment

by:twg
Comment Utility
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
Comment Utility
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
Comment Utility
Answer accepted
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now