JComboBox在多个表格单元格之间共享,自动选择当前选定的项目 [英] JComboBox shared between multiple table cells automatically selecting currently selected item

查看:152
本文介绍了JComboBox在多个表格单元格之间共享,自动选择当前选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下 camickr 提供的代码作为单元格编辑器添加了一个组合框作为参考:

I have added a combobox as a cell editor using the code provided by camickr below as reference:

如何添加唯一JComboBoxes到JTable(Java)中的一列

除了我的情况,我只需要一个组合框用于列中的所有单元格。我遇到的问题是组合框自动选择最后选择的项目(或当前选择的项目,不确定),并且由于不同的行共享相同的组合框,如果您单击其中一个单元格,它将自动更改到最后选择的项目。

Except in my case, I only need one combobox to be used by all of the cells within a column. The problem I'm running into is that the combobox automatically selects the last selected item (or current selected item, not sure), and since different rows share the same combobox, if you click on one of the cells, it'll automatically change to the last selected item.

作为快速演示,我只是修改了上面的代码来显示问题。我希望组合框自动选择列表中的项目等于在选定单元格中设置的项目(而不是选择单元格,然后让该单元格的内容自动更改)

As a quick demonstration I simply modified the code from above to show the issue. I would like the combo box to automatically select an item on the list equal to the item that is set in selected cell (as opposed to selecting a cell, and then having the contents of that cell automatically change)

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class Test extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public Test()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(0);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        Test frame = new Test();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
} 


推荐答案

按默认选择组合框的第一项,在您的示例中为红色。

By default the first item of a combo box is selected, which in your example is "Red".

编辑单元格时,在comboBox中选择TableModel中的值编辑。由于表中的数据与comboBox中的任何条目都不匹配,因此选择不会更改,因此Red将显示为编辑器中的值。

When you edit a cell the value from the TableModel is selected in the comboBox editor. Since the data in your table does not match any of the entries in the comboBox, the selection doesn't change so "Red" is displayed as the value in the editor.

当您从编辑器中进行选择时,该值将保存在模型中,并在下次编辑单元格时正确显示。

When you make a selection from the editor that value is then saved in the model and will be displayed properly the next time you edit the cell.

问题的解决方案是为了确保TableModel在创建时包含有效数据。只有这样才能选择comboBox中的正确项目。

The solution to your problem is to make sure the TableModel contains valid data when it is created. Only that way can the proper item in the comboBox be selected.


我想我必须将一个动作监听器附加到组合框以确定选择哪个项目

I figured I would have to attach an action listener to the combobox to determine which item to selected

不,当它用作编辑器时,您不会在组合框上使用侦听器。 comboBox编辑器会自动为您选择项目。

No, you don't play with listeners on a comboBox when it is used as an editor. The comboBox editor does the selection of the item automatically for you.

这篇关于JComboBox在多个表格单元格之间共享,自动选择当前选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆