java弹窗表格把文件的书存入表格实现管理员对书籍的添加查询修改,不用数据库
时间: 2024-03-16 10:43:52 浏览: 27
可以使用Java中的JTable和JFileChooser实现弹窗表格,并通过文件读写来实现管理员对书籍的添加、查询和修改,不需要使用数据库。
首先,需要创建一个JTable对象和一个JFileChooser对象:
```java
JTable table = new JTable();
JFileChooser fileChooser = new JFileChooser();
```
然后,可以使用JFileChooser来选择文件并将文件中的数据读取到JTable中:
```java
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
String line;
DefaultTableModel model = (DefaultTableModel) table.getModel();
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
model.addRow(data);
}
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
```
在上述代码中,使用BufferedReader和FileReader从文件中逐行读取数据,并将数据拆分为数组,然后将数组添加到JTable的DefaultTableModel中。
接下来,可以使用JTable的getRowSorter()方法和RowFilter类来实现管理员的查询功能:
```java
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
String text = searchField.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text));
}
```
在上述代码中,使用TableRowSorter和RowFilter来实现按关键字查询,并将结果显示在JTable中。
然后,可以使用JTable的setValueAt()方法和Dialog类来实现管理员的修改功能:
```java
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "Please select a book to edit.");
} else {
String title = JOptionPane.showInputDialog(null, "Enter new title:");
String author = JOptionPane.showInputDialog(null, "Enter new author:");
int year = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter new year:"));
table.setValueAt(title, row, 0);
table.setValueAt(author, row, 1);
table.setValueAt(year, row, 2);
}
```
在上述代码中,使用JOptionPane来弹出对话框,让管理员输入新的书籍信息,并使用JTable的setValueAt()方法将新的信息更新到JTable中。
最后,可以使用JTable的getModel()方法和FileWriter类来将数据保存到文件中:
```java
int returnValue = fileChooser.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(selectedFile));
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
bw.write(table.getValueAt(i, j).toString());
if (j < table.getColumnCount() - 1) {
bw.write(",");
}
}
bw.newLine();
}
bw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
```
在上述代码中,使用BufferedWriter和FileWriter将JTable中的数据逐行写入文件中。
至此,就完成了Java弹窗表格把文件的书存入表格实现管理员对书籍的添加查询和修改的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)