please how do i create a login page using gui in java?And also how do i make the login page move to the next java page when the password is correct?

Dani AI

Generated

As suggested, the right flow is: validate input, authenticate, then show the next view. Practical, implementable tips not in the thread: build the GUI on the Swing event dispatch thread (use SwingUtilities.invokeLater), use JPasswordField (read the char[] and clear it after use), load images with Class.getResource(...) so the image works inside a JAR, and prefer CardLayout to swap panels inside one JFrame rather than spawning many frames. For database checks always use PreparedStatement and never store plaintext passwords — use a modern hashing scheme (bcrypt/PBKDF2).

A compact example showing image loading, a JPasswordField, and switching cards:

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class LoginDemo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Login");
            CardLayout cl = new CardLayout();
            JPanel cards = new JPanel(cl);

            JPanel login = new JPanel();
            JLabel logo = new JLabel(new ImageIcon(LoginDemo.class.getResource("/images/logo.png")));
            JTextField user = new JTextField(10);
            JPasswordField pass = new JPasswordField(10);
            JButton loginBtn = new JButton("Login");

            loginBtn.addActionListener(e -> {
                char[] entered = pass.getPassword();
                char[] expected = "secret".toCharArray(); // placeholder; use hashed check in real app
                if (Arrays.equals(entered, expected)) {
                    Arrays.fill(entered, '0');
                    cl.show(cards, "MAIN");
                } else {
                    Arrays.fill(entered, '0');
                    JOptionPane.showMessageDialog(frame, "Invalid login");
                }
            });

            login.add(logo);
            login.add(new JLabel("User:")); login.add(user);
            login.add(new JLabel("Pass:")); login.add(pass);
            login.add(loginBtn);

            JPanel main = new JPanel(); main.add(new JLabel("Welcome"));
            cards.add(login, "LOGIN"); cards.add(main, "MAIN");

            frame.add(cards);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

Troubleshooting and best practices: getResource(...) returns null if the path is wrong — place images on the classpath (e.g. src/main/resources/images/logo.png) and use /images/logo.png. Scale icons with getScaledInstance if needed. For DB authentication use parameterized queries and a server-side salted hash (see OWASP Authentication Cheat Sheet). For Swing basics consult the official tutorial: The Java Tutorials: Creating a GUI With Swing.

Recommended Answers

All 2 Replies

how do i also put a picture on the login page in java?

  1. Create frame with some labels, text fields, submit button and add what ever image you wish
  2. On submit button run validation if any data entered at all, if they fulfil criteria set for each entry (minimum and maximum length, use of allowed/forbidden characters, case sensitivity etc.)
    • If any fails display error message
    • If all OK, proceed with following steps
  3. Run SQL query to check if user exists and if given password is correct
    • If incorrect display error message and get back to loging frame/screen
    • If username and password match with DB entries proceed with following
  4. Show next frame with what ever information should follow
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.