views:

127

answers:

1

What I'm trying to do is create a JTree with each cell containing normal text but also containing a right-aligned text for each cell (right-aligned to the edge of the tree, regardless of the hierarchy level).

I've tried creating my own TreeCellRenderer, but the cells' sizes are not being updated. I also tried this idea with a custom tree UI but am experiencing similar issues.

I also have tried creating a custom component with a JPanel "glued" to the right of the tree. This has been the most successful, but I have been unable to have the tree cells extend to the right and touch this extra panel:

alt text

+1  A: 

You could try to use a JXTreeTable, from the SwingX package.

jxtreetable example

You would define then such an object with two columns (one for the tree, to other for the right aligned text), and it should work out fine.

To prevent it from looking like a table, though, I would recommend you to deactivate the header (setTableHeader(null)), and use their "packing" methods, to have the columns on optimal size.

Gnoupi
interesting. thanks! ... but since I have a pretty customized JTree already, I'd hate to have to start from square 1 with a component that is basically a JTable...
Epaga
@Epaga - Note that the JXTreeTable is actually a table, with a tree as renderer of the first column (plus a lot of other tweaks to make this work). The point is that this is very similar, you can even set the tree cell renderer you used already, for this first column. Data is managed like a tree, with basically treenodes which include a "getValueAt", so it wouldn't change your data model a lot, normally.
Gnoupi