Phone Book in Java

Phone Book in Java

In this article, we will build a Phone Book in Java with MySQL. The GUI is designed with the help of Swing and the database connectivity is managed using JDBC API. This application can help anyone to maintain and manage their contacts.

Features

The user can log in to manage the inventory using the credential root as username and root as password. The manager has access to the following:

  • View all contacts detail
  • Search with first name, last name, country, and phone number
  • Edit a contact entry
  • Delete a contact entry

Setup development environment

  • Open NetBeans or another IDE and create a new Java project with the name ADDRESSBOOK. Now create a new package with the name addressbook. Inside the addresbook package, we will make all the Java files.
  • Make sure you have a MySQL server running using xampp, if you are using any other service you can change the port number in the DB connection file to make it work.
  • Download the MySQL Connector jar file. Now we need to add the dependencies of the project.

Source code

Expand the project and right-click on libraries, click on add jar/folder, browse the jar file you have downloaded from above, and add it here. Also, we have used address book or contact book, or phone book, we have considered all of them the same so please don’t confuse.

Project Structure

project structure

Before you begin, make sure the process for MySQL is running

xampp

MySQL setup for Phone Book in Java

To set up MySQL for address book in java, open PHP My Admin and hit the plus icon and go to the SQL page and then run the following commands. These commands will create a new database with the name addressbook and the table contact with example information.

CREATE DATABASE `addressbook`;
USE `addressbook`;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
 
CREATE TABLE `Contacts` (
  `cid` int(11) NOT NULL,
  `firstName` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  `location` varchar(45) DEFAULT NULL,
  `phone` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
 
INSERT INTO `Contacts` (`cid`, `firstName`, `lastname`, `location`, `phone`) VALUES
(307, 'Aman', 'Raj', 'Patna', '7878787878');
 
ALTER TABLE `Contacts`
  ADD PRIMARY KEY (`cid`);
 
 
ALTER TABLE `Contacts`
  MODIFY `cid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=308;
COMMIT;

Coding Phone Book in Java

ConnectionFactory.java

Here, we create the ConnectionFactory.java class, which is used to connect to MySQL Database and in all other java classes, we use this class’s object to perform the database operations in address book in java. Create a file named ConnectionFactory.java. In this file, you have to keep the credentials of your MySQL server account.

package com.addressbook.Database;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
 
/**
 *
 * @author aman
 */
 
//Class to retrieve connection for database and login verfication.
public class ConnectionFactory {
 
    static final String driver = "com.mysql.cj.jdbc.Driver";
    static final String url = "jdbc:mysql://localhost:3306/addressbook";
    static String username = "root";
    static String password = "";
 
    Properties prop;
 
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
 
    public ConnectionFactory(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            statement = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public Connection getConn() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Connected successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}

ContactDAO module

This file mainly consists of the logic associated with the customers for doing operations on the database. Create a new file and name it ContactDAO.java.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressbook.DAO;
 
import com.addressbook.DTO.ContactDTO;
import com.addressbook.Database.ConnectionFactory;
 
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Locale;
import java.util.Vector;
 
/**
 *
 * @author aman
 */
 
// Data Access Object for Contacts
public class ContactDAO {
    Connection conn = null;
    PreparedStatement prepStatement= null;
    Statement statement = null;
    ResultSet resultSet = null;
 
    public ContactDAO() {
        try {
            conn = new ConnectionFactory().getConn();
            statement = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // Methods to add new custoemr
    public void addContactDAO(ContactDTO ContactDTO) {
        try {
            String query = "SELECT * FROM Contacts WHERE lastname='"
                    +ContactDTO.getlastname()
                    + "' AND location='"
                    +ContactDTO.getLocation()
                    + "' AND phone='"
                    +ContactDTO.getPhone()
                    + "'";
            resultSet = statement.executeQuery(query);
            if (resultSet.next())
                JOptionPane.showMessageDialog(null, "Contact already exists.");
            else
                addFunction(ContactDTO);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void addFunction(ContactDTO ContactDTO) {
        try {
            String query = "INSERT INTO Contacts VALUES(null,?,?,?,?)";
            prepStatement = conn.prepareStatement(query);
            prepStatement.setString(1, ContactDTO.getCustCode());
            prepStatement.setString(2, ContactDTO.getlastname());
            prepStatement.setString(3, ContactDTO.getLocation());
            prepStatement.setString(4, ContactDTO.getPhone());
            prepStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "New Contact has been added.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
    }
 
    // Method to edit existing Contact details
    public  void editContactDAO(ContactDTO ContactDTO) {
        try {
            String query = "UPDATE Contacts SET lastname=?,location=?,phone=? WHERE firstName=?";
            prepStatement = conn.prepareStatement(query);
            prepStatement.setString(1, ContactDTO.getlastname());
            prepStatement.setString(2, ContactDTO.getLocation());
            prepStatement.setString(3, ContactDTO.getPhone());
            prepStatement.setString(4, ContactDTO.getCustCode());
            prepStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "Contact details have been updated.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // Method to delete existing Contact
    public void deleteContactDAO(String custCode) {
        try {
            String query = "DELETE FROM Contacts WHERE firstName='" +custCode+ "'";
            statement.executeUpdate(query);
            JOptionPane.showMessageDialog(null, "Contact removed.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // Method to retrieve data set to be displayed
    public ResultSet getQueryResult() {
        try {
            String query = "SELECT firstName,lastname,location,phone FROM Contacts";
            resultSet = statement.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
 
    // Method to retrieve search data
    public ResultSet getContactSearch(String text) {
        try {
            String query = "SELECT firstName,lastname,location,phone FROM Contacts " +
                    "WHERE firstName LIKE '%"+text+"%' OR lastname LIKE '%"+text+"%' OR " +
                    "location LIKE '%"+text+"%' OR phone LIKE '%"+text+"%'";
            resultSet = statement.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
 
    public ResultSet getCustName(String custCode) {
        try {
            String query = "SELECT * FROM Contacts WHERE firstName='" +custCode+ "'";
            resultSet = statement.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
 
    public ResultSet getProdName(String prodCode) {
        try {
            String query = "SELECT productname,currentstock.quantity FROM products " +
                    "INNER JOIN currentstock ON products.productcode=currentstock.productcode " +
                    "WHERE currentstock.productcode='" +prodCode+ "'";
            resultSet = statement.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
 
    // Method to display data set in tabular form
    public DefaultTableModel buildTableModel(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();
        Vector<String> columnNames = new Vector<String>();
        int colCount = metaData.getColumnCount();
 
        for (int col=1; col <= colCount; col++){
            columnNames.add(metaData.getColumnName(col).toUpperCase(Locale.ROOT));
        }
 
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (resultSet.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int col=1; col<=colCount; col++) {
                vector.add(resultSet.getObject(col));
            }
            data.add(vector);
        }
        return new DefaultTableModel(data, columnNames);
    }
 
}

ContactDTO module

This file mainly consists of the logic for doing operations on the database in the address book in java. Create a new file and name it ContactDTO.java.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressbook;
 
/**
 *
 * @author aman
 */
 
// Data Transfer Object (DTO) class for Contacts
 
public class ContactDTO {
 
    private int custID;
    private String custCode, lastname, location, phone;
    public int getCustID() {
        return custID;
    }
 
    public void setCustID(int custID) {
        this.custID = custID;
    }
 
    public String getCustCode() {
        return custCode;
    }
 
    public void setCustCode(String custCode) {
        this.custCode = custCode;
    }
 
    public String getlastname() {
        return lastname;
    }
 
    public void setlastname(String lastname) {
        this.lastname = lastname;
    }
 
    public String getLocation() {
        return location;
    }
 
    public void setLocation(String location) {
        this.location = location;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

LoginPage

This file of the Phone Book in Java is used to create the GUI for the login page where we have a username and password field to take the user input. Create a file and name it LoginPage.java. Here we can also set a custom password if we don’t want the default one.

 
package com.addressbook;
 
import java.time.LocalDateTime;
import javax.swing.*;
 
public class LoginPage extends javax.swing.JFrame {
 
    LocalDateTime inTime;
 
    public LoginPage() {
        initComponents();
    }
 
    private void initComponents() {
 
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userText = new javax.swing.JTextField();
        passText = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setBackground(new java.awt.Color(102, 102, 102));
        setBounds(new java.awt.Rectangle(500, 100, 0, 0));
        setName("loginFrame"); // NOI18N
 
        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jLabel1.setText("Username:");
 
        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jLabel2.setText("Password:");
 
        jLabel3.setFont(new java.awt.Font("Poor Richard", 1, 24)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("PHONE BOOK");
 
        loginButton.setText("LOGIN");
        loginButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(userText, javax.swing.GroupLayout.DEFAULT_SIZE, 204,
                                                        Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(passText)))
                                .addContainerGap(69, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                                .createSequentialGroup()
                                                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(149, 149, 149))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                layout.createSequentialGroup()
                                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                284, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(57, 57, 57)))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(userText, javax.swing.GroupLayout.DEFAULT_SIZE, 31,
                                                Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(passText, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(1, 1, 1)))
                                .addGap(18, 18, 18)
                                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(44, Short.MAX_VALUE)));
 
        pack();
    }
 
    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_loginButtonActionPerformed
        String username = userText.getText();
        String password = passText.getText();
        if (username.equals("root") && password.equals("root")) {
            JOptionPane.showMessageDialog(null, "Login Successful");
            new Dashboard(username, "ADMINISTRATOR");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(
                    null,
                    "Invalid username or password.");
        }
 
    }
 
    public static void main(String[] args) {
        new LoginPage().setVisible(true);
    }
 
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton loginButton;
    private javax.swing.JPasswordField passText;
    private javax.swing.JTextField userText;
}
login screen

Dashboard

This file is used to create the GUI for the dashboard for the phone book in java with the help of Swing components. Name it Dashboard.java.

 
package com.addressbook;
 
import java.awt.CardLayout;
import java.time.LocalDateTime;
 
public class Dashboard extends javax.swing.JFrame {
 
    CardLayout layout;
    String userSelect;
    String username;
    String lastname;
    LocalDateTime outTime;
 
    public Dashboard(String username, String userType) {
        initComponents();
        layout = new CardLayout();
        userSelect = userType;
        this.username = username;
 
        displayPanel.setLayout(layout);
        displayPanel.add("Contacts", new ContactPage());
        setTitle("PHONE BOOK");
        setVisible(true);
    }
 
    private void initComponents() {
 
        mainPanel = new javax.swing.JPanel();
        displayPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PHONE BOOK");
        setBounds(new java.awt.Rectangle(400, 100, 0, 0));
 
        displayPanel.setLayout(new java.awt.CardLayout());
 
        jLabel1.setFont(new java.awt.Font("Gadugi", 1, 24)); // NOI18N
        jLabel1.setText("PHONE BOOK");
 
        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(0, 20, Short.MAX_VALUE)
                                .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 822,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(335, 335, 335)
                                .addComponent(jLabel1)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 501,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
 
        setJMenuBar(jMenuBar1);
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
 
        pack();
    }
 
    private javax.swing.JPanel displayPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel mainPanel;
}

ContactPage

In this file, we write the GUI for the contact page. Here, we have a screen to add, edit, and delete entries in the phone book. Name it ContactPage.java.

package com.addressbook;
 
import java.time.LocalDateTime;
import javax.swing.*;
 
public class LoginPage extends javax.swing.JFrame {
 
    LocalDateTime inTime;
 
    public LoginPage() {
        initComponents();
    }
 
    private void initComponents() {
 
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userText = new javax.swing.JTextField();
        passText = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setBackground(new java.awt.Color(102, 102, 102));
        setBounds(new java.awt.Rectangle(500, 100, 0, 0));
        setName("loginFrame"); // NOI18N
 
        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jLabel1.setText("Username:");
 
        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jLabel2.setText("Password:");
 
        jLabel3.setFont(new java.awt.Font("Poor Richard", 1, 24)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("PHONE BOOK");
 
        loginButton.setText("LOGIN");
        loginButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(userText, javax.swing.GroupLayout.DEFAULT_SIZE, 204,
                                                        Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(passText)))
                                .addContainerGap(69, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                                .createSequentialGroup()
                                                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(149, 149, 149))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                layout.createSequentialGroup()
                                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                284, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(57, 57, 57)))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(userText, javax.swing.GroupLayout.DEFAULT_SIZE, 31,
                                                Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(passText, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(1, 1, 1)))
                                .addGap(18, 18, 18)
                                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(44, Short.MAX_VALUE)));
 
        pack();
    }
 
    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_loginButtonActionPerformed
        String username = userText.getText();
        String password = passText.getText();
        if (username.equals("root") && password.equals("root")) {
            JOptionPane.showMessageDialog(null, "Login Successful");
            new Dashboard(username, "ADMINISTRATOR");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(
                    null,
                    "Invalid username or password.");
        }
 
    }
 
    public static void main(String[] args) {
        new LoginPage().setVisible(true);
    }
 
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton loginButton;
    private javax.swing.JPasswordField passText;
    private javax.swing.JTextField userText;
}

Output for Phone book in Java

Image output:

output for phone book in java

Video output:

Thank you for visiting our website.


Also Read:

Share:
Avatar of Aman Raj

Author: Aman Raj

Leave a Reply

Your email address will not be published. Required fields are marked *