Skip to content

Commit

Permalink
Enter chat with message and name
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSattizahn committed Jun 2, 2016
1 parent 68d838c commit f3aeefa
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
95 changes: 79 additions & 16 deletions src/ChatClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import java.net.*;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

Expand All @@ -17,6 +21,8 @@ public class ChatClient extends JFrame implements Runnable {
protected TextField name;
protected TextField input;
protected Thread listener;
protected TextField introName;
protected JFrame nameScreen;

public ChatClient(String title, InputStream i, OutputStream o) {
super(title);
Expand All @@ -35,36 +41,93 @@ public ChatClient(String title, InputStream i, OutputStream o) {
add("North", name = new TextField());
name.setText("Enter Name");
pack();
setLocationRelativeTo(null);
setVisible(true);
setSize(500, 500);
input.requestFocus();
listener = new Thread(this);
listener.start();


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try {

ChatClient.this.o.writeUTF(name.getText() + " has left the chat! " );

nameScreen = new JFrame();
introName = new TextField();
introName.setEditable(true);
introName.addActionListener( new AbstractAction(){

@Override
public void actionPerformed(ActionEvent e) {
ChatClient.this.name.setText(ChatClient.this.introName.getText());
try {
ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! ");
ChatClient.this.o.flush();
Thread.sleep(100);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ChatClient.this.nameScreen.dispose();
}

});
introName.setText("Enter Name, Ya Dingus!");
nameScreen.setLayout(new BorderLayout());
nameScreen.add(introName);
nameScreen.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
nameScreen.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ChatClient.this.name.setText(ChatClient.this.introName.getText());
try {
ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! ");
ChatClient.this.o.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});
JButton b = new JButton();
nameScreen.add("South", b);
b.setText("SetName");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
ChatClient.this.name.setText(ChatClient.this.introName.getText());
try {
ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! ");
ChatClient.this.o.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ChatClient.this.nameScreen.dispose();
}

});
nameScreen.setLocationRelativeTo(null);
nameScreen.setVisible(true);
nameScreen.setSize(200, 100);
nameScreen.toFront();
nameScreen.repaint();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {

ChatClient.this.o.writeUTF(name.getText() + " has left the chat! ");
ChatClient.this.o.flush();
Thread.sleep(100);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

System.exit(0);


}

System.exit(0);

}
});

}

public void run() {
Expand Down
2 changes: 1 addition & 1 deletion src/ChatHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void run() {
broadcast(msg);
}
} catch (IOException ex) {
System.out.print("Client has left or error has occured");
System.out.println("Client has left or error has occured");
//ex.printStackTrace();
} finally {
handlers.removeElement(this);
Expand Down

0 comments on commit f3aeefa

Please sign in to comment.