如何读取和搜索 jTable 中的文件?
How can I read and search from file in jTable?
我想在此代码中执行的操作: 单击搜索按钮时,它将读取 file
然后将搜索值与 file
& 将在 jTable
.
中显示搜索结果
我面临的问题: 如果选择 GPA A+,那么它会显示 A+、A- 并且当我在给出另一个搜索值后再次按下搜索按钮时,table 只是在其中添加了更多数据。
所需的解决方案: 我只想读取文件并仅在 jTable
中显示结果,而不是一次又一次地添加结果。搜索按钮应仅在 GPA 和 Class 列中进行搜索。 & when GPA is selected "A/B/C+" or "-" the search result should give only the data containing that particular GPA.
注意:我不想更改搜索选项。
我是 JAVA 的新手。所以任何形式的帮助将不胜感激! :)
Screenshot of the UI
private void srchBtnActionPerformed(java.awt.event.ActionEvent evt) {
//file read
String filepath = "E:\Netbeans workspace\modified\Project\Info.txt";
File file = new File(filepath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
model = (DefaultTableModel)jTable1.getModel();
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++){
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
model.addRow(dataRow);
}
} catch (Exception ex) {
Logger.getLogger(ReceiverF.class.getName()).log(Level.SEVERE, null, ex);
}
//search from file
String bGroupSrch = (String) jComboBoxBGroup.getSelectedItem();
if(positiveRBtn.isSelected())
bGroupSrch = bGroupSrch + "+";
else if(negativeRBtn.isSelected())
bGroupSrch = bGroupSrch + "-";
String areaSrch = (String)jComboBoxArea.getSelectedItem();
if (bgGroup.getSelection() != null) {
filter(bGroupSrch);
filter(areaSrch);
} else {
SrchEMsg sem = new SrchEMsg(this);
sem.setVisible(true);
sem.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
}
//Filter Method
private void filter(String query){
TableRowSorter<DefaultTableModel> tr= new TableRowSorter<DefaultTableModel>(model);
jTable1.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(query));
}
the table just adds more data in it.
当您开始搜索时:
model.setRowCount(0);
清除table的table模型中的数据。
或者更简单的解决方案是不要一直重新加载数据。相反,您只需更改 table 使用的过滤器。
阅读 Sorting and Filtering 上的 Swing 教程部分。每次键入字符时,那里的代码都会替换过滤器。
您的代码将在更改搜索选项时更改过滤器。
我想在此代码中执行的操作: 单击搜索按钮时,它将读取 file
然后将搜索值与 file
& 将在 jTable
.
我面临的问题: 如果选择 GPA A+,那么它会显示 A+、A- 并且当我在给出另一个搜索值后再次按下搜索按钮时,table 只是在其中添加了更多数据。
所需的解决方案: 我只想读取文件并仅在 jTable
中显示结果,而不是一次又一次地添加结果。搜索按钮应仅在 GPA 和 Class 列中进行搜索。 & when GPA is selected "A/B/C+" or "-" the search result should give only the data containing that particular GPA.
注意:我不想更改搜索选项。
我是 JAVA 的新手。所以任何形式的帮助将不胜感激! :)
Screenshot of the UI
private void srchBtnActionPerformed(java.awt.event.ActionEvent evt) {
//file read
String filepath = "E:\Netbeans workspace\modified\Project\Info.txt";
File file = new File(filepath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
model = (DefaultTableModel)jTable1.getModel();
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++){
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
model.addRow(dataRow);
}
} catch (Exception ex) {
Logger.getLogger(ReceiverF.class.getName()).log(Level.SEVERE, null, ex);
}
//search from file
String bGroupSrch = (String) jComboBoxBGroup.getSelectedItem();
if(positiveRBtn.isSelected())
bGroupSrch = bGroupSrch + "+";
else if(negativeRBtn.isSelected())
bGroupSrch = bGroupSrch + "-";
String areaSrch = (String)jComboBoxArea.getSelectedItem();
if (bgGroup.getSelection() != null) {
filter(bGroupSrch);
filter(areaSrch);
} else {
SrchEMsg sem = new SrchEMsg(this);
sem.setVisible(true);
sem.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
}
//Filter Method
private void filter(String query){
TableRowSorter<DefaultTableModel> tr= new TableRowSorter<DefaultTableModel>(model);
jTable1.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(query));
}
the table just adds more data in it.
当您开始搜索时:
model.setRowCount(0);
清除table的table模型中的数据。
或者更简单的解决方案是不要一直重新加载数据。相反,您只需更改 table 使用的过滤器。
阅读 Sorting and Filtering 上的 Swing 教程部分。每次键入字符时,那里的代码都会替换过滤器。
您的代码将在更改搜索选项时更改过滤器。