Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Adding JCheckBox into JList

Posted on 2005-03-15
1
Medium Priority
?
2,100 Views
Last Modified: 2008-10-16
I am trying to put JCheckBox into JList.

What I want is

1 When a check box is clicked, it automatically changes its state
2 Get all the check boxes in the list whose states are  ‘selected’.

The following is my attempt to do it.

At the moment when I click on a check box it turns itself on and then it quickly turns itself off.
Also, I can not get all the check boxes whose states are ‘selected’.


private final Component[] stats_option={new JCheckBox("1"), new JCheckBox("2"), new JCheckBox("3"), new JCheckBox("4"), new JCheckBox("5")};
 
GUITest()
  {
    JList list=new JList(stats_option);
    list.setVisibleRowCount(3);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.addListSelectionListener(new
                                    ListSelectionListener()
                                    {
      public void valueChanged(ListSelectionEvent evt)
      {  
        JList list=(JList)evt.getSource();
        JCheckBox check= (JCheckBox)list.getSelectedValue();
       
        if(check.isSelected())
        {
          check.setSelected(false);
        }
        else
        {
          check.setSelected(true);
        }
      }
     
    });
    list.setCellRenderer(new Stats_cell_renderer());
    JScrollPane scroll=new JScrollPane(list);
   
    add(scroll);
  }
 
  class Stats_cell_renderer implements ListCellRenderer
  {  
    public Component getListCellRendererComponent(JList list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus)
    {  
      JCheckBox check=(JCheckBox)value;
      //check.setText((String)value);
      background = isSelected ? list.getSelectionBackground() : list.getBackground();
      foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
      check.setBackground(background);
      check.setForeground(foreground);
      return check;
    }
   
    public Dimension getPreferredSize()
    {  
     
      return new Dimension(100, 50);
    }
   
    private Color background;
    private Color foreground;
  }
 
0
Comment
Question by:csound
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 13549823
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

571 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