I have an Applet code like below. I have a String called String randomNumberStr
in this Applet to display a random number, which has retrieved from a socket server.
After Applet window is displayed, I have a button called "Connect". Clicking on this button will connect with Socket program and gets a Random Number string.
My issue is, I am running socket connection code in a separate thread SocketConnectionThread
in this Applet, But, String randomNumberStr
and repaint
function are there in main thread.
How can i access and pass the random number value from this thread SocketConnectionThread
to main and repaint the Applet window?
public class CobrowseApplet extends Applet implements ActionListener
{
private static final long serialVersionUID = 1L;
String titleStr ;
String randomNumberStr;
Image sharedImage;
BufferedImage image;
private Button connectBtn;
Socket localSocket;
PrintWriter out;
BufferedReader in;
static Timer timer;
int delay = 1000;
int period = 1000;
DataInputStream inStream;
PrintStream outStream;
InputStream input;
public void init(){
titleStr = "Welcome";
randomNumberStr = "";
connectBtn = new Button("Connect");
connectBtn.addActionListener(this);
add(connectBtn);
connectBtn.setBounds(200, 50, 90, 20);
connectBtn.setEnabled(true);
setLayout( null );
setSize(550, 650);
}
public void paint (final Graphics g)
{
//super.paint(g);
int x = getSize().width;
int c1 = x/2;
g.drawString(titleStr, c1-100, 20);
g.drawString(randomNumberStr, c1-100, 80);
System.out.println("sharedImage" + sharedImage);
//g.drawImage(sharedImage, 100, 100, this);
System.out.println("drawImage");
}
@Override
public void actionPerformed(ActionEvent e)
{
Thread thr = new Thread(new SocketConnectionThread(randomNumberStr));
thr.start();
if ( connectBtn.getLabel()=="Connect" )
{
connectBtn.setLabel("");
connectBtn.setLabel("Disconnect");
}
else
{
connectBtn.setLabel("");
connectBtn.setLabel("Connect");
}
System.out.println("randomNumberStr: " + randomNumberStr);
repaint();
}
}
class SocketConnectionThread implements Runnable {
String randomStr;
public SocketConnectionThread(String randomNumberStr) {
this.randomStr = randomNumberStr;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("Before Applet socket connection");
Socket localSocket = new Socket(getLocalIP(), 8080);
BufferedReader socketReader = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
String msgStr = socketReader.readLine();
System.out.println("Server Message on Client: " + msgStr);
// IT GETS THE NEW STRING HERE FROM SOCKET. HOW CAN I PUSH TO MAIN?
randomStr = msgStr;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String getLocalIP () {
InetAddress inetAddress = null;
String ipAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
ipAddress = inetAddress.getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ipAddress : " + ipAddress);
return ipAddress;
}
}