我想在JTable中添加一个CheckBox,当用户选择一个问题时,它将被添加到另一个JTable上,但优先级是我想要添加复选框,知道JTable包含从数据库中获取的信息。 感谢帮助 。我希望你了解我。 我制作的模型我想要的是this is the models that i want to have 这是我的代码The result
的结果 List<Question> questions=new ArrayList<>();
JButton btnAfficher = new JButton("Afficher toutes les questions");
btnAfficher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
categorie=GestionCategorieDelegate.doFindCategorieById(PreparerTest.idCategorie);
questions=GestionTestDelegate.doPrepareManuallyTest(categorie);
initDataBindings();
}
});
table_1 = new JTable();
scrollPane_1.setViewportView(table_1);
table = new JTable();
scrollPane.setViewportView(table);
setLayout(groupLayout);
initDataBindings();
答案 0 :(得分:1)
CustomTableModel mymodel = new CustomTableModel();
mymodel.addRow(new Object[]{false, "2ndcoldata", "3rdcol data"});
mymodel.addRow(new Object[]{true, "2ndcoldata", "3rdcol data"});
mytable.setModel(mymodel);
public class CustomTableModel extends DefaultTableModel {
public MyTableModel() {
super(new String[]{"col1", "col2", "col3"}, 0);
@Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
switch (columnIndex) {
case 0:
clazz = Boolean.class;
break;
}
return clazz;
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
@Override
public void setValueAt(Object aValue, int row, int column) {
if (aValue instanceof Boolean && column == 0) {
Vector rowData = (Vector)getDataVector().get(row);
rowData.set(0, (boolean)aValue);
fireTableCellUpdated(row, column);
}
}}
你可以用上面的方法做,我有customtableModel类,它扩展了DefaultTableModel类。每当您需要向表中添加数据时,创建一个CustomTableModel实例,并在完成后将行数据添加到模型中,然后将模型设置为jtable。