java - 过滤多个文本框的 jTable 内容并将输出打印到文件
问题描述
我正在制作一个 Java Swing 应用程序,用户可以在其中将一些数据输入到 JTable 中。数据显示在表格中。我添加了从 csv 导入数据、导出到 txt 或搜索数据表的功能。但我在搜索部分遇到问题。
我有 3 个文本字段,我想从中获取输入,然后在 jtable 中搜索字段并更新它(根据 hwo 许多字段中有文本),仅使结果字段可见,然后仅将这些结果字段导出到文本文件。文本字段中的搜索输入可以出现在一个、两个或所有三个 JTextField 中。我已将动作侦听器添加到搜索按钮,因为如果我将其添加到文本字段中,它可能会让程序感到困惑(在将新记录添加到 jtable 期间,它将开始搜索)。
我不想根据所有三个文本字段进行搜索,这意味着,如果一个 JTextField 有要搜索的用户输入,我想搜索 jtable,如果两个有输入,则搜索 2,如果设置了所有三个,则搜索三个三个。
这是我尝试过的代表:
package stackoverflow;
import java.awt.*;
import java.util.List;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class main {
public static class RefSystemGUI extends JFrame implements ActionListener {
Object[] cols = {"Publication Name",
"Journal Name","Journal Volume","Conference Location"};
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
private JScrollPane sp = new JScrollPane(table);
private JTextField nameOfPub = new JTextField(20);
private JTextField nameOfJour = new JTextField(20);
private JTextField volumeOfJour = new JTextField(20);
private JTextField locConf = new JTextField(20);
private JLabel labnameOfPub = new JLabel("Name Of Publication: ");
private JLabel labnameOfJour = new JLabel("Journal Name: ");
private JLabel labvolumeOfJour = new JLabel("Journal Volume: ");
private JLabel lablocConf = new JLabel("Conference Location: ");
private JButton AddButton= new JButton("Add");
private JButton SearchButton= new JButton("Search");
public static void main(String[] args) {
RefSystemGUI applic = new RefSystemGUI();
}
public RefSystemGUI() {
super("Bibliography");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4,4,3,3);
gbc.gridx=0;
gbc.gridy++;
gbc.gridx++;
add(labnameOfPub,gbc);
gbc.gridx++;
add(nameOfPub,gbc);
gbc.gridx++;
gbc.gridx=0;
gbc.gridy++;
add(labnameOfJour,gbc);
gbc.gridx++;
add(nameOfJour,gbc);
gbc.gridx++;
add(labvolumeOfJour,gbc);
gbc.gridx++;
add(volumeOfJour,gbc);
gbc.gridx=0;
gbc.gridy++;
add(lablocConf,gbc);
gbc.gridx++;
add(locConf,gbc);
gbc.gridx=0;
gbc.gridy++;
add(AddButton,gbc);
gbc.gridx++;
add(SearchButton,gbc);
gbc.gridx=0;
gbc.gridy++;
gbc.weightx=1;
gbc.weighty=1;
gbc.fill=GridBagConstraints.BOTH;
gbc.gridwidth = 8;
gbc.gridheight = 4;
model.setColumnIdentifiers(cols);
gbc.fill = GridBagConstraints.BOTH;
add(sp,gbc);
AddButton.addActionListener(this);
SearchButton.addActionListener(this);
setSize(1080, 1080);
setLocationByPlatform(true);
pack();
setVisible(true);
BlankDisplay();
} //setLayout
public void actionPerformed(ActionEvent event) {
if(event.getSource()==AddButton) {
if ( nameOfPub.getText().isEmpty()) {
}else {
String PubName= nameOfPub.getText();
String JourName = nameOfJour.getText();
String JourVol = volumeOfJour.getText();
String ConfLoc = locConf.getText();
if(PubName.isEmpty()) {
PubName = " ";
}else if(JourName.isEmpty()) {
JourName = " ";
}else if(JourVol.isEmpty()) {
JourVol = " ";
}else if(ConfLoc.isEmpty()) {
ConfLoc = " ";
}
model.addRow(new Object[] {PubName,JourName,JourVol,ConfLoc});
System.out.println("Data Entry Successful");
BlankDisplay();
}
}else if(event.getSource()==SearchButton) {
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(model);
//table.setRowSorter(tr);
table.setAutoCreateRowSorter(true);
ArrayList<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>();
filters.add(RowFilter.regexFilter(nameOfJour.getText(), 0));
filters.add(RowFilter.regexFilter(locConf.getText(), 1));
filters.add(RowFilter.regexFilter(nameOfPub.getText(), 2));
//RowFilter<Object,Object> rf = RowFilter.andFilter(filters);
tr.setRowFilter(RowFilter.orFilter(filters));
table.setRowSorter(tr);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
int columnIndexToSort = 2;
sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.ASCENDING));
tr.setSortKeys(sortKeys);
}
BlankDisplay();
}
public void BlankDisplay() {
nameOfPub.setText("");
nameOfJour.setText("");
volumeOfJour.setText("");
locConf.setText("");
}
}}
此外,搜索到的数据需要根据“作者”进行排序,我试图实现但也失败了。我在整个代码中唯一省略的是其他按钮的动作侦听器(我猜这不应该成为在您的系统上重现问题的障碍)
任何人都可以帮忙吗?提前致谢。
解决方案
推荐阅读
- python - Jinja2 & Flask:引导选项卡仅显示第一个选项卡的内容,而不显示其余选项卡的内容
- c# - 函数上的自定义 EventArgs 消息框不显示来自发件人类的属性
- python - 如何在时间序列上合并两个数据帧
- rust - 为什么我可以直接匹配选项数组而不是包含选项数组的变量?
- javascript - d3按钮不起作用
- javascript - 我如何调用一个单独的嵌入文件,然后命令它?
- java - dynamoDB Java 中的分区键字符串
- css - 如何更改文本位置?
- scala - 在 Spark 中减少每个分区的 Hashmaps
- swift - Swift:枚举案例不是“字符串”类型的成员强制使用原始值