chat messenger application system(lAN based)in java

introduction

A chat Messenger application system (LAN-based) provides a mild, safe and efficient way to facilitate real-time message within the local network environment. The project is created using Java (swing for Networking) and imitates a basic client-server chat application, allowing many users to communicate immediately through text messages to many users on the same local area network (LAN).

The system consists of two main components:

1.Server App:

  • Hears for the upcoming client connection and manages the message broadcast.

2.Client application:

  • Allows users to connect to the server, send messages and get real -time updates from other users. 

1. Major features: 

  • Real -time text message on lan Simple and user friendly gui using Java swing Server handles several client connections Each message includes the sender’s name and timestamp Original verification and status update

2.Use cases:

  •  Intra-office communication Class group discussion Lab Environment or Cyber Cafe Small-scale outfits without internet-based chat tool 

3. Use of technology

  • Java SE (Swing, AWT) Java socket (TCP) Multiple formula Local Area Network (LAN)

Scope for Future Enhancements:

  • Group Chat Rooms: Allow multiple users to chat in public or private rooms.

  • Login Authentication: Simple login screen for user verification.

  • File Sharing: Share documents or images over the LAN.

  • Emoji and formatting support: For better message expression.

  • Notification System: Pop-ups or sound alerts for new messages.

  • Dark Mode and UI Themes

steps to create chat messenger

Step 1: Create Server Project in NetBeans

  1. Open NetBeans.

  2. Create a Java Application Project called ChatServer.

  3. Add a Server.java file.

    1. Create another Java Application project called ChatClient.

    2. Add a Client.java file

    3. Start the Server:

        • Run ChatServer project.

        • It will wait for a client to connect.

      1. Run the Client:

        • Run ChatClient project.

        • Type a message and click Send.

        • Messages are exchanged over sockets

code explanation

Main Components and Structure
 1.Class Declaration
public class TwoWayColorfulChat extends JFrame
Inherits from JFrame to create a windowed application.
  • The GUI components are initialized in the constructor.

 2.Class Variables (GUI Elements)

private JTextArea chatAreaLeft, chatAreaRight;
private JTextArea messageAreaLeft, messageAreaRight;
private JButton sendButtonLeft, sendButtonRight;
  • Each user has:

    • chatArea: To display ongoing conversation.

    • messageArea: For typing new messages.

    • sendButton: To send the message.

3.Constructor: Setting Up the UI

Frame Properties

setTitle("Two-Way Colorful Chat Messenger");
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

4. Left Chat Panel (User 1)
  • Created using JPanel with BorderLayout.

  • Includes:

    • chatAreaLeft: Light Cyan background.

    • messageAreaLeft: Input field.

    • sendButtonLeft: Steel Blue send button.

  • Laid out with spacing and scrollbars.

5. Right Chat Panel (User 2)

  • Similar to the left panel but:

    • chatAreaRight: Lavender Blush background.

    • sendButtonRight: Hot Pink.

6. Split Pane

 
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
Allows resizable split between User 1 and User 2 sections.

7.GUI Decoration: addShapesBackground()

This adds gradient overlays and animated shapes to the background using a custom glass pane:

JComponent glass = new JComponent() {
@Override
protected void paintComponent(Graphics g) {
...
}
};
  • Draws:

    • Gradient background using GradientPaint.

    • Semi-transparent pink circles and blue rectangles.

  • Adds an aesthetic and modern feel to the UI.

8.Event Handling & Messaging Logic

Sending Messages

sendButtonLeft.addActionListener(e -> {
sendMessage(messageAreaLeft, chatAreaLeft, chatAreaRight, "User 1");
});
sendButtonRight.addActionListener(e -> {
sendMessage(messageAreaRight, chatAreaRight, chatAreaLeft, "User 2");
});
  • When user clicks Send, the message from their input area is:

    • Appended to both sender and receiver chat areas.

    • Input is cleared.

    • Scroll automatically moves to the latest message.

9.Enter Key Handling

messageAreaLeft.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && !e.isShiftDown()) {
e.consume();
sendButtonLeft.doClick();
}
}
});
  • Pressing Enter sends the message.

  • Pressing Shift + Enter allows a line break (multi-line message)

10.Main Method

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TwoWayColorfulChat chat = new TwoWayColorfulChat();
chat.setVisible(true);
});
}
  • Starts the GUI on the Event Dispatch Thread (EDT) for thread safety.

source code

				
					import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TwoWayColorfulChat extends JFrame {

    private JTextArea chatAreaLeft, chatAreaRight;
    private JTextArea messageAreaLeft, messageAreaRight;
    private JButton sendButtonLeft, sendButtonRight;

    public TwoWayColorfulChat() {
        setTitle("Two-Way Colorful Chat Messenger");
        setSize(900, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Use BorderLayout for main frame
        setLayout(new BorderLayout());

        // Create left chat panel (User 1)
        JPanel leftPanel = new JPanel(new BorderLayout(5, 5));
        leftPanel.setBorder(BorderFactory.createTitledBorder("User 1"));
        chatAreaLeft = createChatArea(new Color(240, 255, 255));  // Light Cyan background
        messageAreaLeft = createMessageArea();
        sendButtonLeft = createSendButton(new Color(70, 130, 180)); // Steel Blue

        JScrollPane scrollPaneLeft = new JScrollPane(chatAreaLeft);
        JScrollPane messageScrollLeft = new JScrollPane(messageAreaLeft);

        JPanel inputPanelLeft = new JPanel(new BorderLayout(5, 5));
        inputPanelLeft.add(messageScrollLeft, BorderLayout.CENTER);
        inputPanelLeft.add(sendButtonLeft, BorderLayout.EAST);

        leftPanel.add(scrollPaneLeft, BorderLayout.CENTER);
        leftPanel.add(inputPanelLeft, BorderLayout.SOUTH);

        // Create right chat panel (User 2)
        JPanel rightPanel = new JPanel(new BorderLayout(5, 5));
        rightPanel.setBorder(BorderFactory.createTitledBorder("User 2"));
        chatAreaRight = createChatArea(new Color(255, 240, 245)); // Lavender Blush background
        messageAreaRight = createMessageArea();
        sendButtonRight = createSendButton(new Color(255, 105, 180)); // Hot Pink

        JScrollPane scrollPaneRight = new JScrollPane(chatAreaRight);
        JScrollPane messageScrollRight = new JScrollPane(messageAreaRight);

        JPanel inputPanelRight = new JPanel(new BorderLayout(5, 5));
        inputPanelRight.add(messageScrollRight, BorderLayout.CENTER);
        inputPanelRight.add(sendButtonRight, BorderLayout.EAST);

        rightPanel.add(scrollPaneRight, BorderLayout.CENTER);
        rightPanel.add(inputPanelRight, BorderLayout.SOUTH);

        // Split pane to separate both chat areas
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        splitPane.setDividerLocation(450);
        add(splitPane, BorderLayout.CENTER);

        // Add background decoration (over the JFrame's layered pane)
        addShapesBackground();

        // Action for left send button
        sendButtonLeft.addActionListener(e -> {
            sendMessage(messageAreaLeft, chatAreaLeft, chatAreaRight, "User 1");
        });

        // Action for right send button
        sendButtonRight.addActionListener(e -> {
            sendMessage(messageAreaRight, chatAreaRight, chatAreaLeft, "User 2");
        });

        // Enter key press for left input area
        messageAreaLeft.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER && !e.isShiftDown()) {
                    e.consume();
                    sendButtonLeft.doClick();
                }
            }
        });

        // Enter key press for right input area
        messageAreaRight.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER && !e.isShiftDown()) {
                    e.consume();
                    sendButtonRight.doClick();
                }
            }
        });
    }

    // Helper to create chat display areas
    private JTextArea createChatArea(Color bgColor) {
        JTextArea ta = new JTextArea();
        ta.setEditable(false);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        ta.setFont(new Font("Segoe UI", Font.PLAIN, 14));
        ta.setBackground(bgColor);
        ta.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        return ta;
    }

    // Helper to create message input areas
    private JTextArea createMessageArea() {
        JTextArea ta = new JTextArea(3, 20);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        ta.setFont(new Font("Segoe UI", Font.PLAIN, 14));
        ta.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        return ta;
    }

    // Helper to create buttons
    private JButton createSendButton(Color bgColor) {
        JButton btn = new JButton("Send");
        btn.setBackground(bgColor);
        btn.setForeground(Color.WHITE);
        btn.setFocusPainted(false);
        btn.setFont(new Font("Segoe UI", Font.BOLD, 14));
        return btn;
    }

    // Method to send message from one user to the other
    private void sendMessage(JTextArea inputArea, JTextArea senderChat, JTextArea receiverChat, String user) {
        String message = inputArea.getText().trim();
        if (!message.isEmpty()) {
            String formattedMessage = user + ": " + message + "\n";
            senderChat.append(formattedMessage);
            receiverChat.append(formattedMessage);
            inputArea.setText("");
            senderChat.setCaretPosition(senderChat.getDocument().getLength());
            receiverChat.setCaretPosition(receiverChat.getDocument().getLength());
        }
    }

    // Add colorful shapes in the background using glass pane overlay
    private void addShapesBackground() {
        JComponent glass = new JComponent() {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                int w = getWidth();
                int h = getHeight();

                // Transparent gradient overlay
                GradientPaint gp = new GradientPaint(0, 0, new Color(255, 192, 203, 80),
                        w, h, new Color(135, 206, 250, 80));
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, w, h);

                // Draw colorful circles
                g2d.setColor(new Color(255, 105, 180, 60));
                for (int i = 0; i < 6; i++) {
                    int size = 120 + i * 30;
                    g2d.fillOval(50 * i, 30 * i, size, size);
                }

                // Draw colorful rectangles
                g2d.setColor(new Color(30, 144, 255, 60));
                for (int i = 0; i  {
            TwoWayColorfulChat chat = new TwoWayColorfulChat();
            chat.setVisible(true);
        });
    }
}

				
			

output

More java Pojects
Get Huge Discounts