• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}


 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic