当JComboBox用作表编辑器时,动态填充它

时间:2014-11-05 16:08:02

标签: java swing jtable

我有一个用例(在Java Swing中),其中必须通过JTable编辑器编辑某个JComboBox列,但可选择的值列表取决于该行的域对象(我正在使用自定义TableModel)。

CellEditor界面有停止或取消编辑时要调用的事件,但没有开始编辑的事件(我希望这是填充选择值列表的好地方)。

如何在单元格的编辑开始时填写组合框?

3 个答案:

答案 0 :(得分:1)

ComboBox中可用的项目是其模型的责任。自定义您的ComboBoxModel(特别是getElementAt(int)方法),以便查询相关的域对象。

您可能希望扩展DefaultComboBoxModel,而不是从头开始实现接口。

答案 1 :(得分:1)

这是一个问题,我今年早些时候必须解决(下面的代码来自该项目)。解决方案是为组合框创建一个专用模型:

mEntityListModel = getMyEntityModel();

T[] items = (T[]) Array.newInstance(type, mEntityListModel.getSize());

for (int i = 0; i < items.length; i++) {
    items[i] = mEntityListModel.getElementAt(i);
}
mComboBoxModel = new DefaultComboBoxModel<T>(items);    

并使用组合框模型均衡实体列表中的每个更改:

mEntityListModel.addListDataListener(new DataListener());// make a better MVC solution if you like

private class DataListener implements ListDataListener {

    @Override
    public void intervalAdded(ListDataEvent e) {
        mComboBoxModel.insertElementAt(mEntityListModel.getElementAt(e.getIndex0()), e.getIndex0());
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
        Object deletedElement = mComboBoxModel.getElementAt(e.getIndex0());

        // Clear the combobox selection, if the item is deleted!
        if (mSelectionManager.isSelected(deletedElement)) {
            mSelectionManager.setSelectedItem(null);
        }
        mComboBoxModel.removeElementAt(e.getIndex0());
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
        intervalRemoved(e);
        intervalAdded(e);
    }

}

旁注:这就是表格组合框中的变化的注册方式:

tablecolumn.getCellEditor()
           .addCellEditorListener(new CellChangeListener());


private class CellChangeListener implements CellEditorListener {

    @Override
    public void editingStopped(ChangeEvent e) {
        DefaultCellEditor editor;
        Object newValue;
        if (e.getSource() instanceof DefaultCellEditor) {
            editor = (DefaultCellEditor) e.getSource();

            newValue = editor.getCellEditorValue();

            if (newValue != null) {
                //set the newValue for the combobox selection manager
            }
        }
    }

    @Override
    public void editingCanceled(ChangeEvent e) {
    }
}

如您所见,组合框项目列表在编辑器开始工作之前更新。编辑不负责这项工作。

答案 2 :(得分:1)

一种方法是覆盖JTable的getCellEditor(...)方法:

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

public class TableComboBoxByRow extends JPanel
{
    List<String[]> editorData = new ArrayList<String[]>(3);

    public TableComboBoxByRow()
    {
        setLayout( new BorderLayout() );

        // Create the editorData to be used for each row

        editorData.add( new String[]{ "Red", "Blue", "Green" } );
        editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
        editorData.add( new String[]{ "Apple", "Orange", "Banana" } );

        //  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)
                {
                    JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
                    return new DefaultCellEditor( comboBox1 );
                }
                else
                    return super.getCellEditor(row, column);
            }
        };

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

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table Combo Box by Row");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableComboBoxByRow() );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}