aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTreeTable and custom renderers/editors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTreeTable and custom renderers/editors" Watch "JTreeTable and custom renderers/editors" New topic
Author

JTreeTable and custom renderers/editors

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I am busy with a JTreeTable and now that I have the table working I need to put indiviual cell editors/renderers on column[1] (ie, not the tree column but the next column.)

The tree represents news items that people can register for. But the user can only select leaves, not the branches of the tree. So for this, what I would like to achieve is that if the node in column 0 is a leaf, then show a check box in column 1 and if not, then show nothing or a black text field.

Now I'm not sure if I must use renderers or editors and I'm not sure where to put the code to link the cell to it's renderer/editor as I have gotten a little lost in my code.

Please could someone tell me if I must use renderers or editors and in which part of the code I need to link them up.

Many kind regards,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Now you're really asking for trouble !

I *think* this will work by just setting your getColumnClass() method to return a Boolean for column 1. So your getColumnClass will be something like :



JTable has default renderers & editors for booleans which it will add if you do this.

D.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Don!

It's like you're watching me or something...

I could get everything to work fine for when I just wanted the boolean checkboxes things. The problem came in when I decided to get adventurous with the selection of my nodes.

My user objects are called NewsNodes. And every NewsNode has the isSelectable property. Now if (myNewsNode.isSelectable()) then show the checkbox but if (!mynewsNode.isSelectable()) then show a blank cell (like a blank jtextfield.

So I fiddled and got the thing to have blanks and now the true false string in the cell. So it can differentiate with if the cell should be showing something. The problem is that it's not in a textbox and not in a checkbox.

Here's the code...


The "hello" was to check that it wasn't getting there. (Which it's not).

And TableRowEditorModel was a piece of code that I found on the net that uses a hashtable to hols the different editors that are required for the different rows. (But the acutall editing of the cells isn't working either, but I'll get to that later when the check boxes work.

I was thinking that this is a total mission to do, but then I remembered that we decided to go this way instead of making a JEditorPane be able to interpret VBScript and JavaScript. Hopefully this is still simpler than that. (And it will probably make me cry if you tell me otherwise!! )

Thank you again.
Take care,
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Brief update...

I got the renderer and editor to know what to do with each cell in the column.

So there are now blank lables where there should be, but there are still the strings of true and false where there should be checkboxes. But when I click on the true or false string, it flashes the checkbox. Almost as if it's only a checkbox when you are editing.

So I'm going to look at the renderer for the cell, since it looks like the editing part is now working.

(I chose to put up an update on my progress so that we're still on the same page.)

Cheers,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Yep, it sounds like the renderer is not working.

Are you adding your own custom renderers ? Cause I would have thought that the default ones would work if getColumnClass() is defined correctly..?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
The problem is that there will be two different types of class in that column (ie Boolean and somethingElse). Where somethingElse is a string or whatever I can use so that the renderer will show a blank cell and not the checkbox.

So I changed the columnClass on the column to be of Object.class instead of Boolean.class. When it was Boolean.class it showed checkboxes in every cell.

Then I wrote custom editors and renderers. And the editor works and the renderer not.

After testing a lot I think that what is happenening is that when I assign the rowRenderer that is stuffs up the treeCellRenderer for some reason as the tree stops working when I use the renderer. I'm using this code to set the editors and the commented out lines were setting the renderers (and messing the tree up.)



So that's where I'm at.

Thanks again
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
There are lots of ways to do this, but I would use a tri-state. Something like :



Then your getColumnClass becomes :


Then make your getValueAt return the correct Selectable when column == 1.
Then write a SelectableCellRenderer that renders a Selectable as either a JCheckBox or empty JLabel, depending on the value.
Then set this renderer to be the default renderer for Selectables.

make sense ?
D.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
OK, that actually sounds simple enough to make sense! I'll give it a bash and see how I go.

Thanks again,
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I've implemented a cell editor for my SelectableComponent class over the weekend but I have obviously missed something as it appears that the setValue is not holding, the value seems to revert to the old value when I move off the cell.

Please could you point me in the right direction.

Many kind regards,
Rachel

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
your getCellEditorValue doesn't look right to me....
I would expect something like :



D.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Don

That was it, the missing link! So the saga of the TreeTable can be laid to rest!

Many kind regards and many many thanks for helping me out.
Take care,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Originally posted by Rachel Swailes:
Hi Don

That was it, the missing link! So the saga of the TreeTable can be laid to rest!



Till next time anyway...


Many kind regards and many many thanks for helping me out.
Take care,
Rachel

No worries
D.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTreeTable and custom renderers/editors