java实现勾选jtable表格中一行,点击修改按钮可以修改其中信息,并且同时将文件中对应信息修改代码实现
时间: 2024-03-23 15:37:45 浏览: 13
好的,这是一个比较复杂的问题,需要分几步来实现。
1. JTable中勾选一行
为了实现勾选JTable中的一行,我们需要使用JCheckBox作为表格中的一个单元格渲染器。我们可以通过设置某一列的渲染器来实现这个功能。具体实现方法如下:
```
JTable table = new JTable();
table.getColumnModel().getColumn(0).setCellRenderer(new CheckBoxRenderer());
table.getColumnModel().getColumn(0).setCellEditor(new CheckBoxEditor(new JCheckBox()));
```
其中,CheckBoxRenderer和CheckBoxEditor是两个自定义的类,CheckBoxRenderer用于渲染JCheckBox,CheckBoxEditor用于编辑JCheckBox。这两个类的代码如下:
```
class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public CheckBoxRenderer() {
setHorizontalAlignment(JCheckBox.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean) value).booleanValue()));
return this;
}
}
class CheckBoxEditor extends DefaultCellEditor {
protected JCheckBox checkBox;
public CheckBoxEditor(JCheckBox checkBox) {
super(checkBox);
this.checkBox = checkBox;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
checkBox.setForeground(table.getSelectionForeground());
checkBox.setBackground(table.getSelectionBackground());
} else {
checkBox.setForeground(table.getForeground());
checkBox.setBackground(table.getBackground());
}
checkBox.setSelected((value != null && ((Boolean) value).booleanValue()));
return checkBox;
}
}
```
2. 点击修改按钮修改信息
当用户勾选了JTable中的一行,并且点击了修改按钮时,我们可以通过获取JTable中选中的行的数据来修改信息。代码如下:
```
JButton modifyButton = new JButton("修改");
modifyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "请先勾选一行");
} else {
// 获取选中行的数据,并进行修改
String name = (String) table.getValueAt(row, 1);
int age = (Integer) table.getValueAt(row, 2);
String address = (String) table.getValueAt(row, 3);
// 显示修改对话框
ModifyDialog dialog = new ModifyDialog(name, age, address);
dialog.setVisible(true);
// 如果用户点击了确定按钮,就更新JTable中的数据
if (dialog.isOK()) {
String newName = dialog.getName();
int newAge = dialog.getAge();
String newAddress = dialog.getAddress();
table.setValueAt(newName, row, 1);
table.setValueAt(newAge, row, 2);
table.setValueAt(newAddress, row, 3);
// 同时更新文件中的数据
updateFile(row, newName, newAge, newAddress);
}
}
}
});
```
这段代码中,我们首先获取选中的行的数据,然后弹出一个修改对话框,让用户输入修改后的数据。如果用户点击了确定按钮,就更新JTable中的数据,并且调用updateFile()方法更新文件中的数据。
3. 更新文件中的对应信息
当用户修改了JTable中的数据后,我们需要将对应的信息写入文件中。具体实现方法如下:
```
private void updateFile(int row, String name, int age, String address) {
try {
File file = new File("data.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
// 更新对应行的数据
String[] values = lines.get(row).split(",");
values[0] = name;
values[1] = String.valueOf(age);
values[2] = address;
lines.set(row, String.join(",", values));
// 将数据写回文件
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (String l : lines) {
writer.write(l);
writer.newLine();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
```
这段代码中,我们首先读取文件中的所有数据,并将其存储在一个List中。然后更新对应行的数据,将修改后的数据写回文件中。注意,我们假设文件中的每一行数据都是由逗号分隔的三个字段组成。如果实际情况不同,请根据实际情况进行修改。
以上就是实现勾选JTable表格中一行,点击修改按钮可以修改其中信息,并且同时将文件中对应信息修改的代码实现。