JTree with checkboxes

后端 未结 4 2083
温柔的废话
温柔的废话 2020-12-05 19:58

I need to add checkboxes to a JTree. A custom TreeCellRenderer/TreeCellEditor seems like the right approach. So far I used the CheckBoxNodeRenderer approach in this webpage.

4条回答
  •  广开言路
    2020-12-05 20:41

    per @aperkins suggestion this is what I ended up doing in the TableCellRenderer, it seems to work well:

    final private JPanel nodeRenderer = new JPanel();
    final private JLabel label = new JLabel();
    final private JCheckBox check = new JCheckBox();
    
         ...
    
    // in constructor:
    final Insets inset0=new Insets(0,0,0,0);        
    this.check.setMargin(inset0);
    this.nodeRenderer.setLayout(new BorderLayout()); 
    this.nodeRenderer.add(this.check, BorderLayout.WEST);
    this.nodeRenderer.add(this.label, BorderLayout.CENTER);
    

    The keys for getting rid of unwanted space in the margins seems to be (a) calling JCheckBox.setMargin() to reduce the checkbox margin, and (b) using a BorderLayout for JPanel.

提交回复
热议问题