使用keylistener在数据库msql中搜索Jtextfield

时间:2015-10-24 15:47:59

标签: java mysql swing

当我写信时,我正在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);

            }
        });


    }


}

1 个答案:

答案 0 :(得分:1)

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

但是,更简单的解决方案是将空表和scrollpane添加到类的构造函数中的框架。然后,当您获得新数据时,您需要刷新表格的TableModel,表格将重新绘制。

然后,您需要在刷新方法中执行的操作是:

table.setModel(model );