Slide 1
Slide 2
Slide 3

Text Field in java programming (V77)

No Comments

  JTextField

JTextField is a component that allows users to enter text in a single line.

Example:

Source Code:

Copied!
    import java.awt.Color;
    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String username = txtUser.getText();
        String password = new String(txtPass.getPassword()); // safer than getText()

    if (username.equals("admin") && password.equals("12345")) {
        lblMessage.setText("Login Successful!");
        lblMessage.setForeground(Color.GREEN);
    } else {
        lblMessage.setText("Invalid username or password.");
        lblMessage.setForeground(Color.RED);
    }        
    }                                        

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
         System.exit(0); 
    }                                                                                   

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top