Tuesday, 10 June 2014

Enabling jButton if jTextfield is not empty - Java

Let's think you designed a Text field and a Button, When you write something on text filed, button will visible or if you clear the data on text field button will hide again.

First create 2 components (Text field and Button).
You have to choose the correct event to write your code. Button will visible when a key pressed, avoid ActionPerformed event (Action Performed will get both click and key pressed and etc.)

Think before code:
                            if (key pressed) Text field equals(=) empty
                            Button won't enable.
                            else (key pressed) Button will enable. (Text field is not empty)

Now let's convert it to code:
                           private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
                           if (jTextField1.getText().equals("")){
                                   jButton1.setEnabled(false);
                          }else{
                                   jButton1.setEnabled(true);
                                  }
                          } 

First we need to get the text from the text field and check whether it's empty or not. Now you can enable the button or hide.

Happy coding guys!



1 comment:

  1. How to get to Borgata via mobile in NJ by Bus?
    Directions to Borgata via mobile — This means that the following transit 밀양 출장마사지 lines 영천 출장샵 will 익산 출장안마 be 창원 출장마사지 provided for the bus. The following transit lines 파주 출장마사지

    ReplyDelete