Java 在两列中筛选JTable
我想在我的JTable中实现2个不同的行过滤器。我已经实现了一个似乎工作完美,但我的第二个似乎不工作。在下面的代码中,我注释掉了数据的一些部分,并加入了示例数据,这样它就可以在没有其他文件的情况下工作Java 在两列中筛选JTable,java,swing,jtable,rowfilter,Java,Swing,Jtable,Rowfilter,我想在我的JTable中实现2个不同的行过滤器。我已经实现了一个似乎工作完美,但我的第二个似乎不工作。在下面的代码中,我注释掉了数据的一些部分,并加入了示例数据,这样它就可以在没有其他文件的情况下工作 import javax.swing.*; import java.awt.*; import java.io.*; import java.lang.*; import java.awt.event.*; /////////// import javax.swing.border.EmptyBo
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.event.*;
///////////
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.text.Document;
import javax.swing.table.TableRowSorter;
public class CompFrame extends JFrame implements ActionListener
{
/*AthleteList a = new AthleteList();
TrainingRList r= new TrainingRList();
TrainingSList s= new TrainingSList();
TrainingCList c= new TrainingCList();
TrainingPList p= new TrainingPList();
*/
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel(); //a panel for first tab
//first panel components
JScrollPane myScrollTable;
/*JLabel Logol = new JLabel();
ImageIcon imgAddAthleteLogo = new ImageIcon("AddAthletelogo.png"); //Declares as an ImageIcon
Image tempOriginal = imgAddAthleteLogo.getImage(); //Declares the ImageIcon as an Image
Image smallerLogo = tempOriginal.getScaledInstance(1280,120,0); //Rescales the Image to specified size
ImageIcon smalltest= new ImageIcon(smallerLogo); //Creates a new ImageIcon with the rescaled image
JLabel Logo = new JLabel(smalltest); //Puts the new ImageIcon inside a JLabel so that it can be displayed */
JButton btnAdminOnly = new JButton();
JButton btnTPersonal = new JButton();
JButton btnTRunning = new JButton();
JButton btnTCycling = new JButton();
JButton btnTSwimming = new JButton();
//end first panel
JTextField filterAthID;
JTextField filterForeName;
TableRowSorter sorter;
JLabel AthleteID = new JLabel();
JLabel ForeName = new JLabel();
///////////////
public void runGUI()
{
/*//////////////
a.readAthleteDataFromFile();
c.readCycling();
p.readPersonal();
r.readRunning();
s.readSwimming();
//////////////
a.athData();
//c.tcData();
//p.tpData();
//r.trData();
//s.tsData();
//////////////*/
myMainWindow.setBounds(10, 10, 1296, 756); //set position, then dimensions
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel(); //call method to create each panel
myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow
myMainWindow.setVisible(true); //make the GUI appear
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
/*
Logo.setLocation(0,0); //Sets the location
Logo.setSize(1280,120); //Sets the size
firstPanel.add(Logo); //Adds it to the panel
*/
AthleteID.setLocation(200,120); //Sets the location
AthleteID.setSize(150,26); //Sets the size
AthleteID.setText("Athlete ID Search:");
firstPanel.add(AthleteID); //Adds it to the panel
ForeName.setLocation(731,120); //Sets the location
ForeName.setSize(150,26); //Sets the size
ForeName.setText("Athlete Name Search:");
firstPanel.add(ForeName); //Adds it to the panel
btnAdminOnly.setLocation(0,120);
btnAdminOnly.setSize(200,50);
btnAdminOnly.setText("Display Athlete Data Admin");
btnAdminOnly.addActionListener(this);
firstPanel.add(btnAdminOnly);
btnTPersonal.setLocation(0,170);
btnTPersonal.setSize(200,50);
btnTPersonal.setText("Display Athlete Data");
btnTPersonal.addActionListener(this);
firstPanel.add(btnTPersonal);
btnTRunning.setLocation(0,220);
btnTRunning.setSize(200,50);
btnTRunning.setText("Display Running Data");
btnTRunning.addActionListener(this);
firstPanel.add(btnTRunning);
btnTCycling.setLocation(0,270);
btnTCycling.setSize(200,50);
btnTCycling.setText("Display Cycling Data");
btnTCycling.addActionListener(this);
firstPanel.add(btnTCycling);
btnTSwimming.setLocation(0,320);
btnTSwimming.setSize(200,50);
btnTSwimming.setText("Display Swimming Data");
btnTSwimming.addActionListener(this);
firstPanel.add(btnTSwimming);
String[] sHeaders = {"Athlete ID","Forename","Surname"};
String[][] sSampleData = new String[3][3]; //rows,cols
sSampleData[0][0] = "JS96";
sSampleData[0][1] = "John";
sSampleData[0][2] = "Smith";
sSampleData[1][0] = "DB67";
sSampleData[1][1] = "David";
sSampleData[1][2] = "Bower";
sSampleData[2][0] = "LL87";
sSampleData[2][1] = "Lex";
sSampleData[2][2] = "Luthor";
JTable athTable = new JTable(sSampleData,sHeaders);
/*JTable tpTable = new JTable(a.compTableData,aHeaders);
JTable trTable = new JTable(a.compTableData,aHeaders);
JTable tcTable = new JTable(a.compTableData,aHeaders);
JTable tsTable = new JTable(a.compTableData,aHeaders);
*/
athTable.setAutoCreateRowSorter(true);
myScrollTable = new JScrollPane(athTable);
myScrollTable.setSize(1082,600);
myScrollTable.setLocation(200,145);
System.out.println("Creating compare table");
///////////////
sorter = new TableRowSorter(athTable.getModel());
athTable.setRowSorter(sorter);
filterAthID = new JTextField(10);
filterAthID.setSize(425,26);
filterAthID.setLocation(306,120);
filterAthID.setToolTipText("Enter Athlete ID");
firstPanel.add(filterAthID);
filterForeName = new JTextField(10);
filterForeName.setSize(425,26);
filterForeName.setLocation(857,120);
filterForeName.setToolTipText("Enter Athlete Name");
firstPanel.add(filterForeName);
//////////////////////
Document doc = filterAthID.getDocument();
DocumentListener listener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
doc.addDocumentListener(listener);
///////////////
Document docb = filterForeName.getDocument();
DocumentListener listenerb = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilterb();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilterb();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilterb();
}
};
docb.addDocumentListener(listenerb);
///////////////
firstPanel.add(myScrollTable);
}
private void newFilter()
{
RowFilter rf = null;
try
{
rf = RowFilter.regexFilter(filterAthID.getText(), 0);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
private void newFilterb()
{
RowFilter rf = null;
try
{
rf = RowFilter.regexFilter(filterAthID.getText(), 1);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnAdminOnly)
{
firstPanel.remove(myScrollTable);
firstPanel.add(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to athTable");
}
if(e.getSource() == btnTPersonal)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tpTable");
}
if(e.getSource() == btnTRunning)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to trTable");
}
if(e.getSource() == btnTCycling)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tcTable");
}
if(e.getSource() == btnTSwimming)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tsTable");
}
}
public static void main(String[] args)
{
CompFrame cf = new CompFrame();
cf.runGUI();
}
}
有人能告诉我问题是什么,并告诉我如何解决它吗?同样,一旦这起作用,如果有人在FilterHid中搜索了某个内容,然后开始在filterForeName中键入某个内容,它会起作用吗?我所说的工作是指通过输入的数据进一步过滤剩余的结果。如果不是的话,这件事还会发生吗?如果有办法克服这个问题,你能解释一下吗
在下面的代码中,我注释掉了数据的一些部分,并加入了示例数据,这样它就可以在没有其他文件的情况下工作
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.event.*;
///////////
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.text.Document;
import javax.swing.table.TableRowSorter;
public class CompFrame extends JFrame implements ActionListener
{
/*AthleteList a = new AthleteList();
TrainingRList r= new TrainingRList();
TrainingSList s= new TrainingSList();
TrainingCList c= new TrainingCList();
TrainingPList p= new TrainingPList();
*/
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel(); //a panel for first tab
//first panel components
JScrollPane myScrollTable;
/*JLabel Logol = new JLabel();
ImageIcon imgAddAthleteLogo = new ImageIcon("AddAthletelogo.png"); //Declares as an ImageIcon
Image tempOriginal = imgAddAthleteLogo.getImage(); //Declares the ImageIcon as an Image
Image smallerLogo = tempOriginal.getScaledInstance(1280,120,0); //Rescales the Image to specified size
ImageIcon smalltest= new ImageIcon(smallerLogo); //Creates a new ImageIcon with the rescaled image
JLabel Logo = new JLabel(smalltest); //Puts the new ImageIcon inside a JLabel so that it can be displayed */
JButton btnAdminOnly = new JButton();
JButton btnTPersonal = new JButton();
JButton btnTRunning = new JButton();
JButton btnTCycling = new JButton();
JButton btnTSwimming = new JButton();
//end first panel
JTextField filterAthID;
JTextField filterForeName;
TableRowSorter sorter;
JLabel AthleteID = new JLabel();
JLabel ForeName = new JLabel();
///////////////
public void runGUI()
{
/*//////////////
a.readAthleteDataFromFile();
c.readCycling();
p.readPersonal();
r.readRunning();
s.readSwimming();
//////////////
a.athData();
//c.tcData();
//p.tpData();
//r.trData();
//s.tsData();
//////////////*/
myMainWindow.setBounds(10, 10, 1296, 756); //set position, then dimensions
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel(); //call method to create each panel
myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow
myMainWindow.setVisible(true); //make the GUI appear
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
/*
Logo.setLocation(0,0); //Sets the location
Logo.setSize(1280,120); //Sets the size
firstPanel.add(Logo); //Adds it to the panel
*/
AthleteID.setLocation(200,120); //Sets the location
AthleteID.setSize(150,26); //Sets the size
AthleteID.setText("Athlete ID Search:");
firstPanel.add(AthleteID); //Adds it to the panel
ForeName.setLocation(731,120); //Sets the location
ForeName.setSize(150,26); //Sets the size
ForeName.setText("Athlete Name Search:");
firstPanel.add(ForeName); //Adds it to the panel
btnAdminOnly.setLocation(0,120);
btnAdminOnly.setSize(200,50);
btnAdminOnly.setText("Display Athlete Data Admin");
btnAdminOnly.addActionListener(this);
firstPanel.add(btnAdminOnly);
btnTPersonal.setLocation(0,170);
btnTPersonal.setSize(200,50);
btnTPersonal.setText("Display Athlete Data");
btnTPersonal.addActionListener(this);
firstPanel.add(btnTPersonal);
btnTRunning.setLocation(0,220);
btnTRunning.setSize(200,50);
btnTRunning.setText("Display Running Data");
btnTRunning.addActionListener(this);
firstPanel.add(btnTRunning);
btnTCycling.setLocation(0,270);
btnTCycling.setSize(200,50);
btnTCycling.setText("Display Cycling Data");
btnTCycling.addActionListener(this);
firstPanel.add(btnTCycling);
btnTSwimming.setLocation(0,320);
btnTSwimming.setSize(200,50);
btnTSwimming.setText("Display Swimming Data");
btnTSwimming.addActionListener(this);
firstPanel.add(btnTSwimming);
String[] sHeaders = {"Athlete ID","Forename","Surname"};
String[][] sSampleData = new String[3][3]; //rows,cols
sSampleData[0][0] = "JS96";
sSampleData[0][1] = "John";
sSampleData[0][2] = "Smith";
sSampleData[1][0] = "DB67";
sSampleData[1][1] = "David";
sSampleData[1][2] = "Bower";
sSampleData[2][0] = "LL87";
sSampleData[2][1] = "Lex";
sSampleData[2][2] = "Luthor";
JTable athTable = new JTable(sSampleData,sHeaders);
/*JTable tpTable = new JTable(a.compTableData,aHeaders);
JTable trTable = new JTable(a.compTableData,aHeaders);
JTable tcTable = new JTable(a.compTableData,aHeaders);
JTable tsTable = new JTable(a.compTableData,aHeaders);
*/
athTable.setAutoCreateRowSorter(true);
myScrollTable = new JScrollPane(athTable);
myScrollTable.setSize(1082,600);
myScrollTable.setLocation(200,145);
System.out.println("Creating compare table");
///////////////
sorter = new TableRowSorter(athTable.getModel());
athTable.setRowSorter(sorter);
filterAthID = new JTextField(10);
filterAthID.setSize(425,26);
filterAthID.setLocation(306,120);
filterAthID.setToolTipText("Enter Athlete ID");
firstPanel.add(filterAthID);
filterForeName = new JTextField(10);
filterForeName.setSize(425,26);
filterForeName.setLocation(857,120);
filterForeName.setToolTipText("Enter Athlete Name");
firstPanel.add(filterForeName);
//////////////////////
Document doc = filterAthID.getDocument();
DocumentListener listener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
doc.addDocumentListener(listener);
///////////////
Document docb = filterForeName.getDocument();
DocumentListener listenerb = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilterb();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilterb();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilterb();
}
};
docb.addDocumentListener(listenerb);
///////////////
firstPanel.add(myScrollTable);
}
private void newFilter()
{
RowFilter rf = null;
try
{
rf = RowFilter.regexFilter(filterAthID.getText(), 0);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
private void newFilterb()
{
RowFilter rf = null;
try
{
rf = RowFilter.regexFilter(filterAthID.getText(), 1);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnAdminOnly)
{
firstPanel.remove(myScrollTable);
firstPanel.add(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to athTable");
}
if(e.getSource() == btnTPersonal)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tpTable");
}
if(e.getSource() == btnTRunning)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to trTable");
}
if(e.getSource() == btnTCycling)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tcTable");
}
if(e.getSource() == btnTSwimming)
{
firstPanel.remove(myScrollTable);
firstPanel.repaint();
System.out.println("Changing table to tsTable");
}
}
public static void main(String[] args)
{
CompFrame cf = new CompFrame();
cf.runGUI();
}
}
不要只是注释掉代码,从帖子中删除它,我们对此不感兴趣
还有,为什么要包含按钮的所有代码?按钮与问题无关,因此我们不想通读所有代码。要读取的代码越少,我们就越有可能发现问题
然而,我的第二个似乎不起作用
在我看来,你好像有打字错误:
//rf = RowFilter.regexFilter(filterAthID.getText(), 1);
您应该使用筛选器的名称(而不是ID):
编辑:
同时对两列进行筛选的示例:
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter(textField1.getText(), 0));
filters.add(RowFilter.regexFilter(textField2.getText(), 1));
rf = RowFilter.andFilter(filters);
List filters=newarraylist(2);
添加(RowFilter.regexFilter(textField1.getText(),0));
添加(RowFilter.regexFilter(textField2.getText(),1));
rf=行过滤器和过滤器(过滤器);
我想确保我理解。您想根据ID进行筛选,然后在重复的ID中,根据名字进行筛选?为什么会有重复的身份证?不,我不想要重复的身份证,老实说,我不知道我在哪里提到过。我在哪里提到的?我不明白你说的是什么意思看起来我有打字错误。第一行代码rf=RowFilter.regexFilter(filterAthID.getText(),0);过滤第一列,另一列应过滤第二列rf=RowFilter.regexFilter(filterForeName.getText(),1);如0和1所示,问题是您的第二个筛选器仍然使用“ID”作为筛选器,而不是“名称”。因此,我如何使其生效,以使上的名称生效。因为当第一个文本字段为空时,第二个文本字段仍然不起作用。您正在用一个全新的筛选器替换筛选器,因此筛选器过程将重新开始。如果要同时对ID和名称进行筛选,则需要更复杂的筛选。请参见编辑:当使用“列表”和“ArrayList”时,我会为它们中的每一个获取一个找不到的符号。我需要申报什么@卡米克尔