如何连续使用多种编辑器类型制作JTable?

时间:2016-04-29 09:00:51

标签: java swing jtable jcombobox

我正在渲染一个JTable,其中某些单元格是普通文本输入或ComboBox。

当我这样尝试时:

package sandbox.jtablegui;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.*;
import javax.swing.table.*;

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

    public App()
    {
        String[] colNames = { "Name", "Value" };
        final String[] propertyNames = { "Val1", "Val2" };
        DefaultTableModel model = new DefaultTableModel(colNames, 2) {

            // set setting names
            public Object getValueAt(int row, int col) {
                if (col == 0)
                    return propertyNames[row];
                return super.getValueAt(row, col);
            }

            public boolean isCellEditable(int row, int col) {
                if (col == 0)
                    return false;
                return true;
            }
        };

        JComboBox<String> comboBox = new JComboBox<String>(new String[] { "Option1", "Option2" });
        final DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox)    {
            @Override
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                Object val = value;
                System.out.println(val);
                if (val == null) {
                    val = ((JComboBox<?>) getComponent()).getItemAt(0);
                    System.out.println(val);
                }
                return super.getTableCellEditorComponent(table, val, isSelected, row, column);
            }
        };

        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 == 1)
                    return comboBoxEditor;
                else
                    return super.getCellEditor(row, column);
            }
        };
        RowEditorModel rowEditorModel = new RowEditorModel();



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

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

    public class RowEditorModel {
        private Hashtable data;

        public RowEditorModel() {
            data = new Hashtable();
        }

        public void addEditorForRow(int row, TableCellEditor editor) {
            data.put(new Integer(row), editor);
        }

        public void removeEditorForRow(int row) {
            data.remove(new Integer(row));
        }

        public TableCellEditor getEditor(int row) {
            return (TableCellEditor) data.get(new Integer(row));
        }
    }
}

但是,从屏幕截图中可以看出,JTable中没有显示默认值。

enter image description here

1 个答案:

答案 0 :(得分:2)

在组合框创建上设置所选索引无济于事。每次用作编辑器时都需要设置它。像这样:

DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox) {
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        Object val = value;
        if (val == null) {
            val = ((JComboBox<?>) getComponent()).getItemAt(0);
        }
        return super.getTableCellEditorComponent(table, val, isSelected, row, column);
    }

}