Advanced Chat Box Application in Jav
a
Swing
Event Handling
The following example
illustrates creating an advanced chat box in Java in Swing that
contain two Chat Boxes for two different User’s. In this Chat Box we use
the concepts of java Swing package .The software that You need to install is Eclipse.
No external library required to build this Chat Box application.you
can download the zip file of the project but before downloading you need to
learn how its work.
Let’s Start……..
Firstly you need to
create a java project into eclipse workspace,name of my project is ChatBox.
Step1: Click on File->New->JavaProject and create a project.
Step2: Explore the project folder named as Chatbox ,right click on
src folder and create a class Name as ChatRoom.java
Step3: After that you need to create three Jframe files named as:
1.Server
2.Window_1
3.Window_2
Shown as below:
If your eclipse does
not have jframe option then you need to install Window Builder.
Step4: Design the Server.java for getting the both usernames and
Start the chat.It contains:
1.Two label’s,
2.Two Text fields and
3. A button
Shown as below: You can change the layout according to your choice.
Step5: Design the Window_1 and Window_2 as a chat box.Both window
contain:
1.A label to show the name of the user,
2. Two textArea’s :one for typing
message and 2nd for displaying chats and
3.Two Buttons:one for clear the chat and
second one for send the chat.
Shown as below:
Till yet we cover all
the designing part.Now we are moving on the working of our ChatBox.
Let’s start it…
ChatRoom.java
public class
ChatRoom {
static
Window_1 win1;
static
Window_2 win2;
static
Server server;
public
ChatRoom() {
server =
new Server();
server.setLocationRelativeTo(null);
server.setVisible(true);
}
public static void
createRoom() {
win1 =
new Window_1();
win2 =
new Window_2();
win1.setLocation(500,
200);
win2.setLocation(900,
200);
win1.setVisible(true);
win2.setVisible(true);
server.setVisible(false);
}
public static void
main(String[] args) {
ChatRoom chatRoom =
new ChatRoom();
}
}
|
Window_1.java
//define
them below the public class
Window_1 extends JFrame
static
String Username1;
private
JPanel contentPane;
|
//declare
the access specifier of all the elements
private
javax.swing.JLabel label1;
private static
javax.swing.JTextArea display1;
private
javax.swing.JButton send1;
public static
javax.swing.JTextArea text1;
|
//create the function sentText
public static void
sendText() {
String
s= Window_2.text2.getText();
if (s.equals(""))
{
return;
}
display1.append(Window_2.Username2 +
":" + s +
"\n");
}
|
//find the label1 and set its text to shown in chatbox
label1 = new JLabel("Chat Window for : " + Username1);
|
//perform
the following tast onaction of SEND button
send1.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
//code
String s =
text1.getText();
if(s.equals(""))
{
return;
}
display1.append(Username1 +
":" + s +
"\n");
Window_2.sendText();
text1.setText("");
}
});
|
//clear the chat
clear.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent arg0) {
display1.setText("");
}
});
|
Do the same for Window_2.java file ,you just need to
change the:
Username1 into Username2,display1 -> display2, label1
-> label2, text1-> text2, send1 -> send2
Change all the object name as reverse in Window_2 .java for example: if window_1 use
Username2, You need to change it Username1 in the Window_2 file.
Server.java
//event
on text field and button,just call the function createRoom written at the end
name1 =
new JTextField();
name1.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
//code
createRoom();
}
});
name2 =
new JTextField();
name2.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent arg0) {
//code
createRoom();
}
});
JButton
btnNewButton = new
JButton("Join Chat");
btnNewButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent arg0) {
//code
createRoom();
}
});
|
private void
createRoom() {
String p1,p2;
p1 =
name1.getText();
p2 =
name2.getText();
if(p1.equals("")
|| p2.equals("")) {
JOptionPane.showMessageDialog(null, "Please
Enter a Valid Username");
return;
}
Window_1.Username1 =
name1.getText();
Window_2.Username2 =
name2.getText();
ChatRoom.createRoom();
}
|
Working of the Project is shown below:
Run the ChatRoom.java:
After giving the usernames click on Join Chat Button.If you leave a text field blank it will show the Message Box.
The two chat window's appear as given:
Send the chat by clicking on send button and it will display on the TextArea of both chats.
Clear the chat by clicking on Clear button.
Download the Zip file of project Chat Box Download Now
I hope you enjoy this ChatBox application guidance.Download the zip file and run it on your systems.If you got any bug or error you can comment below or email me.
Thank you
0 Comments