• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

URGENT!! chat applet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've created a chat Applet as client. i want it to be used like this
-user login
-choose chat room
-and start applet.
what i've done now is chat applet. i want it to be used by multi user.
and display online users name or username.
but now it can only detect one user. if i run many applet concurrently using my pc, it can't detect other user's name.
how to do that?
actually i modified this code from one i get from the web page i found.
the original one is not connected to server. and it has the problem as i mentioned before.
this is the original code.
can anyone help me?
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ChatApplet extends Applet implements ActionListener,Runnable
{
String user;
String msg;
public void init() {
super.init();
//{{INIT_CONTROLS
setLayout(new BorderLayout(0,0));
addNotify();
resize(518,347);
setBackground(new Color(12632256));
msgbox = new java.awt.TextArea("",2,0,TextArea.SCROLLBARS_NONE);
msgbox.setEditable(false);
msgbox.disable();
//msgbox.hide();
msgbox.reshape(0,0,380,216);
add(msgbox);
idbox = new java.awt.TextField();
idbox.reshape(84,288,284,24);
add(idbox);
button1 = new java.awt.Button("EnterRoom");
button1.reshape(384,288,72,21);
add(button1);
list = new java.awt.List();
//list.TOP_ALIGNMENT();
//list.disable();
list = new java.awt.List(5);
list.add("#Default User"+"\n");
list.reshape(384,24,128,196);
list.setFont(new Font("Helvetica", Font.BOLD, 12));
add(list);
label2 = new java.awt.Label("Members");
label2.reshape(396,0,100,19);
add(label2);
label1 = new java.awt.Label("UserName");
label1.reshape(0,288,72,27);
add(label1);
textbox = new java.awt.TextField();
textbox.reshape(84,240,431,44);
add(textbox);
label3 = new java.awt.Label("EnterText");
label3.reshape(0,252,72,25);
add(label3);
//}}
//uf = new UserFrame();
button1.addActionListener(this);
idbox.addActionListener(this);
textbox.addActionListener(this);
list.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==idbox)
{
user = idbox.getText()+"\n";
list.addItem(user.trim());
idbox.setText("");
msgbox.append(user +" HAS JOINED THE GROUP");
}
if(ae.getSource().equals(button1))
{
user = idbox.getText()+"\n";
list.addItem(user.trim());
idbox.setText("");
msgbox.append(user +" HAS JOINED THE GROUP");
}
if(ae.getSource().equals(textbox))
{
msg = textbox.getText();
msgbox.append(msg +"\n");
textbox.setText("");
}
if(ae.getSource().equals(list))
{
String l = list.getSelectedItem();
//uf.setTitle(l);
//Frame i[] = uf.getFrames();
//uf.setVisible(true);
}
}
public void start()
{
if(vt == null)
{
vt = new Thread(this,getClass().getName());
vt.start();
}
}
public void run()
{
try{
for(int i=0;i<10;i++)
{
msgbox.append("One stop Java source code - www.globalleafs.com"+"\n");
msgbox.setForeground(Color.red);
vt.sleep(30000);
vt.resume();

}
}catch(Exception e){e.printStackTrace();}
}

java.awt.TextArea msgbox;
java.awt.TextField idbox;
java.awt.Button button1;
java.awt.List list;
java.awt.Label label2;
java.awt.Label label1;
java.awt.TextField textbox;
java.awt.Label label3;
private Thread vt;
}
 
sue wah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my codes after i modified the original codes.
it can be compiled but when i run nothing happens.
please help me..import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatApplet2 extends Applet implements ActionListener,Runnable
{
String user;
String msg;
String chatServer;
ObjectOutputStream output;
ObjectInputStream input;
Socket client;
public void init() {
super.init();
//{{INIT_CONTROLS
setLayout(new BorderLayout(0,0));
addNotify();
resize(518,347);
setBackground(new Color(12632256));
displayArea = new java.awt.TextArea("",2,0,TextArea.SCROLLBARS_NONE);
displayArea.setEditable(false);
displayArea.disable();
//displayArea.hide();
displayArea.reshape(0,0,380,216);
add(displayArea);
idbox = new java.awt.TextField();
idbox.reshape(84,288,284,24);
add(idbox);
button1 = new java.awt.Button("EnterRoom");
button1.reshape(384,288,72,21);
add(button1);
list = new java.awt.List();
//list.TOP_ALIGNMENT();
//list.disable();
list = new java.awt.List(5);
list.add("#Default User"+"\n");
list.reshape(384,24,128,196);
list.setFont(new Font("Helvetica", Font.BOLD, 12));
add(list);
label2 = new java.awt.Label("Members");
label2.reshape(396,0,100,19);
add(label2);
label1 = new java.awt.Label("UserName");
label1.reshape(0,288,72,27);
add(label1);
enterField = new java.awt.TextField();
enterField.reshape(84,240,431,44);
add(enterField);
label3 = new java.awt.Label("EnterText");
label3.reshape(0,252,72,25);
add(label3);
//}}
//uf = new UserFrame();
button1.addActionListener(this);
idbox.addActionListener(this);
enterField.addActionListener(this);
list.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==idbox)
{
user = idbox.getText()+"\n";
list.addItem(user.trim());
idbox.setText("");
displayArea.append(user +" HAS JOINED THE GROUP");
}
if(ae.getSource().equals(button1))
{
user = idbox.getText()+"\n";
list.addItem(user.trim());
idbox.setText("");
displayArea.append(user +" HAS JOINED THE GROUP");
}
if(ae.getSource().equals(enterField))
{
sendData(ae.getActionCommand() );
/*msg = enterField.getText();
displayArea.append(msg +"\n");
enterField.setText("");*/
}
if(ae.getSource().equals(list))
{
String l = list.getSelectedItem();
//uf.setTitle(l);
//Frame i[] = uf.getFrames();
//uf.setVisible(true);
}
}
public void start()
{
try {
// Step 1: Create a Socket to make connection
connectToServer();
// Step 2: Get the input and output streams
getStreams();
// Step 3: Process connection
processConnection();
// Step 4: Close connection
closeConnection();
}
// server closed connection
catch ( EOFException eofException ) {
System.out.println( "Server terminated connection" );
}
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
if(vt == null)
{
vt = new Thread(this,getClass().getName());
vt.start();
}
}
public void run()
{
try{
for(int i=0;i<10;i++)
{
displayArea.append("One stop Java source code - www.globalleafs.com"+"\n");
displayArea.setForeground(Color.red);
vt.sleep(30000);
vt.resume();

}
}catch(Exception e){e.printStackTrace();}
}
private void sendData( String msg )
{
// send object to server
try {
output.writeObject( user + ">>> " + msg );
output.flush();
displayArea.append( "\nuser>>>" + msg );
}
// process problems sending object
catch ( IOException ioException ) {
displayArea.append( "\nError writing object" );
}
}
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream(
client.getOutputStream() );
// flush output buffer to send header information
output.flush();
// set up input stream for objects
input = new ObjectInputStream(
client.getInputStream() );
displayArea.append( "\nGot I/O streams\n" );
}
// connect to server
private void connectToServer() throws IOException
{
displayArea.setText( "Attempting connection\n" );
// create Socket to make connection to server
client = new Socket(
InetAddress.getByName( chatServer ), 5000 );
// display connection information
displayArea.append( "Connected to: " +
client.getInetAddress().getHostName() );
}
// process connection with server
private void processConnection() throws IOException
{
// enable enterField so client user can send messages
enterField.setEnabled( true );
// process messages sent from server
do {
// read message and display it
try {
msg = ( String ) input.readObject();
displayArea.append( "\n" + msg );
displayArea.setCaretPosition(
displayArea.getText().length() );
}
// catch problems reading from server
catch ( ClassNotFoundException classNotFoundException ) {
displayArea.append( "\nUnknown object type received" );
}
} while ( !msg.equals( "SERVER>>> TERMINATE" ) );
} // end method process connection
// close streams and socket
private void closeConnection() throws IOException
{
displayArea.append( "\nClosing connection" );
output.close();
input.close();
client.close();
}
java.awt.TextArea displayArea;
java.awt.TextField idbox;
java.awt.Button button1;
java.awt.List list;
java.awt.Label label2;
java.awt.Label label1;
java.awt.TextField enterField;
java.awt.Label label3;
private Thread vt;
}
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic