Solved

Java JList issue

Posted on 2011-09-12
7
347 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
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

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

20 Experts available now in Live!

Get 1:1 Help Now