JSwing simple login application

In the previous Simple login Form, you should have created a JSwing login Form, In this tutorial, we will continue to create a JSwing App that can open the JSwing Form and start the JSwing applications.

You will learn how to:

  • Create a JSwing App in NetBeans.
  • Add event handlers for JButtons.
  • Code parser.
  • Run JSwing Application.

 

Prerequisites

This tutorial builds on the previous tutorials, Quick Start Guide and JSwing simple login form. If you haven't done those tutorials, go through them first.

 

Create a JSwing App

  1. Open Apache NetBeans.
  2. Click JSwingApplication,Select File > New File > select Swing GUI Forms in the Categories, JFrame Form in the File Types, and then click Next.
  3. In the Name and Location window, enter Login in the Class Name, change Location from default to Source Packages, package to form, and then click Finish.
  4. Add code to the JSwing App.
package app;
import jswing.application.*;

public class Login extends App {
    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Form.open("form.Login","Login");
                UserInterface.initialize("app.Login",Form.getCurrentForm());
            }
        });
    } 
}

 

Add event handlers for JButtons

An event handler is a process that is called when the user presses a button. JSwing applications use actionCommand to verify which method should be executed.

  1. Insert actionPerformed after method main.
  2. Add code to the actionPerformed as the following.
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e){
        String action = e.getActionCommand();
        switch(action){
            case "login":
                ((javax.swing.JLabel) Form.getCurrentForm().getComponentByName("message")).setText("Faile to login");
                ((javax.swing.JLabel) Form.getCurrentForm().getComponentByName("message")).setVisible(true);
                break;
            case "reset":
                ((javax.swing.JTextField) Form.getCurrentForm().getComponentByName("user_text")).setText("");
                ((javax.swing.JTextField) Form.getCurrentForm().getComponentByName("pwd_text")).setText("");
                break;
            case "cancel":
                System.exit(0);
                break;
        }
    }

 

Compile and run the JSwing Application

Click Run > Run File or Shift+F6, and you will get the result. Try to press a button, and you will get a response from the event handler, download source code for your practice.

Simple Login

 

Next steps

Congratulations! you've completed an application with the JSwing library. Now you can download JSwing and get started, or read more.

Share this page

Let more people know about this site, please share