我在開始時沒有任何JCheckBox,在我的GUI中得到了(或多或少)空的JTable。按下按鈕後,我想根據數組的內容使用名稱(字符串)和JCheckBoxes來填充表格。具有名稱的部分已經正常工作,但我無法將單個單元格的類更改爲布爾值,而是在JCheckBox實際應該在的JTable中出現true或false。我知道如果您在開始時將列類型設置爲布爾值,那麼您可以使用JCheckBox填充整個列。如何將JTable單元的類更改爲JCheckBox?
我現在的問題是如果有可能改變單個單元格的列類型。
這是JTable中創建代碼:
table = new JTable();
table.setRowMargin(3);
table.setRowHeight(25);
table.setFillsViewportHeight(true);
table.setModel(new DefaultTableModel(
new Object[][] {
{null, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"},
{"", "2016-02-29", "2016-03-01", "2016-03-02", "2016-03-03", "2016-03-04"},
{" 1. Lesson", null, null, null, null, null},
{" 2. Lesson", null, null, null, null, null},
{" 3. Lesson", null, null, null, null, null},
{" 4. Lesson", null, null, null, null, null},
{" 5. Lesson", null, null, null, null, null},
{" 6. Lesson", null, null, null, null, null},
{" 7. Lesson", null, null, null, null, null},
{" 8. Lesson", null, null, null, null, null},
{" 9. Lesson", null, null, null, null, null},
{" 10. Lesson", null, null, null, null, null},
},
new String[] { "Empty", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }
)
{
Class[] columnTypes = new Class[]
{
String.class, Object.class, Object.class, Object.class, Object.class, Object.class
};
public Class getColumnClass(int columnIndex)
{
return columnTypes[columnIndex];
}
});
table.setBorder(new LineBorder(new Color(0, 0, 0)));
table.setBounds(381, 11, 518, 300);
panel.add(table);
這是JTable的內容被改變的部分:
public void tableUpdate(Object[][] a)
{
for(int row = 0; row < 11; row++)
{
for(int column = 0 ; column < 5; column++)
{
table.setValueAt(a[column][row], row+1, column+1);
}
}
}
如果您需要了解更多信息請諮詢我,提前謝謝了。
從'getColumnClass'返回'Boolean.class'作爲你想顯示爲複選框的列 – MadProgrammer
問題是我不想只用複選框選擇整個列,我想要名稱(字符串)和複選框,取決於數組的內容 – jonas001
好的,這不是JTable的工作原理。一列只假設有一個單一的數據類型 – MadProgrammer