立即编辑MCVE:
我想将一个JTable单元视为JComboBox。我尝试使用Oracle Tutorial,它不起作用。本教程没有说明如何更改表模型以使其正常工作。所以这是代码:
的TableModel:
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();
}
}
我需要在tablemodel中更改什么才能使其正常工作?
答案 0 :(得分:1)
在您的示例中,您不能使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;
}
}