Adding JCheckBox into JList

Posted on 2005-03-15
Medium Priority
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")};
    JList list=new JList(stats_option);
      public void valueChanged(ListSelectionEvent evt)
        JList list=(JList)evt.getSource();
        JCheckBox check= (JCheckBox)list.getSelectedValue();
    list.setCellRenderer(new Stats_cell_renderer());
    JScrollPane scroll=new JScrollPane(list);
  class Stats_cell_renderer implements ListCellRenderer
    public Component getListCellRendererComponent(JList list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus)
      JCheckBox check=(JCheckBox)value;
      background = isSelected ? list.getSelectionBackground() : list.getBackground();
      foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
      return check;
    public Dimension getPreferredSize()
      return new Dimension(100, 50);
    private Color background;
    private Color foreground;
Question by:csound
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
1 Comment
LVL 92

Accepted Solution

objects earned 200 total points
ID: 13549823

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

765 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