我想在我的JTable中实现2个不同的行过滤器。我已经实现了一个似乎完美无缺的工作但是我的第二个似乎不起作用。在下面的代码中,我已经注释掉了一些数据部分并放入了示例数据,因此它可以在没有其他文件的情况下工作。
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();
}
}
有人可以告诉我问题是什么,并告诉我如何解决它?一旦这个工作,如果有人在filterAthID中搜索了一些东西然后开始在filterForeName中键入一些东西它会起作用吗?通过工作,我的意思是通过输入的数据进一步过滤剩余的结果。如果没有,那么反过来呢?如果有一种方法可以解决这个问题,请解释一下吗?
答案 0 :(得分:3)
在下面的代码中,我已经注释掉了一些数据部分并放入了样本数据,因此它可以在没有其他文件的情况下工作。
不要只是注释掉代码,将其从我们不感兴趣的帖子中删除。
另外,为什么要包含按钮的所有代码?按钮与问题无关,因此我们不想阅读所有代码。读取的代码越少,我们就越有可能找到问题。
然而我的第二个似乎不起作用。
在我看来你有一个错字:
//rf = RowFilter.regexFilter(filterAthID.getText(), 1);
您应该使用过滤器的名称(而不是ID):
rf = RowFilter.regexFilter(filterForeName.getText(), 1);
编辑:
同时在两列上过滤的示例:
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);