在单元格中具有左对齐和右对齐的文本的JTree ?

时间:2022-02-28 12:53:55

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).

我要做的是创建一个JTree,其中每个单元格包含普通文本,但也包含每个单元格的右对齐文本(无论层次结构级别如何,右对齐到树的边缘)。

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.

我尝试过创建自己的TreeCellRenderer,但是单元格的大小没有被更新。我也用自定义树UI尝试了这个想法,但也遇到了类似的问题。

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:

我还尝试过创建一个自定义组件,将JPanel“粘合”在树的右边。这是最成功的,但我一直无法让树细胞向右延伸并触摸这个额外的面板:

alt text http://img718.imageshack.us/img718/3676/problem.png

alt文本http://img718.imageshack.us/img718/3676/problem.png

1 个解决方案

#1


1  

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

您可以尝试从SwingX包中使用JXTreeTable。

jxtreetable example http://blogs.sun.com/geertjan/resource/outline-tim-browser.png

jxtreetable例子http://blogs.sun.com/geertjan/resource/outline-tim-browser.png

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.

但是,为了防止它看起来像一个表,我建议您禁用header (setTableHeader(null)),并使用它们的“打包”方法,使列达到最佳大小。

#1


1  

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

您可以尝试从SwingX包中使用JXTreeTable。

jxtreetable example http://blogs.sun.com/geertjan/resource/outline-tim-browser.png

jxtreetable例子http://blogs.sun.com/geertjan/resource/outline-tim-browser.png

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.

但是,为了防止它看起来像一个表,我建议您禁用header (setTableHeader(null)),并使用它们的“打包”方法,使列达到最佳大小。