在两列中过滤JTable [英] Filtering a JTable in two columns

查看:78
本文介绍了在两列中过滤JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JTable中实现2个不同的行过滤器.我已经实现了一种似乎可以完美工作的方法,但是我的第二种似乎却没有效果.在下面的代码中,我注释掉了部分数据,并放入了示例数据,这样它就可以在没有其他文件的情况下工作.

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();
}
}

有人可以告诉我问题出在哪里,然后告诉我如何解决?同样,一旦有人在filterAthID中搜索某些内容,然后开始在filterForeName中键入内容,这是否可行呢?所谓工作,是指根据输入的数据进一步过滤其余结果.如果不是,那可以克服吗?如果有办法解决这个问题,您能解释一下吗?

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);

您应该使用过滤器的名称(而不是ID):

You should be using the name for the filter (not the ID):

rf = RowFilter.regexFilter(filterForeName.getText(), 1);

同时在两列上进行过滤的示例:

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);

这篇关于在两列中过滤JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆