1
我正在嘗試將一個jcheckbox添加到JTable的第一列,該列使用一個DefaultTableModel
。我試着爲該列返回一個Boolean.class
,但它不起作用。如何通過添加布爾列將JCheckBox添加到DefaultTableModel?
我已經有一個的JComboBox在最後一列,但使用我用於添加,爲了增加JCheckBox的不起作用的方法相同。我在線閱讀,如果您在列中呈現Boolean.class
,但是使用它也不起作用,java會自動爲您返回複選框。我認爲這可能是我訂購組件的方式中的一個問題。
//import statements.
public class CourseSelection extends GUIDesign implements ActionListener{
// lot of extraneous stuff,
JLabel termLabel;
String term;
JLabel departmentLabel;
String department;
String[] columns = {
"Select", "CRN", "Title", "Instructor", "Time",
"Days", "Location", "Section", "Code", "Mode of Grading"
};
int courses;
Object[][] data;
String[] modes = {"Audit", "Pass/Fail", "Letter Grding"};
// create the table here, with model.
JTable table;
DefaultTableModel model = new DefaultTableModel() {
Class[] types = {
Boolean.class, String.class, String.class, String.class,
String.class, String.class, String.class, String.class,
String.class, Object.class
};
// making sure that it returns boolean.class.
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
};
JPanel termPanel = new JPanel();
JPanel departmentPanel = new JPanel();
JButton backButton = new JButton("Back");
JButton registerButton = new JButton("Register");
JButton refreshButton = new JButton("Refresh");
public CourseSelection(GTPort gPort) {
super("Course Selection", 3);
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
header = new JPanel();
header.setSize(getWidth(), 30);
body = new JPanel();
body.setLayout(new BoxLayout(body,BoxLayout.Y_AXIS));
header.setBackground(color);
title.setFont(new Font("Helvetica", 1,20));
header.add(title);
termLabel = new JLabel("Term: " + term);
data = new Object[25][10];
model = new DefaultTableModel(data, columns);
table = new JTable(model);
table.setModel(model);
departmentLabel = new JLabel("Department: " + department);
termPanel.add(termLabel);
departmentPanel.add(departmentLabel);
JComboBox box = new JComboBox(modes);
JCheckBox checkBox = new JCheckBox();
// lot of other code I tried that doesn't work.
// table.getColumnModel().getColumn(1)
// .setCellEditor(table.getDefaultEditor(Boolean.class));
// table.getColumnModel().getColumn(1)
// .setCellRenderer(table.getDefaultRenderer(Boolean.class));
// table.setDefaultEditor(Boolean.class, new DefaultCellEditor(checkBox));
// table.setDefaultRenderer(Boolean.class, new DefaultTableCellRenderer());
// model.getColumnModel().getColumn(1)
// .setCellEditor(new DefaultCellEditor(checkBox));
// model.getColumnModel().getColumn(1)
// .setCellRenderer(new DefaultTableCellRenderer());
table.getColumnModel().getColumn(9)
.setCellEditor(new DefaultCellEditor(box));
// .setCellEditor(new DefaultCellEditor(box));
// table = new JTable(model);
body.add(termPanel);
body.add(departmentPanel);
body.add(table.getTableHeader());
body.add(table);
// body.add(body);
buttonPanel.add(backButton);
buttonPanel.add(refreshButton);
buttonPanel.add(registerButton);
backButton.addActionListener(this);
refreshButton.addActionListener(this);
registerButton.addActionListener(this);
add(header);
add(body);
add(buttonPanel);
gtPort = gPort;
}
}
但後來我又如何得到的getColumnClass()方法返回一個布爾類,因爲我讀到這是確保您獲得複選框的唯一方法。 – iamseiko
確實是這樣做的方式。它不工作,因爲你**不會**代碼中的'getColumnClass()'。覆蓋它,它會工作。看到我編輯的答案。 –
請參閱我編輯的答案。 –