gpt4 book ai didi

java - 从 JTable 中选择一个项目并将其放在另一个项目上

转载 作者:行者123 更新时间:2023-11-30 07:24:24 25 4
gpt4 key购买 nike

我想在 JTable 中添加一个复选框,当用户选择一个问题时,它将添加到另一个 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();

最佳答案

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。

关于java - 从 JTable 中选择一个项目并将其放在另一个项目上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37002441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com