gpt4 book ai didi

java - 在两列中过滤 JTable

转载 作者:行者123 更新时间:2023-11-30 08:15:07 27 4
gpt4 key购买 nike

我想在我的 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 中键入内容,这是否可行?通过工作,我的意思是通过输入的数据进一步过滤剩余的结果。如果没有,有没有办法结束这个?如果有办法克服这个问题,您能解释一下吗?

最佳答案

In the following code I have commented out a few parts of data and put in sample data so it works without other files.

不要只是注释掉代码,将它从我们不感兴趣的帖子中删除。

另外,为什么要包含按钮的所有代码?这些按钮与问题无关,因此我们不想通读所有代码。阅读的代码越少,我们就越有可能找到问题。

however my second one does not appear to work.

在我看来你有错字:

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

关于java - 在两列中过滤 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106565/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com