I would like to implement 2 different row filters into my JTable. I have implemented one which appears to work flawlessly however my second one does not appear to work. In the following code I have commented out a few parts of data and put in sample data so it works without other files.
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();
}
}
Can someone tell me what the problem is and tell me how to fix it? Also once this is working if someone searched something in the filterAthID and then started to type something in the filterForeName would it work? By work I mean filter the remaining results further by the inputted data. If not is there anyway to over come this? And if there is a way to over come this could you please explain how?
In the following code I have commented out a few parts of data and put in sample data so it works without other files.
Don't just comment out the code, remove it from the posting it is of no interest to us.
Also, why did you include all the code for the buttons? The buttons are not related to the problem so we don't want to read through all the code. The less code to read the more likely we can find the problem.
however my second one does not appear to work.
Looks to me like you have a typo:
//rf = RowFilter.regexFilter(filterAthID.getText(), 1);
You should be using the name for the filter (not the ID):
rf = RowFilter.regexFilter(filterForeName.getText(), 1);
Edit:
Example of filter on two columns at the same time:
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);