简体   繁体   English

JTable 中的 JComboBox 不保存选择

[英]JComboBox within JTable not saving selection

I have a JTable which contains a Name and Choice column.我有一个包含NameChoice列的 JTable。 I would like for the Choice column to contain a JComboBox Component that has the same selection for every row, although allow for independent selection for every unique row.我希望Choice列包含一个JComboBox组件,该组件对每一行都有相同的选择,尽管允许对每个唯一行进行独立选择。

Currently, interacting with the JComboBox within the column allows for the drop-down to appear for a selection to be made;目前,与列中的JComboBox交互允许出现下拉菜单以进行选择; although, no selection is saved.但是,没有保存任何选择。

在此处输入图像描述

Instead, the selection made migrates to any JComboBox that I click on.相反,所做的选择会迁移到我单击的任何JComboBox Eg, I click on the first row's JComboBox and select "Choice B", but all choices still appear as "Choice A".例如,我点击第一行的JComboBox和 select “Choice B”,但所有选项仍然显示为“Choice A”。 It isn't until I click on another row's JComboBox that the drop-down appears with the "Choice B" selection highlighted.直到我单击另一行的JComboBox时才会出现下拉菜单并突出显示“Choice B”选项。

The code used for this table is as follows:该表使用的代码如下:

final String[] choices = new String[]{"Choice A", "Choice B", "Choice C"};
final Collection<String> mockData = Arrays.asList("First", "Second", "Third", "Fourth", "Fifth", "Sixth");
table.setModel(new MasterTableModel(mockData.stream().map(s -> {
    return new Object[]{s, new JComboBox<>(choices)};
}).collect(Collectors.toSet())));
table.setDefaultEditor(JComboBox.class, new DefaultCellEditor(new JComboBox<>(choices)));
table.setDefaultRenderer(JComboBox.class, new MasterTableComboRenderer(table.getDefaultRenderer(JComboBox.class)));

I have three choices, which are used to initalize JComboBox es that populate the Choice column.我有三个选项,用于初始化填充Choice列的JComboBox I set the DefaultRenderer to my custom MasterTableComboRenderer object which implements the TableCellRenderer interface so as to have the JComboBox show up as a Component rather than to print the address of the object.我将DefaultRenderer设置为我的自定义MasterTableComboRenderer object,它实现了TableCellRenderer接口,以便让JComboBox显示为Component ,而不是打印 object 的地址。 It only requires the overriding of a single method:它只需要覆盖一个方法:

class MasterTableComboRenderer ...
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
    return value instanceof Component ? (Component) value : renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}

Finally, MasterTableModel is a simple extension of AbstractTableModel , which defines the column headers [Name, Choice] and holds an Object[][] reperesenting the JTable 's data.最后, MasterTableModelAbstractTableModel的一个简单扩展,它定义了列标题[Name, Choice]并保存了一个代表JTable数据的Object[][] I've overriden isCellEditable and getColumnClass as follows:我已经覆盖isCellEditablegetColumnClass如下:

class MasterTableModel...
@Override
public boolean isCellEditable(final int rowIndex, final int columnIndex) {
    return getColumnName(columnIndex).equals("Choice");
}

@Override
public Class<?> getColumnClass(final int columnIndex) {
    return getValueAt(0, columnIndex).getClass();
}

Is there something I've been missing to achieve the functionality of the JComboBox saving its choices and not having the selection highlight migrate to other boxes?为了实现JComboBox的功能,我是否缺少一些东西来保存其选择并且没有选择突出显示迁移到其他框?

A JComboBox should NOT be stored in the TableModel . JComboBox不应存储在TableModel中。 The String value is stored in the model. String值存储在 model 中。

Read the section from the Swing tutorial on Using a Combo Box as a Renderer for a working example.阅读 Swing 教程中有关使用组合框作为渲染器的部分以获取工作示例。

If you want the renderer to look like a combo box, then you need to use a combo box as a renderer.如果您希望渲染器看起来像一个组合框,那么您需要使用组合框作为渲染器。 Something like:就像是:

class ComboBoxRenderer extends JComboBox implements TableCellRenderer
{
    public ComboBoxRenderer()
    {
        setBorder(null);
    }

    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        removeAllItems();
        addItem( value );

        return this;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM