4 Replies - 367 Views - Last Post: 12 April 2012 - 07:39 PM Rate Topic: -----

#1 XBlitz0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 12

I have a question regarding the use of GUI and JButton

Posted 12 April 2012 - 05:34 PM

So I want to make a chatroom type code that implements the use of JButton where you connect and disconnect from the server. I have already done the connect portion but for some reason I can't get the use of the close button to work. I also wanted to add the use of name where the user would enter his/her name and it would appear and as they talk it would show "Sally: Hi" or "Matt: Hi". I just can't figure out how to combine it with JButton

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

public class Gui2Client extends JFrame implements ActionListener{

JTextField tf;
JTextArea ta;
JButton connect, close;	
DataObject in, out;
Socket s;
ObjectOutputStream oos;
ObjectInputStream ois;
Listener l;

public Gui2Client(){

setSize(500,400);
setTitle("My Chat Program (Alpha)");
setDefaultCloseOperation(EXIT_ON_CLO…
tf = new JTextField();
tf.addActionListener(this);
ta = new JTextArea();
JScrollPane jsp = new JScrollPane(ta);

connect = new JButton("Connect");
connect.addActionListener(this);
close = new JButton("Close");

JPanel p = new JPanel();
p.add(connect);
p.add(close);

getContentPane().add(tf, BorderLayout.NORTH);
getContentPane().add(jsp, BorderLayout.CENTER);
getContentPane().add(p, BorderLayout.SOUTH);


setVisible(true);
}

public void actionPerformed(ActionEvent ae){
Object obj = ae.getSource();

if(obj == connect){
try{
s = new Socket("localhost", 8189);
oos = new ObjectOutputStream(s.getOutputStream());

out = new DataObject();
l = new Listener(s);
l.start();
}catch(Exception e){

System.out.println(e.getMessage())…
}
}else{
String temp = tf.getText();
tf.setText("");
out.setMessage(temp);
try{	
oos.writeObject(out);
oos.reset();
}catch(Exception e){

System.out.println(e.getMessage())…
}
//ta.append(temp + "\n");
}
}

class Listener extends Thread{

Socket s;

public Listener(Socket s){
this.s = s;
}
public void run(){
try{
ois = new ObjectInputStream(s.getInputStream());

while(true){

in = (DataObject)ois.readObject();
ta.append(in.getMessage() + "\n");

}
}catch(Exception e){
System.out.println(e.getMessage()…
}	
}

}

public static void main(String[] args){

new Gui2Client();

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: I have a question regarding the use of GUI and JButton

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: I have a question regarding the use of GUI and JButton

Posted 12 April 2012 - 05:47 PM

Why don't use you just append the client name to the message you're sending.

else{
String temp = tf.getText();
tf.setText("");

out.setMessage(myName+":"+temp); //<---HERE

try{	
oos.writeObject(out);
oos.reset();
}



Ask the user their name with a dialog when they open the program.

This post has been edited by blackcompe: 12 April 2012 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 XBlitz0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 12

Re: I have a question regarding the use of GUI and JButton

Posted 12 April 2012 - 06:03 PM

So will this ask the client/user for their name and then input it into the chatroom?? sorry I'm fairly new at java, just started about 2-3 months ago
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: I have a question regarding the use of GUI and JButton

Posted 12 April 2012 - 06:09 PM

Quote

So will this ask the client/user for their name and then input it into the chatroom??


That's your code with the proper modifications. Do you understand what I said? Do you know how to create a dialog?

out.setMessage(myName+":"+temp); //<---HERE


This is the answer to what you asked: "How do I append a name to a message in the text field?" Append the name before sending the message across the socket.

This post has been edited by blackcompe: 12 April 2012 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: I have a question regarding the use of GUI and JButton

Posted 12 April 2012 - 07:39 PM

You need to add the actualListener to the "close" button
in the actionPerformed() if it is that button just close the I/O stream and the socket
(may be you can send a message name + "logoff" before)

And correctly indent your code, it is a nighmare to read
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1