将 JComboBox 设置为 table 列不起作用

Setting JComboBox as table column doesn't work

立即使用 MCVE 进行编辑:

我想将一个 JTable 单元格视为一个 JComboBox。我在 Oracle 教程中尝试过它,但它不起作用。本教程没有说明您必须如何更改 table 模型才能使其正常工作。所以这是代码:

表格模型:

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

public class TableModel extends AbstractTableModel {

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return 2;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return 2;
}

@Override
public Object getValueAt(int row, int col) {
    if (row == 0) {
        return 2;
    } else if (col == 1 ) {
        return 1;
    } else {
        return null;
    }
}

}

以及显示组件的视图:

import java.awt.BorderLayout;

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

public class View extends JFrame {
public View() {
    super();
    TableModel tableModel = new TableModel();
    JTable testTable = new JTable(tableModel);

    JScrollPane scrollpane = new JScrollPane(testTable);
    // scrollpane.setOpaque(false);
    // scrollpane.getViewport().setOpaque(false);
    JPanel testTablePanel = new JPanel();
    testTablePanel.setBorder(
            BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
    testTablePanel.setLayout(new BorderLayout());
    testTablePanel.add(scrollpane);

    TableColumn sportColumn = testTable.getColumnModel().getColumn(1);

    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Snowboarding");
    comboBox.addItem("Rowing");
    comboBox.addItem("Chasing toddlers");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Teaching high school");
    comboBox.addItem("None");
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

    add(scrollpane);

    pack();
    setVisible(true);
}
public static void main(String[] args) {
    new View();
}

}

我必须在 table 模型中更改什么才能使其正常工作?

在您的示例中,您没有将第 1 列设置为可编辑。你首先需要这样做,比如在你的模型中:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return columnIndex == 1;
}

还有你的模型没有很好的处理数据,实际上没有办法获取到数据,而且实际上是固定的,所以编辑器可能没有效果。您需要覆盖 setValueAt 并让它更新模型的核心。

例如使用您的简单示例:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class View extends JFrame {
    public View() {
        super();
        TableModel tableModel = new TableModel();
        JTable testTable = new JTable(tableModel);

        JScrollPane scrollpane = new JScrollPane(testTable);
        // scrollpane.setOpaque(false);
        // scrollpane.getViewport().setOpaque(false);
        JPanel testTablePanel = new JPanel();
        testTablePanel.setBorder(BorderFactory
                .createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
        testTablePanel.setLayout(new BorderLayout());
        testTablePanel.add(scrollpane);

        TableColumn sportColumn = testTable.getColumnModel().getColumn(1);

        JComboBox<String> comboBox = new JComboBox<>();
        comboBox.addItem("Snowboarding");
        comboBox.addItem("Rowing");
        comboBox.addItem("Chasing toddlers");
        comboBox.addItem("Speed reading");
        comboBox.addItem("Teaching high school");
        comboBox.addItem("None");
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(scrollpane);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new View();
    }

}

class TableModel extends AbstractTableModel {
    Object[][] innerModel = new Object[][]{{2, 1}, {2, null}};

    public TableModel() {

    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public int getRowCount() {
        return innerModel.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return innerModel[row][col];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        innerModel[rowIndex][columnIndex] = aValue;
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 1;
    }

}