import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame{
String [] col = {"Last Name", "First Name", "HKID", "Student ID", "Final Exam Score"};
String [][] data = new String [1][5];
public JFrame j;
public JPanel p;
public JLabel search;
public JTextField s;
public JButton enter;
public JButton NEW;
public JButton unedit;
public JButton update;
public JButton exam;
public DefaultTableModel model = new DefaultTableModel(data, col);;
public JTable jt = new JTable(model);
public GUI(){
setVisible(true);
setSize(600,400);
setTitle("Student Record Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
search = new JLabel("Search: ");
s = new JTextField(" ");
enter = new JButton("Enter");
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
enterPressed();
}
});
p.add(search);
p.add(s);
p.add(enter);
NEW = new JButton("Edit");
NEW.setBounds(10,10,20,20);
NEW.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
updatePressed();
}
});
p.add(NEW);
unedit = new JButton("Unedit");
unedit.setBounds(70,80,20, 20);
unedit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
uneditPressed();
}
});
p.add(unedit);
update = new JButton("Add Student");
update.setBounds(50,40,20,20);
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
NEWpressed();
}
});
p.add(update);
jt.setEnabled(false);
jt.setAutoCreateRowSorter(true);
jt.setPreferredScrollableViewportSize(new Dimension(500,300));
jt.setFillsViewportHeight(true);
jt.setBackground(Color.GRAY);
jt.setAlignmentY(BOTTOM_ALIGNMENT);
JScrollPane jps = new JScrollPane(jt);
p.add(jps);
add(p);
}
public void NEWpressed(){
model.addRow(new Object[]{" ", " ", " ", " ", " "});
}
public void updatePressed(){
jt.setEnabled(true);
}
public void uneditPressed(){
jt.setEnabled(false);
}
public void enterPressed(){
String get = s.getText().toString();
for(int x=0; x< get.length(); x++){
if(data[x].equals(get)){
model= new DefaultTableModel(data[x], col);
}else{
model = new DefaultTableModel(data, col);
}
}
}
public static void main(String args []){
GUI a = new GUI();
a.setVisible(true);
}
}
When making a search JTextField
on JTable
, I want it to show only what I have typed to the search bar.
Under enterPressed()
method I intend to filter the results by having the program examine each letter entered into the textfield and then reset the JTable
to whatever those results are.
I am getting error attempting to reset the DefaultTableModel
with data[x]
. data[x]
is supposed to be whatever is entered into the textfield
If this is not the correct way to do this please explain using Standard Java API. That is excluding DocumentListener
and the like....Maybe i need double for loop here. Also not sure if the if statement made is actually doing what I think it is doing but it is supposed to mean if the text entered in textfield is equal to the data in the JTable
then reset the JTable
to whatever is equal to what is entered in the JTextField
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…