Sunday, November 27, 2011

Java search bar - Click event and Enter key event (Netbeans)

Create a search bar in a java application using netbeans, configuring click event listener and ENTER key event listener in the jTextField.







Code for button click event listener and key listener in jTextField:

        jButton1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jLabel1.setText("You searched for: "+jTextField1.getText());
            }
        });
        
        jTextField1.addKeyListener(new KeyAdapter()
        {       
            @Override
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode() == KeyEvent.VK_ENTER)
                    jLabel1.setText("You searched for: "+jTextField1.getText());
            }
        })



No comments:

Post a Comment