Solved

Java JList issue

Posted on 2011-09-12
7
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

617 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