我正在 JavaGUI 中制作一個程式,我將從資料庫中獲取一些資訊,在 JTable 中我可以更改值并更新資料庫。但是在資料庫的一列中,我希望出現一個組合框,其中包含用戶可以選擇的所有選項。運行程式時,會出現查詢,我可以對其進行編輯,但在我想要組合框的列中什么也沒有出現
PreparedStatement pst = con.prepareStatement(m);
rp = pst.executeQuery();
DefaultTableCellRenderer direita = new DefaultTableCellRenderer();
JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem("Asia");
comboBox.addItem("Europe");
comboBox.addItem("North America");
comboBox.addItem("South America");
comboBox.addItem("Africa");
comboBox.addItem("Antartica");
comboBox.addItem("Australia");
TableColumn testColumn = table.getColumnModel().getColumn(2);
testColumn.setCellEditor(new DefaultCellEditor(comboBox));
direita.setHorizontalAlignment(SwingConstants.RIGHT);
table.setModel(DbUtils.resultSetToTableModel(rp));
table.getColumnModel().getColumn(2).setCellRenderer((TableCellRenderer) comboBox);
uj5u.com熱心網友回復:
TableColumn testColumn = table.getColumnModel().getColumn(2);
testColumn.setCellEditor(new DefaultCellEditor(comboBox));
table.setModel(DbUtils.resultSetToTableModel(rp));
重置模型時會創建一個新模型,TableColumnModel因此您將失去對編輯器和渲染器的自定義。
模型設定后需要設定編輯器:
table.setModel(DbUtils.resultSetToTableModel(rp));
TableColumn testColumn = table.getColumnModel().getColumn(2);
testColumn.setCellEditor(new DefaultCellEditor(comboBox));
您的渲染器設定正確。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523034.html
標籤:爪哇摇摆
