2

我正在尝试将 JComboBox 放置在 JTable 的某个列中。我有这段代码,它正在工作:

    model = new DefaultTableModel();
    JComboBox<String> optionComboCell = new JComboBox<String>();
    optionComboCell.addItem("Option 1");
    optionComboCell.addItem("Option 2");
    optionComboCell.setSelectedIndex(1);


    table = new JTable(model);
    // Adding here all the columns, removed for clarity
    model.addColumn("Options");
    TableColumn optionsColumn = table.getColumn("Options");
    optionsColumn.setCellEditor(new DefaultCellEditor(optionComboCell));

我的问题是,在选择该列中的单元格之前,它不会显示为 JComboBox。当 JFrame 被加载时,整个表格看起来是一样的,好像所有的单元格都只有文本。单击时,它会显示组合框的箭头和选项,但再次取消选择时,它看起来像一个常规单元格。

有什么办法可以解决吗?

4

3 回答 3

4

是的,使用 JComboBox 来呈现您的单元格:

import java.awt.Component;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Test4 {

    private static class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {

        public ComboBoxCellRenderer(int column) {
            for (int i = 0; i < 10; i++) {
                addItem("Cell (" + i + "," + column + ")");
            }
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setSelectedItem(value);
            return this;
        }
    }

    protected void initUI() {
        JFrame frame = new JFrame("test");
        frame.add(getTable());
        frame.pack();
        frame.setVisible(true);
    }

    private Component getTable() {
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 10; i++) {
            Vector<String> row = new Vector<String>();
            for (int j = 0; j < 3; j++) {
                row.add("Cell (" + i + "," + j + ")");
            }
            data.add(row);
        }
        Vector<String> columns = new Vector<String>();
        columns.add("Column 1");
        columns.add("Column 2");
        columns.add("Column 3");
        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        table.setRowHeight(20);
        int i = 0;
        Enumeration<TableColumn> c = table.getColumnModel().getColumns();
        while (c.hasMoreElements()) {
            TableColumn column = c.nextElement();
            column.setCellRenderer(new ComboBoxCellRenderer(i));
            i++;
        }
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scroll;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test4().initUI();
            }
        });
    }
}
于 2012-06-27T13:11:45.487 回答
4

您将需要定义自己的渲染器来在表格上显示组件,因为只有在编辑表格单元格中的值时才需要 CellEditor(这就是为什么它只会在您单击单元格时做出反应)。

也许看看Java Tutorials来了解更多关于 JTables 的渲染器和编辑器的概念。

于 2012-06-27T13:14:48.623 回答
-1

尝试设置单元格渲染器。

于 2012-06-27T13:11:31.873 回答