ID:277314
 
I was bored in class today, so I whipped this up. I got it hosted and on a site and everything, but when I tested to see if others can see what you say(or the other way around), it doesn't work - It only shows what you said. Somebody please point out what I did wrong?

Source:
/*
Trying to create a chatroom in java
Grrr...Property of Kit.
*/


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class chat extends Applet implements ActionListener
{

TextArea msg=new TextArea(null,1,60,0);
TextArea ppl=new TextArea(null,15,20,0);
TextArea dsp=new TextArea(null,15,75,3);
TextField nameField=new TextField(10);
Label nspc=new Label(" ");
Label sspc=new Label(" ");
String name="Chatter";
int chatnum=1;
Button send=new Button("Send");
Button changeName=new Button("Change Name");

public void init()
{
add(dsp);
dsp.setEditable(false);
add(ppl);
ppl.setEditable(false);
ppl.setText("~People In Chat~");
add(nameField);
nameField.setText(name);
add(nspc);
add(changeName);
changeName.addActionListener(this);
add(msg);
add(sspc);
add(send);
send.addActionListener(this);



}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
{
dsp.setText(dsp.getText()+name+": "+msg.getText()+"\n");
msg.setText("");
return;
}

if(e.getSource()==changeName)
{
dsp.setText(dsp.getText()+name+" changes thier name to: "+nameField.getText()+"\n");
name=nameField.getText();
nameField.setText(name);
return;
}

}
}
It looks entirely devoid of network code. Remember, Java doesn't do the networking for you like BYOND does.
In response to PirateHead
Don't see networking. Put that in.
In response to PirateHead
It doesn't? D:
Where can I learn the networking part, since my book does not cover that?
Any good site tutorials?

e.e Wts...my computer has gone crazy! It keeps saying I have a trojan and worm on it, but I ran my scanner, and it only found spyware, and it got deleted <<;

In response to Dead_Demon
Dead_Demon wrote:
It doesn't? D:
Where can I learn the networking part, since my book does not cover that?
Any good site tutorials?

e.e Wts...my computer has gone crazy! It keeps saying I have a trojan and worm on it, but I ran my scanner, and it only found spyware, and it got deleted <<;


http://math.hws.edu/javanotes/

Thats the newest version. Has networking.
In response to Dead_Demon
Look up the 'socket' class in the Java API.
Dead_Demon wrote:
I was bored in class today, so I whipped this up. I got it hosted and on a site and everything, but when I tested to see if others can see what you say(or the other way around), it doesn't work - It only shows what you said. Somebody please point out what I did wrong?

Source:
> /*
> Trying to create a chatroom in java
> Grrr...Property of Kit.
> */

>
> import java.applet.*;
> import java.awt.*;
> import java.awt.event.*;
>
> public class chat extends Applet implements ActionListener
> {
>
> TextArea msg=new TextArea(null,1,60,0);
> TextArea ppl=new TextArea(null,15,20,0);
> TextArea dsp=new TextArea(null,15,75,3);
> TextField nameField=new TextField(10);
> Label nspc=new Label(" ");
> Label sspc=new Label(" ");
> String name="Chatter";
> int chatnum=1;
> Button send=new Button("Send");
> Button changeName=new Button("Change Name");
>
> public void init()
> {
> add(dsp);
> dsp.setEditable(false);
> add(ppl);
> ppl.setEditable(false);
> ppl.setText("~People In Chat~");
> add(nameField);
> nameField.setText(name);
> add(nspc);
> add(changeName);
> changeName.addActionListener(this);
> add(msg);
> add(sspc);
> add(send);
> send.addActionListener(this);
>
>
>
> }
>
> public void actionPerformed(ActionEvent e)
> {
> if(e.getSource()==send)
> {
> dsp.setText(dsp.getText()+name+": "+msg.getText()+"\n");
> msg.setText("");
> return;
> }
>
> if(e.getSource()==changeName)
> {
> dsp.setText(dsp.getText()+name+" changes thier name to: "+nameField.getText()+"\n");
> name=nameField.getText();
> nameField.setText(name);
> return;
> }
>
> }
> }
>


I read up on sockets and networking and such, and I understand most of it, but I'm not sure how to use it to send messages through this, since it isn't a swing or command line. I can get the rest of it set up(sockets and such) if I knew how to get the text to display to others. Would somebody please shed a little more light on this?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;

public class Chatter
{
//Components
TextField textIn=new TextField(15);

public static void main(String[] args) throws IOException
{
//Server and Client information
ServerSocket server=null;
Socket client=null;
PrintWriter sendOut=null;
BufferedReader recieve=null;

try{
server=new ServerSocket(2500);
client=new Socket("Testament",2500);
sendOut=new PrintWriter(client.getOutputStream(), true);
recieve=new BufferedReader(new InputStreamReader(client.getInputStream()));
}
catch(UnknownHostException e){
System.err.println("Cannot connect to host");
System.exit(0);
}
catch(IOException e){
System.err.println("Cannot get Input/Output for the host");
System.exit(1);
}
}

public void init()
{
add(textIn);
}

}


I am pretty sure I got the networking right, but I keep getting this error when I compile:

Chatter.java:38 Cannot Resolve Symbol
Symbol: method add (java.awt.TextField)
location: class Chatter
add(textIn);

How do I fix this?
In response to Dead_Demon
There is no "add" method, possibly because you forgot to make your Chatter class inherit from something.

I'm not sure you have any clue as to what you're doing. Find a decent networking tutorial and read up on the basics.
In response to Crispy
But add is part of the java.awt.* package, and works fine without the networking. I've been following the java.sun tutorials and the Javanotes 5.0, and I am pretty sure I have a basic grasp on networking. Does the java.net.* and java.awt.* cause problems with each other?
In response to Dead_Demon
Dead_Demon wrote:
But add is part of the java.awt.* package

Doesn't matter. Simply including a package does absolutely nothing.

There may be an add() method defined on some class, somewhere (which you haven't actually instantiated) but it's not defined on this class. That's the only thing that matters.

I suggest you try to find some working sample code and dissect it to see how it works, rather than struggling to implement your own version when you clearly don't understand the required basic concepts.
In response to Crispy
Crispy wrote:
I suggest you try to find some working sample code and dissect it to see how it works, rather than struggling to implement your own version when you clearly don't understand the required basic concepts.

I've been trying to find some small applet chatroom examples, but all google can find are premade rooms for download, no tutorials or examples to download for me to look at.
In response to Dead_Demon
Your Chatter class has no add method, so Java has no idea what to call. What is your logic behind using add() there?
In response to Stephen001
The add(textIn) is supposed to add a text field onto the applet. It is strange, I went and remade it exactly, and it worked for the Chatter2.java, but not for Chatter.java o_O;
In response to Dead_Demon
Chatter isn't extending Applet in the example you've provided, that's why it can't find add().