我的JTable有问题。首先,我在表格中选择了一些条目。但是,当我取消选择其中一些时,表格无法理解。
示例场景:在我改变主意并取消选择job2后,我选择了job1和2进行测试。但在结果中我看到job1 job1和job2(作业1见过2次,即使我取消选择作业2,我看到了它们。)或者在选择了所有作业(选择所有按钮)之后我想取消全部作业(清除)按钮)当我点击清除所有表似乎是空的。这很好,但不知何故,该计划的背景仍然保护所有旧的选择。我该如何解决这个问题?
尝试:
我通过读取csv文件创建了我的表行。
public class JobSelectionListPanel extends JPanel {
private static final long serialVersionUID = 5198916547962359735L;
private static JobSelectionListPanel INSTANCE = new JobSelectionListPanel();
public static JobSelectionListPanel getInstance() {
return INSTANCE;
}
private JTable table;
private JButton next, back, btnClear, btnNewButton, btnChooseAll;
private JobList fnnJobList = new JobList();
private JobSelectionListPanel() {
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setBorder(new CompoundBorder());
// Read all FNN jobs from file
try {
fnnJobList.readFromFile("rules.csv");
} catch (IOException e1) {
System.out.println("You are not able to read the rules.csv file");
}
// Create ArrayList of JobNames
Object[][] initialData = new Object[fnnJobList.size()][1];
int i = 0;
for (Job jobDes : fnnJobList) {
initialData[i][0] = (Object) jobDes.getJobname();
i++;
}
String[] columnNames = new String[] { "", "Your preferences" };
table.setModel(new DefaultTableModel(initialData, columnNames) {
private static final long serialVersionUID = 1L;
@SuppressWarnings("rawtypes")
Class[] columnTypes = new Class[] { Object.class, Boolean.class };
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
table.getColumnModel().getColumn(1).setPreferredWidth(80);
table.getColumnModel().getColumn(1).setMinWidth(40);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setCellSelectionEnabled(true);
我想要选择所有行,然后我实现了这个。 btnChooseAll =新的JButton(“全选”);
btnChooseAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel chooseAllData = (DefaultTableModel) table.getModel();
if (DeviceGroups.DeviceAList.size() == 0 || DeviceGroups.DeviceBList.size() == 0
|| DeviceGroups.DeviceCList.size() == 0 || DeviceGroups.DeviceDList.size() == 0)
JOptionPane.showMessageDialog(null,
"You should choose at least 1 device for each test device to apply this test case", "Invalid OPTION",
JOptionPane.ERROR_MESSAGE);
else
for (int i = 0; i < chooseAllData.getRowCount(); i++) {
for (int j = 1; j < chooseAllData.getColumnCount(); j++) {
chooseAllData.setValueAt(true, i, j);
}
}
}
});
清除所有偏好:
btnClear = new JButton("Clear all");
// Clear button create a model of JTable and delete all the rows of table!!
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel clearTableData = (DefaultTableModel) table.getModel();
for (int i = 0; i < clearTableData.getRowCount(); i++) {
for (int j = 1; j < clearTableData.getColumnCount(); j++) {
clearTableData.setValueAt(null, i, j);
}
}
}
});
答案 0 :(得分:3)
我在您的代码中看到以下问题:混合视图索引和模型索引。这是令人讨厌的片段:
i
您正在使用table.getValueAt(i, 1) != null
变量来表示视图行索引,因为您正在检查此语句中的值:i
。
但是稍后您使用String jobName = ((DefaultTableModel) table.getModel()).getValueAt(i, 0).toString();
来索引模型:
i
如果 String jobName = ((DefaultTableModel) table.getModel()).getValueAt(table.convertRowIndexToModel(i), 0).toString();
是视图索引,则需要在索引模型之前将其转换为模型索引:
table.getValueAt(i, 1) != null
此外,当在视图中切换列时(即在GUI中的屏幕上),以下内容可能无法按预期工作:
table.getValueAt(i, table.convertColumnIndexToView(1)) != null
您很可能意味着,在模型中获取第二列值,而不是视图。最佳重写然后
{{1}}