JXTreeTable 和 JComboBox 单元格编辑器

发布于 2024-07-30 06:31:00 字数 210 浏览 4 评论 0原文

如何在 JXTreeTable? 你能给我一个有效的例子吗?

How can I use JComboBox as cell editor in JXTreeTable? Can you give me a working example please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-08-06 06:31:00
table.getColumn(0).setCellEditor(
    new DefaultCellEditor(new JComboBox(new Object[]{"A", "B", "C"})));
table.getColumn(0).setCellEditor(
    new DefaultCellEditor(new JComboBox(new Object[]{"A", "B", "C"})));
蓝眼泪 2024-08-06 06:31:00

除了 JXTreeTable 用于显示分层数据的列之外,您可以使用以下代码将 JComboBox 使用单元格编辑器用于所有列。

例如:

    TableColumnExt column = this.tree.getColumnExt(1);
    column.setCellEditor(new DefaultCellEditor(new JComboBox());

即您不能将 JComboBox 用于 JXTreeTable 用于显示分层数据的列。

JXTreeTable java doc 中他们提到了这一点,

JXTreeTable 是一个专用表,由用于显示分层数据的单列和任意数量的用于显示常规数据的其他列组成。

JXTreeTable内部创建了TreeTableCellEditor,用于显示分层数据。

TreeTableModel提供方法 getHierarchicalColumn() 其中您可以指定使用哪个列来显示分层数据

现在,如果您想在第一列提供 JComboBox (第 0 个位置列默认为分层列),那么您有使用 TreeTableModel 提供不同的列索引,否则您必须移动您的列(我这边推荐)。

为分层数据提供不同列的代码

this.tree = new JXTreeTable(new DefaultTreeTableModel() {
    public int getHierarchicalColumn() {
       return 2;
    }
 });

预览
输入图片此处描述

注意:我提供了此解决方案,以便您可以使用最少的自定义代码来实现您的解决方案。 可以有另一种方法,但我个人认为这个解决方案很容易实现

You can use JComboBox using cell editor for all column using below code except a column which JXTreeTable uses to display hierarchical data.

Ex:

    TableColumnExt column = this.tree.getColumnExt(1);
    column.setCellEditor(new DefaultCellEditor(new JComboBox());

i.e. you can not use JComboBox for a column which JXTreeTable uses to display hierarchical data.

In JXTreeTable java doc they have mention that,

JXTreeTable is a specialized table consisting of a single column in which to display hierarchical data, and any number of other columns in which to display regular data.

JXTreeTable creates TreeTableCellEditor internally which is used to display hierarchical data.

TreeTableModel provides method getHierarchicalColumn() in which you can specify which colum uses to display hierarchical data

Now if you want to provide JComboBox at first column(0th position column is by default hierarchical column) then you have to provide different column index using TreeTableModel otherwise you have to shift your column(recommended from my side).

Code to provide different column for hierarchical data

this.tree = new JXTreeTable(new DefaultTreeTableModel() {
    public int getHierarchicalColumn() {
       return 2;
    }
 });

Preview
enter image description here

NOTE: I have provided this solution such that you can achieve your solution with minimum customization code. There can be another way but I personally find this solution very easy to implement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文