0% found this document useful (0 votes)
199 views5 pages

Simple TCP Chat GUI in Java

A java swing gui project for the front end of a TCP/IP chat box. This has various features such as radioButton,ButtonGroup , etc. This project was submitted as part of 15SE205J java programming.

Uploaded by

Akanksha Sonkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views5 pages

Simple TCP Chat GUI in Java

A java swing gui project for the front end of a TCP/IP chat box. This has various features such as radioButton,ButtonGroup , etc. This project was submitted as part of 15SE205J java programming.

Uploaded by

Akanksha Sonkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA GUI PROGRAMMING

ASSIGNMENT


A program for simple TCP Chat














Made by –
Akanksha Sonkar
RA1711020010159
Subject Code-15SE205J
CODE-
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class Gui {
public static JFrame mainFrame = null;
public static JTextArea chatText = null;
public static JTextField chatLine = null;
public static JLabel statusBar = null;
public static JTextField ipField = null;
public static JTextField portField = null;
public static JRadioButton hostOption = null;
public static JRadioButton guestOption = null;
public static JButton connectButton = null;
public static JButton disconnectButton = null;

private static JPanel initOptionsPane() {
JPanel pane = null;
ActionAdapter buttonListener = null;
JPanel optionsPane = new JPanel(new GridLayout(4, 1));
pane = new JPanel(new FlowLayout([Link]));
[Link](new JLabel("Host IP:"));
ipField = new JTextField(10); [Link]("localhost");
[Link](true);
[Link](ipField);
[Link](pane);

pane = new JPanel(new FlowLayout([Link]));
[Link](new JLabel("Port:"));
portField = new JTextField(10); [Link](true);
[Link]((new Integer(1234)).toString());
[Link](portField);
[Link](pane);

ButtonGroup bg = new ButtonGroup();
hostOption = new JRadioButton("Host", true);
guestOption = new JRadioButton("Guest", false);
[Link](hostOption);
[Link](guestOption);
pane = new JPanel(new GridLayout(1, 2));
[Link](hostOption);
[Link](guestOption);
[Link](pane);

JPanel buttonPane = new JPanel(new GridLayout(1, 2));
buttonListener = new ActionAdapter() {
public void actionPerformed(ActionEvent e) {

if ([Link]().equals("connect")) {
[Link](false);
[Link](true);
}
else {
[Link](true);
[Link](false);
}
}
};
connectButton = new JButton("Connect");
[Link]("connect");
[Link](buttonListener);
[Link](true);
disconnectButton = new JButton("Disconnect");
[Link]("disconnect");
[Link](buttonListener);
[Link](false);
[Link](connectButton);
[Link](disconnectButton);
[Link](buttonPane);

return optionsPane;
}

private static void Gui() {
statusBar = new JLabel();
[Link]("Offline");

JPanel optionsPane = initOptionsPane();

JPanel chatPane = new JPanel(new BorderLayout());
chatText = new JTextArea(10, 20);
[Link](true);
[Link](false);
[Link]([Link]);
JScrollPane chatTextPane = new JScrollPane(chatText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
chatLine = new JTextField();
[Link](false);
[Link](chatLine, [Link]);
[Link](chatTextPane, [Link]);
[Link](new Dimension(200, 200));

JPanel mainPane = new JPanel(new BorderLayout());
[Link](statusBar, [Link]);
[Link](optionsPane, [Link]);
[Link](chatPane, [Link]);

mainFrame = new JFrame("Simple TCP Chat");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](mainPane);
[Link]([Link]());
[Link](200, 200);
[Link]();
[Link](true);
}

public static void main(String args[]) {
Gui();
}
}

class ActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {}
}

OUTPUT-

Common questions

Powered by AI

The program uses a JLabel statusBar to communicate the connection state to the user, initially setting it to "Offline." The ActionAdapter updates this label and enables/disables the connect/disconnect buttons based on user actions. This approach effectively combines visual cues and interaction feedback, helping users understand the current state and available actions .

The ActionAdapter class implements the ActionListener interface which allows it to handle action events generated by the GUI components. In this program, the class's actionPerformed method processes actions for the connect and disconnect buttons, enabling or disabling these buttons based on user interaction .

The program reuses existing panels like optionsPane and chatPane by configuring them separately and then adding to the main panel, mainPane, with layouts managing their positioning. This modular approach prevents redundancy by ensuring components are created once and reused, contributing to cleaner code management .

The program uses a ButtonGroup to group the JRadioButtons for Host and Guest, ensuring that only one can be selected at a time by the user. This is achieved by adding both hostOption and guestOption to the ButtonGroup, which manages their selection states .

The main window is initialized in the Gui method. A JFrame named mainFrame is used as the main application window, set to close on exit. It incorporates several panes: the optionsPane from initOptionsPane, a chatPane, and a statusBar at the bottom. The layout is managed using BorderLayout, and components such as JTextArea for chat display and JTextField for input are added. JFrame properties like size and location are set before making the window visible .

The application utilizes a combination of BorderLayout for the main frame and several nested panels using GridLayout and FlowLayout for organizing controls. The advantage of this approach is the clear separation of different parts of the interface, such as options, status, and chat areas, each managed independently. However, a potential drawback is the complexity of managing multiple layouts, which can lead to increased development overhead and potential alignment issues if not handled carefully .

The default IP set in the program is 'localhost', and the port number is 1234. 'Localhost' is significant for development because it allows the application to communicate within the same machine, facilitating testing and debugging processes without requiring network access .

The chatLine JTextField is initially disabled to prevent the user from entering text before a connection is established. This state ensures the application reflects the correct workflow, avoiding errors that could arise if messages were sent without a valid connection. The GUI logic enables chatLine when the user is connected to a chat session .

The JTextArea chatText is customized by setting line wrapping to true, making it non-editable to prevent user modifications, and applying a blue foreground color to enhance readability. This ensures that messages appear in a consistent format and prevents user-generated errors in display output .

Using JFrame as the main container provides benefits like built-in window controls, support for menus, and easy integration with lightweight components, making it ideal for a standalone GUI application. Compared to JDialog or a simple JPanel, JFrame offers more flexibility and user interaction control options. However, it might be an overkill for very simple interfaces where such features are unnecessary .

You might also like