Link to home
Start Free TrialLog in
Avatar of CloudStrife209
CloudStrife209

asked on

Java JList issue

I've added the following code to a JList and am now unable to select individual items in the list

Any Ideas? For reference getANZIC returns a string value
@SuppressWarnings("serial")
class MyCellRenderer extends JLabel implements ListCellRenderer {

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
    {
	
    
        setText(((Occupation)value).getANZIC());
        return this;
    }
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image



>I've added the following code to a JList

Please, post your code the part were you craete the list and set the renderer
Avatar of CloudStrife209
CloudStrife209

ASKER

here you go
JList listing = new JList(occupationList);
listing.setCellRenderer(new MyCellRenderer());

Open in new window

What means unable to select?
How do you judge that it is not selecting?

Can you post more of your code so that I copuld try
to run it myself?
It would work if you extend DefaultListCellRenderer.
Try this code:

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;

public class JListTest extends JFrame {

    public JListTest() {
        super("test");

        Vector<Occupation> ar = new Vector<Occupation>();
      ar.add(new Occupation("MD"));
       ar.add(new Occupation("PD"));
       ar.add(new Occupation("LD"));

        JList l = new JList(ar);
        l.setCellRenderer(new MyCellRenderer());

        Container c = this.getContentPane();
        c.add(l);

        this.setSize(300,300);
        this.setVisible(true);





    }

    public static void main(String[] args) {
        new JListTest();
    }

}
class Occupation {
    String text;

    Occupation(String s){
        text = s;
    }

    public String toString(){
        return text;
    }

}

@SuppressWarnings("serial")
class MyCellRenderer extends  DefaultListCellRenderer{

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
    {

          System.out.println("value: " + value);
          System.out.println(isSelected);
           System.out.println(cellHasFocus);

        if(isSelected)this.setBackground(Color.blue);
        else    this.setBackground(Color.white);
       this.setText(((Occupation)value).toString());
        return this;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is based on your code and it is working.

You of course need to add if(isSelected)... else
and another thing which turns out to be impiortnat
is setOpaque(true) in the constructor.

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;

public class JListTest extends JFrame {

    public JListTest() {
        super("test");

        Vector<Occupation> ar = new Vector<Occupation>();
      ar.add(new Occupation("MD"));
       ar.add(new Occupation("PD"));
       ar.add(new Occupation("LD"));

        JList l = new JList(ar);
        l.setCellRenderer(new MyCellRenderer());

        Container c = this.getContentPane();
        c.add(l);

        this.setSize(300,300);
        this.setVisible(true);





    }

    public static void main(String[] args) {
        new JListTest();
    }

}
class Occupation {
    String text;

    Occupation(String s){
        text = s;
    }

    public String toString(){
        return text;
    }

}




class MyCellRenderer extends JLabel implements ListCellRenderer {
     public MyCellRenderer() {
    setOpaque(true);
  }

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
    {
         
         if (isSelected) {
      setBackground(Color.blue);
      setForeground(Color.black);
    } else {
      setBackground(Color.white);
      setForeground(Color.black);
    }

        setText(((Occupation)value).toString());
        return this;
    }
}

Open in new window

Thanks