0

I am developing a GUI where I have included some Checkboxes into a Jlist but I cannot read their status. Below i have an example of my code

final JScrollPane scrollPaneWat = new JScrollPane();
        scrollPaneWat.setBounds(526, 248, 152, 140);
        Pane.add(scrollPaneWat);
        scrollPaneWat.hide();

        @SuppressWarnings("unchecked")
        final JList<?> list = new JList(new CheckListRendererClasses[] {
                new CheckListRendererClasses("a"),
                new CheckListRendererClasses("b"),
                new CheckListRendererClasses("c"),
                new CheckListRendererClasses("d"),
                new CheckListRendererClasses("e"),
                new CheckListRendererClasses("f"),
                new CheckListRendererClasses("g"),
        scrollPaneWat.setViewportView(list);
        list.setBackground(Color.LIGHT_GRAY);
        list.setFont(new Font("Times New Roman", Font.ITALIC, 14));

        list.setCellRenderer(new CheckListRenderer());
          list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          list.addMouseListener(new MouseAdapter(){
             public void mouseClicked(MouseEvent event){
                JList list = (JList) event.getSource();
                int index = list.locationToIndex(event.getPoint());
                CheckListRendererClasses item = (CheckListRendererClasses)
                   list.getModel().getElementAt(index);
                item.setSelected(! item.isSelected());
                list.repaint(list.getCellBounds(index, index));
             }
          });

I have tried this:

for (int i = 0; i < 7; i++) {

    Object sel = list.getModel().getElementAt(i);

    System.out.print(sel.toString());
}

and I get back a, b, c, d, e, f, g

Also, when i tried this:

System.out.println(list.isSelectedIndex(1));

if I tick the Checkbox a, it prints true. But if the Checkbox a is ticked and I tick one more (so now I have ticked the Checkbox a and c) it returns false.

It's like it keeps my last choice.

What I want is, for example if I have ticked the a, c, d Checkboxes to be able to make a for loop and see everytime if a checkbox at value i is ticked or not.

Thank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3358377
  • 145
  • 3
  • 16
  • 2
    Use JTable instead. Example here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – Sergiy Medvynskyy Jun 04 '14 at 10:03
  • 1
    list.repaint(list.getCellBounds(index, index)); talking about CheckListRendererClasses isn't good designated, or usage of is wrong, by default casted value (Boolean) from model must to returns true of false, for String value "true" of "false", otherwise put this class to the trash – mKorbel Jun 04 '14 at 10:08
  • @Sergiy Medvynskyy could be a nice answer (with getColumnClass) – mKorbel Jun 04 '14 at 10:09
  • I think the problem might be with this line: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Surely that means only one checkbox can be selected? – Si Kelly Jun 04 '14 at 10:10
  • @mKorbel It would be a good idea to develop a table which can edit some checkable options. If I have time this evening, I will try to do it. – Sergiy Medvynskyy Jun 04 '14 at 10:23
  • @user3358377 I'm strongly suggest to use [SINGLE_SELECTION](http://stackoverflow.com/a/7620693/714968) – mKorbel Jun 04 '14 at 11:26

1 Answers1

1

Change ListSelectionModel.SINGLE_SELECTION to MULTIPLE_INTERVAL_SELECTION to allow more than one selection at a time.

Si Kelly
  • 703
  • 3
  • 9