Building Simple GUI in Java – ChatClient

Here in this tutorial we will build a simple GUI (graphical user interface) application for a simple client version of a chat program. To do that, we need to use Java’s swing library. We are going to useGroupLayout class to manage sizing and position of the Buttons, TextFields, Labels and ScrollPanes etc.

A captured image of the sample GUI:

chatclient

How it works?

User has to type his/her name in order to send a message. Doing that he can send message to the console. After sending a message or creating a username, There will be a status update in south left corner of the frame.

User Interface Structure

Please note that this is only the graphical interface to manage Chat of a client. Network functionality is not added. Meaning that it can’t connect to a socket and receive other clients messages.

Horizontal Group Configuration
layout.setHorizontalGroup(layout
.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane)
.addComponent(statusbar)
.addGroup(layout.createSequentialGroup()
.addComponent(label)
.addComponent(username)
.addComponent(setusernamebutton)
.addGroup(layout.createSequentialGroup()
.addComponent(message)
.addComponent(sendbutton));

Vertical Group Configuration
layout.setVerticalGroup(layout.createSequentialGroup()
.createSequentialGroup()
.addComponent(jScrollPane)
.addComponent(statusbar)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label).addComponent(username).addComponent(setusernamebutton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(message).addComponent(sendbutton)) );

For more info on layouts, please visit the following links below:

https://docs.oracle.com/javase/tutorial/uiswing/layout/group.html

http://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.html

 

Here is the code of this GUI application:

Share this Post

Categories

Featured Author