【问题标题】:Jtextfield search in database msql using keylistener使用keylistener在数据库msql中搜索Jtextfield
【发布时间】:2015-10-24 15:47:58
【问题描述】:

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

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

            }
        });


    }


}

【问题讨论】:

  • 嗨@Recay,欢迎来到 StackOverflow!你的问题并不完全清楚。您是否尝试根据当前用户输入从数据库中提供搜索建议?

标签: java mysql swing


【解决方案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

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

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

table.setModel(model );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多