问enterPressed方法有什么问题? [英] Asking what is wrong with enterPressed method?

查看:112
本文介绍了问enterPressed方法有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

        }

    }     

JTable上进行搜索JTextField时,我希望它仅显示我在搜索栏中键入的内容.

When making a search JTextField on JTable, I want it to show only what I have typed to the search bar.

enterPressed()方法下,我打算通过让程序检查输入到文本字段中的每个字母,然后将JTable重置为那些结果,来过滤结果.

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.

尝试将DefaultTableModel重置为data[x]时出现错误. data[x]应该是在文本字段中输入的任何内容

I am getting error attempting to reset the DefaultTableModel with data[x]. data[x] is supposed to be whatever is entered into the textfield

如果这不是正确的方法,请使用标准Java API进行说明.那不包括DocumentListener之类的东西....也许我需要在这里加倍for循环.也不确定如果if语句是否确实在做我认为正在做的事情,但这应该意味着在textfield中输入的文本是否等于JTable中的数据,然后将JTable重置为等于JTextField

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

推荐答案

JTable支持通过分类程序API进行过滤.

JTable supports filtering through the sorter API.

从<一个href ="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html" rel ="nofollow">如何使用表格教程

例如...

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

这个简单的示例将前两列进行比较,以查看它们是否包含s JTextField中包含的文本.

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

如果需要,可以使用String#startsWith查找以您输入的文本开头的String匹配项.

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

该示例也区分大小写,您可以在String(过滤器和列值)上同时使用toLowerCase来否定该值,或者如果您想要绝对匹配,请使用equalsIgnoresCase

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

请勿使用null布局.像素完美的布局是现代UI设计中的一种错觉,您无法控制字体,DPI,渲染管道或其他会改变组件在屏幕上渲染方式的因素.

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旨在与布局管理器合作以克服这些问题.如果您坚持不理会这些功能并反对API设计,请为许多麻烦做准备,并且永不结束艰苦的工作...

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...

这篇关于问enterPressed方法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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