如何在 JTable 单元格中使用 JList?

发布于 2024-10-18 12:05:47 字数 582 浏览 5 评论 0原文

我想要一种简单的方法将 JList 放入 JTable 的列中。我已经有了 JLists 和表格,但是当放入表格中时,Jlists 显示为字符串,这是正常的,因为我使用 DefaultTableModel。我已将 getColumnClass() 重写为:

public Class<? extends Object> getColumnClass(int c)
{
    return getValueAt(0, c).getClass();
}

但这只是格式化整数和浮点值。

我认为 setValueAt() 和 getValueAt() 也应该被重写,以便在调用 JList.getSelectedValues() 时返回字符串数组em>,但我不知道怎么做。
我还希望单元格可编辑,以便用户可以从 JList 中选择一个或多个选项。编辑一行后,我使用“保存”按钮将更改保存在数据库中,因此我认为我不需要 ListSelectionListener,JList.getSelectedValues() 工作得很好。

我知道这是一个常见问题,但我在这里找不到答案。如果这是重复的,请告诉我,我将删除它。

I would like a simple way to put a JList in a column of a JTable. I already have the JLists and the table, but when put in the table, the Jlists are displayed as Strings, which is normal, because I use DefaultTableModel. I have overriden the getColumnClass() as:

public Class<? extends Object> getColumnClass(int c)
{
    return getValueAt(0, c).getClass();
}

but this just formats the integer and float values.

I suppose that setValueAt() and getValueAt() should also be overriden, in order to return am array of Strings when I call JList.getSelectedValues(), but I can't figure out how.
I also want the cells to be editable, so the users can choose one or more option from the JList. After editing a row, I use a Save button to save the changes in a database, so I don't think I need a ListSelectionListener, JList.getSelectedValues() works just fine.

I know this is a common question, but I couldn't find an answer here. If this is a duplicate, please let me know and I will delete it.

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

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

发布评论

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

评论(1

寄人书 2024-10-25 12:05:47

我已经做到了。对于需要同样东西的每个人,这就是我所做的:

1)我创建了一个 JScrollTableRenderer,并设置了我需要显示 JList 的列以使用此渲染器

    table.getColumnModel().getColumn(5).setCellRenderer(new JScrollTableRenderer());

JScrollTableRenderer 类内容:

public class JScrollTableRenderer extends DefaultTableCellRenderer {

JScrollPane pane = new JScrollPane();

public JScrollTableRenderer()
{
    super();
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column)
{
    pane = (JScrollPane) value;
    return pane;
}
}

2)我创建了一个 JScrollTableEditor ,并设置我需要显示 JList 以使用此编辑器的列

    table.getColumnModel().getColumn(5).setCellEditor(new JScrollTableEditor());

JScrollTableEditor 类内容:

    public class JScrollTableEditor extends AbstractCellEditor implements TableCellEditor {
    JScrollPane component = new JScrollPane();
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
          int rowIndex, int vColIndex)
    {
        component = ((JScrollPane) value);
        return ((JScrollPane) value);
    }

    public Object getCellEditorValue()
    {
        return component;
    }

    }

3)我在 JTable 模型中添加了此方法:

            public Class<? extends Object> getColumnClass(int c)
            {
                if(c == 5) return JScrollPane.class;
                else return getValueAt(0, c).getClass();
            }

I've done it. For everyone who needs the same thing, here is what I've done:

1)I have created a JScrollTableRenderer, and set the column I needed to show the JList to use this renderer

    table.getColumnModel().getColumn(5).setCellRenderer(new JScrollTableRenderer());

The JScrollTableRenderer class content:

public class JScrollTableRenderer extends DefaultTableCellRenderer {

JScrollPane pane = new JScrollPane();

public JScrollTableRenderer()
{
    super();
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column)
{
    pane = (JScrollPane) value;
    return pane;
}
}

2)I have created a JScrollTableEditor, and set the column I needed to show the JList to use this editor

    table.getColumnModel().getColumn(5).setCellEditor(new JScrollTableEditor());

The JScrollTableEditor class content:

    public class JScrollTableEditor extends AbstractCellEditor implements TableCellEditor {
    JScrollPane component = new JScrollPane();
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
          int rowIndex, int vColIndex)
    {
        component = ((JScrollPane) value);
        return ((JScrollPane) value);
    }

    public Object getCellEditorValue()
    {
        return component;
    }

    }

3)I added this method in the JTable model:

            public Class<? extends Object> getColumnClass(int c)
            {
                if(c == 5) return JScrollPane.class;
                else return getValueAt(0, c).getClass();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文