File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JCheckBox in JXTreeTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCheckBox in JXTreeTable" Watch "JCheckBox in JXTreeTable" New topic
Author

JCheckBox in JXTreeTable

M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Hi,

I have written a simplistic program where I wanted to show a JCheckBox in an JXTreeTable, but it does not work. Can you give me an hint what I could try?

Thank you

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

If the column 'class' is boolean, the JTable will show the column as a checkbox. Same applies for JXTreeTable. You need to override the getColumnClass of the TreeTableModel to return the Boolean.class for the column that you want to show as checkbox.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
This is my current try but the field just stays empty, no matter if i use line 57 or not:



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You're using the first boolean value to set the checked state, but that indicates the selected state of the cell - in other words, whether or not it's selected in the tree. That's different from whether or not the check box should be checked. That's where the value comes in. Try replacing line 69 with this:
Although I must admit I am surprised that JXTreeTable doesn't have support for Boolean.class out-of-the-box like JTable has. With JTable, all you need to do is let your TableModel specify the column class is Boolean.class (like you're doing) and it will display a check box automatically (unless you override this behaviour).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Rob Spoor wrote: Although I must admit I am surprised that JXTreeTable doesn't have support for Boolean.class out-of-the-box like JTable has. With JTable, all you need to do is let your TableModel specify the column class is Boolean.class (like you're doing) and it will display a check box automatically (unless you override this behaviour).


No, it does. It works fine for me and I am not using a TableCellRenderer. But, I have the second column as a boolean and the first column is a String.
I am beginning to think if Bryan has the first column itself as a boolean.
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Rob Spoor wrote:You're using the first boolean value to set the checked state, but that indicates the selected state of the cell - in other words, whether or not it's selected in the tree. That's different from whether or not the check box should be checked. That's where the value comes in. Try replacing line 69 with this:
Although I must admit I am surprised that JXTreeTable doesn't have support for Boolean.class out-of-the-box like JTable has. With JTable, all you need to do is let your TableModel specify the column class is Boolean.class (like you're doing) and it will display a check box automatically (unless you override this behaviour).


Unfortunately that didn't do it. If JXTreeTable has an own implementation of what I need I'm sure that one's better than mine. How do I use it?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

M Bryan wrote: If JXTreeTable has an own implementation of what I need I'm sure that one's better than mine. How do I use it?

There is no special implementation. It is same as the JTable where you get a checkbox by overriding the getColumnClass which you have already done. Remove the TableCellRenderer altogether and make sure you are returning a boolean value in the getValueAt method for that column.

Note: However, this does not work for the first column and works from second column onwards.
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Okay, I figured it out how to get the treetable to show checkboxes in a column. It now has checkboxes in every line, but I think I can solve that problem.
But there is still a problem: If I click on a checkbox, its visible state does not change. I only can see a change if I move the cursor away from that specific checkbox. Why?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

I tried with just overriding getColumnClass and without using a renderer and it is working fine for me. You need to implement the getValueAt and setValueAt correctly. However, if you are still using a renderer, then I am not sure...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCheckBox in JXTreeTable
 
Similar Threads
JTable
JCheckBox and JTable
JTable
calculate sum in jtable
Images in a JTable