2013-12-09 64 views
0

我有這個小代碼片段,顯示一個簡單的面板與複選框列表。行爲必須是以下: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); 
} 
} 
+0

閱讀[that](http://stackoverflow.com/a/17952140/2894369)回答,它對你的問題有建議。 – alex2410

+0

感謝您的回覆;是一個很好的選擇。 – microvo

回答

0

更新您添加的ItemListener的方法在所有複選框如下

allCheck.addItemListener(new ItemListener() { 
    public void itemStateChanged(ItemEvent e) { 
     for(int i =0 ; i< list.getModel().getSize(); i++){ 
      if(e.getStateChange() == ItemEvent.SELECTED) 
      { 
      System.out.println("value"+e.getStateChange()); 
      ((CheckableItem)list.getModel().getElementAt(i)).setSelected(true); 
      } else 
      { 
       ((CheckableItem)list.getModel().getElementAt(i)).setSelected(false); 
      } 
      Rectangle rect = list.getCellBounds(i, i); 
      list.repaint(rect); 
     } 
    } 
}); 
+0

感謝您的回覆,但是當我按「[]全部」時,您的代碼會標記所有其他複選框。這不是您可以在上面閱讀的預期行爲 – microvo

0

好吧,我解決,但我不知道爲什麼......我寫在這裏,希望有人可以給我解釋一下這個奇怪的問題的解決方案。我會很高興地明白爲什麼這個小小的變化解決了一切。

基本上它是sufficent改變這個代碼片段小:

list.addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e) { 
        // I have made the operation on the JCheckBox first of all 
      if(allCheck.isSelected()){ 
       allCheck.setSelected(false); 
      } 


      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); 




     } 
    }); 

我認爲這是由於JCheckBox類的和JList類的之間的一些同步問題,不是嗎?看起來JCheckBox上的操作(在前面的代碼中)在某種程度上會導致JList失去焦點,因此第二次單擊會重置JList焦點

相關問題