aspose file tools*
The moose likes Applets and the fly likes 4th time i am asking.Please see the code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "4th time i am asking.Please see the code." Watch "4th time i am asking.Please see the code." New topic
Author

4th time i am asking.Please see the code.

nima lama
Greenhorn

Joined: Jun 18, 2001
Posts: 19
hello,
can you please help me. i am struck completely.
note: there are 4 more classes and two interfaces.so, this program wont run.
please change the required code which you feel its neccessary.
specially the one, where i have to getText from the textfield not from the console(dosprompt).
i thank you deeply
nima lama
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class RunTCPClient extends Applet implements ChatClient,ActionListener
{
TextArea txta;
TextField txt;
Button b;

public void init()
{
txta=new TextArea(20,30);
txt=new TextField(40);
b=new Button("send");
b.addActionListener(this);
add(txta);
add(txt);
add(b);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{


int port=8080;

String portStr=System.getProperty("port");
if(portStr!=null)
{
try
{
port=Integer.parseInt(portStr);
}
catch(Exception ignore)
{}
}
String hostName=System.getProperty("host");
if(hostName==null)hostName="localhost";

try
{
Socket clientSocket=new Socket(hostName,port);
DataOutputStream chatOutputStream=new DataOutputStream(clientSocket.getOutputStream());

DataInputStream chatInputStream=new DataInputStream(clientSocket.getInputStream());
//I can't know how to get input from the TextField not from System.in(console).
DataInputStream userInputStream=new DataInputStream(System.in);


System.out.println("connected to the chat server");

System.out.println("what name do you like to keep");
System.out.flush();
String myName=out.readLine();

chatOutputStream.writeUTF(myName);


RunTCPClient thisClient=new RunTCPClient();
//starts up a reader thread that reads messages from the server.
TCPChatReader reader=new TCPChatReader(thisClient,chatInputStream);

reader.start();

while(true)
{
String chatLine=out.readLine();
sendChat(chatOutputStream,chatLine);

}
}
catch(Exception ex)
{
System.out.println("got exception");
ex.printStackTrace();
System.exit(1);
}
}
}
public void userHasEntered(String who)
{
System.out.println("---"+who+" has entered--");
}
public void userHasLeft(String who)
{
System.out.println("---"+who+" has left---");
}
public void incomingChat(String who,String chat)
{
System.out.println("<"+who+">"+chat);
}
public void disconnect()
{
System.out.println("chat server connection closed");
System.exit(0);
}

public static void sendChat(DataOutputStream outStream,String line)throws IOException
{
outStream.writeInt(TCPChatMessageTypes.CHAT);
outStream.writeInt(line.length());
outStream.writeBytes(line);
}
}


Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can get text from a textfield with the getText() method... this returns a String representing the text in the textfield.

You would have to use something like the following code to get the text from your textfield inside the ActionListener for the "send" button.



HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
parimal patil
Greenhorn

Joined: Jun 25, 2001
Posts: 4
hello! ! !
Nima,
i am trying your code.
hope to get your code running within two to three days.
wait for reply
please i will take some time.
hope u understand
bye for now.
 
 
subject: 4th time i am asking.Please see the code.