Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

JTable supports filtering through the sorter API.

Check out Sorting and Filtering from the How to use Tables tutorial

For example...

public void enterPressed() {
    RowFilter<DefaultTableModel, Integer> filter;
    filter = new RowFilter<DefaultTableModel, Integer>() {

        @Override
        public boolean include(RowFilter.Entry<? extends DefaultTableModel, ? extends Integer> entry) {
            String filter = s.getText();

            DefaultTableModel model = entry.getModel();
            int row = entry.getIdentifier();

            boolean include = false;
            if (model.getValueAt(row, 0).toString().contains(filter) || model.getValueAt(row, 0).toString().contains(filter)) {
                include = true;
            }

            return include;
        }
    };
    ((TableRowSorter)jt.getRowSorter()).setRowFilter(filter);
    //        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);
    //            }
    //        }
}

This simple examples compares the first two columns to see if they contain the text contained within the s JTextField.

You could use String#startsWith if you wanted, to look for String matches that start with the text you have entered.

The example is also case sensitive, you could use toLowerCase on both Strings (the filter and the column value) to negate this or equalsIgnoresCase if you want an absolute match

Don't use null layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen.

Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...