JTable select 和 deselect 不工作
JTable select and deselect not working
我的 JTable 有问题。首先,我 select 在我的 table 中编辑了一些条目。但是当我 deselect 其中一些时, table 无法理解。
示例场景:我 select job1 和 2 用于测试,之后我改变主意并删除 select job2。但在结果中我看到了 job1 job1 和 job2(job 1 看到了 2 次,即使我看到了 dis-select job 2 我也看到了它们。)或者在 selected 之后所有的工作(选择所有按钮)当我单击清除所有 table 时,我想删除所有这些(清除按钮),table 似乎是空的。这很好,但不知何故,程序的背景仍然保护着所有旧的 selection。我该如何解决?
尝试:
我通过读取 csv 文件创建了 table 的行。
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 = new JButton("Choose all");
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);
}
}
}
});
我在您的代码中看到以下问题:混淆了视图索引和模型索引。这是有问题的片段:
for (int i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 1) != null) {
if (((Boolean) table.getValueAt(i, 1)).booleanValue()) {
String jobName = (((DefaultTableModel) table.getModel()).getValueAt(i, 0).toString());
您正在使用 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();
此外,当列在视图中切换时(即在您的 GUI 屏幕上),以下内容可能无法按预期工作:
table.getValueAt(i, 1) != null
您很可能是想说,获取模型中的第二列值,而不是视图中的值。最好重写为
table.getValueAt(i, table.convertColumnIndexToView(1)) != null
我的 JTable 有问题。首先,我 select 在我的 table 中编辑了一些条目。但是当我 deselect 其中一些时, table 无法理解。
示例场景:我 select job1 和 2 用于测试,之后我改变主意并删除 select job2。但在结果中我看到了 job1 job1 和 job2(job 1 看到了 2 次,即使我看到了 dis-select job 2 我也看到了它们。)或者在 selected 之后所有的工作(选择所有按钮)当我单击清除所有 table 时,我想删除所有这些(清除按钮),table 似乎是空的。这很好,但不知何故,程序的背景仍然保护着所有旧的 selection。我该如何解决?
尝试:
我通过读取 csv 文件创建了 table 的行。
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 = new JButton("Choose all");
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);
}
}
}
});
我在您的代码中看到以下问题:混淆了视图索引和模型索引。这是有问题的片段:
for (int i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 1) != null) {
if (((Boolean) table.getValueAt(i, 1)).booleanValue()) {
String jobName = (((DefaultTableModel) table.getModel()).getValueAt(i, 0).toString());
您正在使用 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();
此外,当列在视图中切换时(即在您的 GUI 屏幕上),以下内容可能无法按预期工作:
table.getValueAt(i, 1) != null
您很可能是想说,获取模型中的第二列值,而不是视图中的值。最好重写为
table.getValueAt(i, table.convertColumnIndexToView(1)) != null