2012-05-29 23 views
3

您好我无法切换JList中的复选框,我希望当点击一个项目时勾选复选框,并且如果它被勾选再次我想它切换到unticked。我希望可以在不使用Ctrl或Shift键的情况下勾选或取消勾选多个项目。JCheckBox检查在JList中的切换逻辑的问题

public class CustCellRenderer extends JCheckBox 
implements ListCellRenderer 
{ 
    boolean selected = false; 

    void CustCellRenderer() 
    { 
     setOpaque(true); 
     setIconTextGap(12); 
    } 

// allows a custom list cell rendering which will enable me to display an icon as well as filename 
    @Override 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
    { 
     JCheckBox checkBox = (JCheckBox)value; 

     if (isSelected) 
     { 
      setBackground(list.getSelectionBackground()); 
      setForeground(list.getSelectionForeground()); 

      if (!selected) 
      { 
       selected = true; 
       setSelected(selected); 
      } 
      else 
      { 
       selected = false; 
       setSelected(selected); 
      } 
     } 
     else 
     { 
      setBackground(list.getBackground()); 
      setForeground(list.getForeground()); 

      setSelected(selected); 
     } 

     setText(checkBox.getText()); 

     return this; 
    } 
} 

这是我如何试图向表中添加数据,因为某些原因没有出现任何想法?

public void addDirectoryLabelsToList() 
{ 
    // clears all the previous labels from the listModel to ensure only labels 
    // that refelect the current directory are shown 
    for (int x = 0; x < tableModel.getRowCount(); x++) 
     tableModel.removeRow(x); 

    // iterate through the dirLabels and add them to the listModel 
    for (JCheckBox j : dirLabels) 
    { 
     Vector<Object> obj = new Vector<>(); 

     obj.add(Boolean.FALSE); 
     obj.add(j.getText()); 

     tableModel.addRow(obj); 
    } 
} 

回答

3

JList允许自定义渲染器,但您也需要自定义编辑器。作为替代方案,考虑具有Boolean.class列的JTable列,其图示为here

附录:我改变了我的问题,请检查

我不确定事情会出错,但我怀疑您的模型的getColumnClass()方法不会返回Boolean.class的相关列。您可以将您的实施与此相关的example进行比较,并发布sscce

+0

所以它不能正确使用的JList做些什么呢? –

+0

这一努力将是相当可观的。 – trashgod

+0

JTable似乎也会涉及很多工作:( –

4

如果我没有理解这个问题......

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class JListToggleLogicTest { 
    private final ClearSelectionListener listener = new ClearSelectionListener(); 
    public JComponent makeUI() { 
    JList<String> list = new JList<String>(makeModel()) { 
     @Override public void setSelectionInterval(int anchor, int lead) { 
     if(anchor==lead && lead>=0 && anchor>=0) { 
      if(listener.isDragging) { 
      addSelectionInterval(anchor, anchor); 
      } else if(!listener.isCellInsideDragging) { 
      if(isSelectedIndex(anchor)) { 
       removeSelectionInterval(anchor, anchor); 
      } else { 
       addSelectionInterval(anchor, anchor); 
      } 
      listener.isCellInsideDragging = true; 
      } 
     } else { 
      super.setSelectionInterval(anchor, lead); 
     } 
     } 
    }; 
    list.setCellRenderer(new CheckBoxCellRenderer()); 
    list.addMouseListener(listener); 
    list.addMouseMotionListener(listener); 
    JPanel p = new JPanel(new GridLayout(1,2)); 
    p.add(makeTitledPanel("Default", new JList<String>(makeModel()))); 
    p.add(makeTitledPanel("SelectionInterval", list)); 
    return p; 
    } 
    private static DefaultListModel<String> makeModel() { 
    DefaultListModel<String> model = new DefaultListModel<>(); 
    model.addElement("aaaaaaa"); 
    model.addElement("bbbbbbbbbbbbb"); 
    model.addElement("cccccccccc"); 
    model.addElement("ddddddddd"); 
    model.addElement("eeeeeeeeee"); 
    return model; 
    } 
    private static JComponent makeTitledPanel(String title, JComponent c) { 
    JPanel p = new JPanel(new BorderLayout()); 
    p.setBorder(BorderFactory.createTitledBorder(title)); 
    p.add(new JScrollPane(c)); 
    return p; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(new JListToggleLogicTest().makeUI()); 
    f.setSize(320, 240); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
} 

class ClearSelectionListener extends MouseAdapter { 
    private static void clearSelectionAndFocus(JList list) { 
    list.getSelectionModel().clearSelection(); 
    list.getSelectionModel().setAnchorSelectionIndex(-1); 
    list.getSelectionModel().setLeadSelectionIndex(-1); 
    } 
    private static boolean contains(JList list, Point pt) { 
    for(int i=0; i<list.getModel().getSize(); i++) { 
     Rectangle r = list.getCellBounds(i, i); 
     if(r.contains(pt)) return true; 
    } 
    return false; 
    } 
    private boolean startOutside = false; 
    private int  startIndex = -1; 
    public boolean isDragging = false; 
    public boolean isCellInsideDragging = false; 
    @Override public void mousePressed(MouseEvent e) { 
    JList list = (JList)e.getSource(); 
    startOutside = !contains(list, e.getPoint()); 
    startIndex = list.locationToIndex(e.getPoint()); 
    if(startOutside) { 
     clearSelectionAndFocus(list); 
    } 
    } 
    @Override public void mouseReleased(MouseEvent e) { 
    startOutside = false; 
    isDragging = false; 
    isCellInsideDragging = false; 
    startIndex = -1; 
    } 
    @Override public void mouseDragged(MouseEvent e) { 
    JList list = (JList)e.getSource(); 
    if(!isDragging && startIndex == list.locationToIndex(e.getPoint())) { 
     isCellInsideDragging = true; 
    } else { 
     isDragging = true; 
     isCellInsideDragging = false; 
    } 
    if(contains(list, e.getPoint())) { 
     startOutside = false; 
     isDragging = true; //add:2012-06-01 
    } else if(startOutside) { 
     clearSelectionAndFocus(list); 
    } 
    } 
} 
class CheckBoxCellRenderer extends JCheckBox implements ListCellRenderer<String> { 
    @Override public Component getListCellRendererComponent(
     JList<? extends String> list, String value, int index, 
     boolean isSelected, boolean cellHasFocus) { 
    setOpaque(true); 
    if(isSelected) { 
     setBackground(list.getSelectionBackground()); 
     setForeground(list.getSelectionForeground()); 
     setSelected(true); 
    }else{ 
     setBackground(list.getBackground()); 
     setForeground(list.getForeground()); 
     setSelected(false); 
    } 
    setText(value); 
    return this; 
    } 
} 
+1

+1并行比较和使用'MouseAdapter'。 – trashgod

+0

@trashgod:谢谢。 – aterai