Java swing show form

Login Form in Java Swing With Source Code Tutorial

Hello Friends, Today we will learn how to make a simple Login Form in Java Swing With Source Code. A login form or login page is the core functionality of any application. It is usually used to check the authenticity of the user. If the user gives the valid username and password then he/she can log into the system otherwise the system will deny accepting the user’s login request. To create our Gui Login Form or Login Page in Java Swing, we must have good concepts about fundamental components of Java Swing. Below I am providing you all the topics that are required to build our GUI Login Form in Java or we can say as Java code for Login page with Username and Password.

If you are done with the above topics, then we can start our tutorial login form in Java swing with Source Code .

Watch Video Tutorial

Login Form in Java Swing With Source Code | Login Page

Importing Packages

  • To create our Login Page using Java, In the very first step, we need to import all the necessary packages for our program so that we will be able to use all the interfaces and classes that we will use in our program.

Creating a Class LoginFrame.java

  • In this step, we will create a class (LoginFrame.java) in this example that will both extend the JFrame class and implements the ActionListener interface.
  • We are going to implement the ActionListener interface because we will do some click events in our program. ActionListener interface resides in java.awt.event.ActionListener package so, first of all, we have to import this package into our class which we have already done.
  • ActionListener contains only one method actionPerformed(ActionEvent e), so if we are implementing the ActionListener interface in any class, then we have to override its method actionPerformed(ActionEvent e) into that class.
  • Next, we will create the constructor of the LoginFrame class.

Creating Object of LoginFrame class

  • Now we will create another class(Login.java in this example) where our main method will reside.
  • Now inside our main method, we will create the object of LoginFrame class and set some of its properties like its title, size and location, default close operation, and its visibility.

Login Form in Java Swing With Source Code

  • Now save your program compile it and run it.
  • You can see that we have created our JFrame. Now we can add components to it.

Setting Layout Manager of JFrame

  • Now inside our LoginFrame class, we will get the content pane of the frame using the getContenPane() method.
  • getContentPane() returns a Container reference so we will create a reference to the container.
  • Now we are going to set the layout manager of the Container.For this, we are going to create a user-defined method setLayoutManager.
  • By default, a content pane uses BorderLayout If we do not like the default layout manager that a content pane uses then we are free to change it to a different one.
  • To set the layout manager to null, we have to call the setLayout() method using the object of Container and we will pass null as its argument.By doing this, we are setting the Layout manager of Container to null.
Читайте также:  Javascript кнопка показать еще

Adding Components to our JFrame

  • Since we have already created our JFrame, now, we will design our login page in Java by adding components to it.
  • To add components to it, first of all, we have to create the objects of all components.
  • We are going to create,
    • Two JLabel USERNAME and PASSWORD.
    • One JTextField to enter username one JPasswordField to enter the password
    • Two JButon LOGIN and RESET.
    • One JCheckBox showPassword.

    • Now save your program, compile it and run it.
    • Now we can see that we have successfully added components to JFrame.
    • Now the next thing we have to do is to add functionalities to our JButtons and JCheckBox so that when we click on them, they should perform some actions.

    Adding Event Handling to JButtons(LOGIN,RESET) and JCheckBox(showPassword)

    • To add event handling to our JButtons and JCheckBox first thing we have to do is to register(add) actionListener to them.
    • For this, we will call the addActionListener() method using the object of the desired component.The parameter of the addActionListener() method is the object of that class in which ActionListener interface is implemented and since we are in the same class so we will pass this as the argument of the addActionListener() method.
    • By registering the component, the actionPerformed() method will be called whenever we click on any of the registered components.
    • Now we will sum up all the things inside a user-defined method addActionEvent().
    • The next thing we have to do is to code the desired actions that we want in response whenever we click on the Jbutton or JcheckBox inside actionPerformed() method.

    actionPerformed() method

    • If we enter username as “mehtab” and password as “12345” and click on the LOGIN button then a message dialog box should appear and display the message “Login Successful” and if we enter different username or password then message dialog box should display the message “Invalid Username or Password”.For message dialog box we will use showMessageDialog() method of JOptionPane class.
    • If we click on the RESET button, then all the texts of JTextField and JPasswordField should be cleared.
    • If we click on the showPassword JCheckBox, then the secret password should be revealed, and if we uncheck the showPassword JCheckBox, then all the texts of the JPasswordField should be masked with “*” characters.
    • Now you can save your program, compile it and run it.
    • Now let’s provide username as “mehtab” and password as “12345” and see what happens.

    • As we can see the message dialog box has displayed the message “Login Successful”.
    • Now let’s enter other username or password.

    • As we can see message dialog box has displayed the message “Invalid Username or Password”.
    • Now let’s check the JCheckBox showPassword and see what happens.

    • As we can see the secret password has been revealed.
    • You can also check the RESET button and also uncheck the JCheckBox and it will work fine.

    Login Form in Java Swing With Source Code Download

    • You can also download the source code of this login system in Java.
    • The Download Link of the login page code in Java file is given below..

    So this was all for Login Form in Java swing with Source Code tutorial. Feel free to ask if you have any queries regarding this tutorial By commenting on this post. Thank You.

    • How to Create Multi User Login Form in Java using MySQL Database
    • How to Play Mp3 File in Java Tutorial | Simple Steps
    • Number Guessing Game in Java Swing with Source Code
    • Menu Driven Program in Java Using Switch Case
    • Calculator Program in Java Swing/JFrame with Source Code
    • Registration Form in Java With Database Connectivity
    • Tic Tac Toe Game in Java with Source Code
    • Text to Speech in Java
    • How to Create Splash Screen in Java
    • Java Button Click Event
    • Best Laptops for Java Programming
    • How to Connecty MySQL Database in Java Using Eclipse
    • How to Connect MySQL Database in Java using NetBeans
    • How to Fetch Data from Database in Java to JTable
    • Why Pointer are not used in Java?

    13 thoughts on “Login Form in Java Swing With Source Code Tutorial”

    how about using the username and password from ms access data base from your last tutorial about user registration. how does the coding of such?thanks a lot..

    Источник

    How to create a simple Java form with Swing?

    I’m very new to swing and have trouble dealing with JFrame s so I thought you guys could help. I’m trying to make a simple window show up with 3 text fields and a check box. At the bottom there should be a «done» button which closes the form. I can make the JFrame with the JTextField s and JCheckBox but how do I retrieve the input? Thanks in advance

    Consider using a JDialog instead of a JFrame . Anything that ends «At the bottom there should be a «done» button which closes the form» strongly suggests a dialog to me.

    3 Answers 3

    As stated in the Swing tutorial, you can add an ActionListener to the JButton , which will be called when the button has been pressed.

    To retrieve the text from the JTextField , use the JTextField#getText() method

    To determine whether the JCheckBox is actually selected, use the JCheckBox#isSelected() method

    But a good starting point is reading the Swing tutorial from the start

    Thanks, but how do I get the checkbox input? Is there any way to get it as a boolean? I literally just learned how to make checkboxes and know nothing about them

    Thanks! I’ll check out that link to find out how to add an actionlistener to the button and i should be good to go!

    It would be helpful if you would post some code showing how you show your JFrame , then I could give you a more specific example.

    In general, you’ll have a class that extends JFrame , JDialog , etc. In that class you’ll have getters and setters that will get and set the values of the controls on the form.

    In your case, once «Done» is clicked you could have a listener, either on the «Done» button or on the frame itself (listening for the closing event) retrieve the values from your form and do something with them.

    If this isn’t clear please post some code and perhaps I can give you a concrete example.

    Источник

    Java GUI: How do I open an existing Form?

    I’m trying to reach an existing form that I made from another form. I want to click a button, and make the other form appear. Here’s what I wrote:

    import javax.swing.JFrame; public class CustomerUI extends JFrame < public CustomerUI(java.awt.Frame parent, boolean modal) < initComponents(); setTitle("Customer Data Input"); setLocationRelativeTo(null); pack(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setVisible(true); >@SuppressWarnings("unchecked") private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) < Customer c = new Customer(jTextField1.getText(), jTextField2.getText(), jTextField3.getText()); CinemaUI form = new CinemaUI(); form.CUST.SetName(jTextField1.getText()); //Customer's Name form.CUST.SetID(jTextField2.getText()); //Customer's ID form.CUST.SetCard(jTextField3.getText()); //Customer's card form.NUM_OF_SEATS = Integer.parseInt(jTextField4.getText()); //Number of seats form.pack(); //Shows the next Form form.setVisible(true); CustomerUI.this.setVisible(false); //Hides this Form >/** * @param args the command line arguments */ public static void main(String args[]) < /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() < public void run() < CustomerUI dialog = new CustomerUI(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() < @Override public void windowClosing(java.awt.event.WindowEvent e) < System.exit(0); >>); dialog.setVisible(true); > >); > > 

    CinemaUI is the name of the form I’d like to open. After clicking jButton1 (the one I want to open the new window) I get the following error:

    Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet. at my.Cinema.CinemaUI.(CinemaUI.java:30) at my.Cinema.CustomerUI.jButton1ActionPerformed(CustomerUI.java:126) at my.Cinema.CustomerUI.access$100(CustomerUI.java:11) at my.Cinema.CustomerUI$2.actionPerformed(CustomerUI.java:60) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

    Edit:
    I’ll try explaining what my objective is: A certain person wants to order tickets to a movie. I save his data (Name, credit card, etc.) using textboxes, and proceed to selecting the person’s seats. The first window I’m looking at is the one with the textboxes, and after I click a button I want the Window that shows a list of available seats to show up ( CinemaUI ). Thanks again for the help, I’m sorry its so long.

    Источник

Оцените статью