Solved

Java JList issue

Posted on 2011-09-12
7
348 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstChar challenge 13 106
JDeveloper 12c for 32 bit 4 67
HSSFWorkbook cannot be resolved error 10 49
fibonacci ten numbers 4 24
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

919 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

17 Experts available now in Live!

Get 1:1 Help Now