1

I want to created a Tree with multi-columns. I found this tutorial here (German) and this answer (English). I want to add checkboxes in one column, but I have no idea how to do it. When I return a checkbox to JTreeTable, something show in execute is checkbox detail not checkbox object. How can I get something like this, pictured below?

like that

Community
  • 1
  • 1
Ives
  • 505
  • 1
  • 4
  • 14
  • Isnt `JCheckBox` is displayed as default for `boolean` data of a `JTable` cell? – Prashant Apr 07 '16 at 09:32
  • 1
    For [example](http://stackoverflow.com/questions/21924846/checkbox-within-a-jxtreetable/21924930#21924930), [example](http://stackoverflow.com/questions/31375773/how-do-you-make-components-of-jpanel-as-a-node-in-jtree-usable/31376208#31376208) and personally, I'd use `JXTreeTable` from SwingX, it's simply a better implementation which will give you the control you will need to make this work – MadProgrammer Apr 07 '16 at 09:38
  • thanks,i dont want let the checkboxes inside Jtrees, i want to do is in the column besides trees. – Ives Apr 07 '16 at 10:34

1 Answers1

2

As shown in Taking the New Swing Tree Table for a Spin, cited here, your implementation of RowModel must return the correct type from getColumnClass() and the correct value from getValueFor(). Values of type Boolean.class will be rendered with a JCheckBox. The following implementations produce the image cited:

image

@Override
public Class getColumnClass(int column) {
    switch (column) {
        case 0:
            return Date.class;
        case 1:
            return Long.class;
        case 2:
            return Boolean.class;
        case 3:
            return Boolean.class;
        case 4:
            return Boolean.class;
        default:
            assert false;
    }
    return null;
}

@Override
public Object getValueFor(Object node, int column) {
    File f = (File) node;
    switch (column) {
        case 0:
            return new Date(f.lastModified());
        case 1:
            return f.length();
        case 2:
            return f.canRead();
        case 3:
            return f.canWrite();
        case 4:
            return f.canExecute();
        default:
            assert false;
    }
    return null;
}

I can't select checkbox.

You need to return true in your implementation of isCellEditable() for the desired column(s) and update the node in your implementation of setValueFor() accordingly. When the cell editor concludes, your implementation of setValueFor() will be called, so verify that it updates the same value that will later be returned by getValueFor(). Optionally, you'll want to implement the TreeModel methods that manage the TreeModelListener list by using the scheme prescribed in an EventListenerList API; the DefaultTreeModel source code is a good example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thank you,i add checkbox into table successful, it seems i dont need to create new JCheckbox objects,it use default JCheckbox,can i use my customize checkbox? – Ives Apr 08 '16 at 01:27
  • and i cant select checkbox – Ives Apr 08 '16 at 02:27
  • The example cited uses `File`, instances of which are immutable; I've elaborated above on the general approach to handling mutable data. – trashgod Apr 08 '16 at 22:18
  • can I use my custom checkbox? default is JCheckbox – Ives Apr 11 '16 at 10:50
  • Yes; use `setDefaultEditor()`; ping me here if you pose a new question on this topic. – trashgod Apr 11 '16 at 11:05
  • I return the custom Checkbox in `getTableCellEditorComponent` and `getTableCellRendererComponent` but it didn` t work – Ives Apr 13 '16 at 09:02
  • I'm not sure what didn't work; I see the expected result using `DefaultCellEditor` with `JCheckBox`; if you're still having trouble; please post a new question with a [mcve] that shows your approach. – trashgod Apr 13 '16 at 09:59