Solved

Java JList issue

Posted on 2011-09-12
7
349 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:CloudStrife209
  • 5
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36526932


>I've added the following code to a JList

Please, post your code the part were you craete the list and set the renderer
0
 

Author Comment

by:CloudStrife209
ID: 36526937
here you go
JList listing = new JList(occupationList);
listing.setCellRenderer(new MyCellRenderer());

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36526953
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?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 47

Expert Comment

by:for_yan
ID: 36527024
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

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36527059
This is another variant how it works:

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 MyCellRenderer1());

        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 MyCellRenderer1 extends JLabel implements ListCellRenderer {

  public MyCellRenderer1() {
    setOpaque(true);
  }

  public Component getListCellRendererComponent(JList list, Object value, int index,
      boolean isSelected, boolean cellHasFocus) {
    setText(value.toString());

    if (isSelected) {
      setBackground(list.getSelectionBackground());
      setForeground(list.getSelectionForeground());
    } else {
      setBackground(list.getBackground());
      setForeground(list.getForeground());
    }
    return this;
  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36527075
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

0
 

Author Closing Comment

by:CloudStrife209
ID: 36527082
Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
Java - Why doesn't this JFrame work 3 58
even odd program using while loop 3 40
Desingning Refactoring existing code 2 24
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

815 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

7 Experts available now in Live!

Get 1:1 Help Now