我有這個小代碼片段,顯示一個簡單的面板與複選框列表。行爲必須是以下:JList需要兩次點擊來更新一個CheckableItem
如果複選框「】所有」被選擇每隔一個複選框必須是未選中,否則(「[]所有」未選中複選框時),用戶可以選擇任何數量的複選框。
如果從「[]所有」被選中,然後不同的一個複選框,在「[]所有」複選框必須取消選中
遠從現在沒什麼奇怪,只是如果勾選「[]所有」被選中,那麼用戶必須按兩次複選框才能將其標記爲已選。第一次點擊取消選中「[]全部」複選框,但不檢查選中的複選框。我想要將兩種行爲結合在一起。我猜錯過了什麼(也許同步事件...)
下面的代碼(包含自我,所以如果你想你可以運行它,因爲它是):
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);
}
}
閱讀[that](http://stackoverflow.com/a/17952140/2894369)回答,它對你的問題有建議。 – alex2410
感謝您的回覆;是一個很好的選擇。 – microvo