I have this small snippet of code that display a simple panel with a list of checkboxes. The behavior must be the following:
if the checkbox "[] all" is selected every other checKbox must be unchecked, otherwise (when the "[] all" checkbox isn't selected) the user can select any number of checkboxes.
if a checkbox different from "[] all" is checked then the "[] all" checkbox must be unchecked
Far from now nothing strange except that if the checkbox "[] all" is selected then the user must press twice each other checkbox in order to mark it as selected. The first click uncheck the "[] all" checkbox but does not check the selected checkbox. I would like to get both behaviors together. I guess miss something (a sync event maybe...)
Here the code (is self contained so if you want you can run it as it is):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class CheckListPlot extends JFrame {
public CheckListPlot() {
super("CheckList");
String[] strs = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
JLabel label = new JLabel("Please, before to start the simulation, choose what species you desire to plot");
Font font = new Font("Tahoma", label.getFont().getStyle(), label.getFont().getSize() - 2);
label.setFont(font);
JPanel allCheckpanel = new JPanel(new BorderLayout());
allCheckpanel.setBorder(new EmptyBorder(6, 6, 6, 6));
final JCheckBox allCheck = new JCheckBox("all", false);
allCheckpanel.add(allCheck, BorderLayout.WEST);
JPanel labelPanel = new JPanel(new GridLayout(1, 1));
labelPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
labelPanel.add(label);
final JList list = new JList(createData(strs));
list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setBorder(new EmptyBorder(0, 4, 0, 0));
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
CheckableItem item = (CheckableItem) list.getModel().getElementAt(index);
System.out.println("item " + item.toString() + " is selected before? " + item.isSelected());
item.setSelected(!item.isSelected());
System.out.println("item " + item.toString() + " is selected after ? " + item.isSelected());
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);
if(allCheck.isSelected()){
allCheck.setSelected(false);
}
}
});
JScrollPane sp = new JScrollPane(list);
JButton simulateButton = new JButton("simulate");
simulateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
int n = model.getSize();
for (int i = 0; i < n; i++) {
CheckableItem item = (CheckableItem) model.getElementAt(i);
if (item.isSelected()) {
System.out.println(item.toString());
}
}
}
});
JPanel panel = new JPanel(new GridLayout(1, 1));
panel.add(simulateButton);
allCheck.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
for(int i =0 ; i< list.getModel().getSize(); i++){
((CheckableItem)list.getModel().getElementAt(i)).setSelected(false);
Rectangle rect = list.getCellBounds(i, i);
list.repaint(rect);
}
}
});
getContentPane().add(labelPanel, BorderLayout.NORTH);
getContentPane().add(allCheckpanel, BorderLayout.NORTH);
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
}
private CheckableItem[] createData(String[] strs) {
int n = strs.length;
CheckableItem[] items = new CheckableItem[n];
for (int i = 0; i < n; i++) {
items[i] = new CheckableItem(strs[i]);
}
return items;
}
class CheckableItem {
private String str;
private boolean isSelected;
public CheckableItem(String str) {
this.str = str;
isSelected = false;
}
public void setSelected(boolean value) {
isSelected = value;
}
public boolean isSelected() {
return isSelected;
}
public String toString() {
return str;
}
}
class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public CheckListRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
setSelected(((CheckableItem) value).isSelected());
setFont(list.getFont());
setText(value.toString());
return this;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
CheckListPlot frame = new CheckListPlot();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
update your method of adding ItemListener on all check box as follows