繁体   English   中英

使用密钥侦听器在数据库 msql 中进行 Jtextfield 搜索

[英]Jtextfield search in database msql using keylistener

当我在JTextField查找数据库中的名称时,我想按 Enter 键显示所有相应的名称。 不幸的是我做不到,有人可以帮助我。

public class Rechercher extends JFrame{

    private JLabel rechercher;
    private JTextField trechercher;
    private JButton executer;
    private JButton exit;
    static Connection connection;
    Vector titrecolonnes = new Vector();
    Vector donnee = new Vector();
    private JTable table;
    private TableRowSorter<TableModel> sorter;

    public Rechercher()
    {
        initComponents();
    }

    public void initComponents()
    {
        setLayout(null);

        trechercher = new JTextField("");
        add(trechercher);
        trechercher.setBounds(140, 30, 235,35);
        executer = new JButton("Rechercher :");
        add(executer);
        executer.setBounds(10, 34, 115,25);
        try
         {
             Class.forName("com.mysql.jdbc.Driver");
             System.out.println("com.mysql.jdbc.Driver found");
             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/eva","root","");
             System.out.println("Connexion Ok");
         }catch(Exception cnfe)
        {
                System.out.println("Error:"+cnfe.getMessage());
                }

        //-----actionner textfield
                trechercher.addMouseListener(new MouseAdapter()
                {
                    public void mousePressed(MouseEvent e){

                        trechercher.setText("");

                }

                }); 

        //------Connection à la base de donneés
        trechercher.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e) {

                try{

                       //  Read data from a table
                       String sql = "SELECT * FROM impaye";
                       Statement stmt = connection.createStatement();
                       ResultSet rs = stmt.executeQuery(sql);
                       ResultSetMetaData md = rs.getMetaData();
                       int columns = md.getColumnCount();

                       //  Get column names 
                       for (int i = 1; i <= columns; i++)
                       {
                          titrecolonnes.addElement( md.getColumnName(i) );
                       }

                       //  Get row data
                       while (rs.next()) 
                       {
                          Vector row = new Vector(columns);
                          for (int i = 1; i <= columns; i++)
                          {
                             row.addElement(rs.getObject(i));
                          }
                          donnee.addElement(row);
                       }
                       rs.close();
                       stmt.close();
                }catch(Exception cnfe)
                {
                    System.out.println("Error:"+cnfe.getMessage());
                    }

                TableModel model = new DefaultTableModel (donnee, titrecolonnes)
                {
                public Class getColumnClass(int columnNames) {
                    Class returnValue;
                    if ((columnNames >= 0) && (columnNames < getColumnCount())) {
                      returnValue = getValueAt(0, columnNames).getClass();
                    } else {
                      returnValue = Object.class;
                    }
                    return returnValue;
                  }
                };
                table = new JTable (model) ; 
                sorter = new TableRowSorter<TableModel>(model) ; 
                JScrollPane scrollPane = new JScrollPane((table));
                table.setRowSorter (sorter) ; 
                getContentPane().setLayout(new GridLayout(1,1));
                getContentPane().add(scrollPane);

            }
        });


    }


}
trechercher.addKeyListener(new KeyAdapter()

不要使用 KeyListener。

JTextField被设计为与ActionListener一起使用来处理 Enter 键。

table = new JTable (model) ; 
sorter = new TableRowSorter<TableModel>(model) ;    
JScrollPane scrollPane = new JScrollPane((table));
table.setRowSorter (sorter) ; 
getContentPane().setLayout(new GridLayout(1,1));
getContentPane().add(scrollPane);

上面的代码正在创建要添加到 GUI 的新组件。 问题是所有组件的大小都是 (0, 0) 所以没有什么可绘制的。 当您将组件动态添加到可见 GUI 时,基本代码是:

panel.add(...);
panel.revalidate(); // invokes the layout manager
panel.repaint(); // repaints the components

然而,一个更简单的解决方案是在类的构造函数中将空表和滚动窗格添加到框架中。 然后,当您获得新数据时,您需要做的就是刷新表的TableModel ,该表将重新绘制自身。

那么你需要在你的 refresh 方法中做的就是:

table.setModel(model );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM