threads and main program
i have made a program that spawns a thread. from this thread i want to call a function of the main program and pass a string variable to it.
like this:
public void showit(String line)
{
show line...in the textbox area...
}
is this somehow possible? help please i did it and is not working
i have on the thread
server ss;
ss=new mainprogram();
ss.showit(threadline);
this is the GUI
import java.io.*;
import java.net.*;
import java.util.*;
public class server extends javax.swing.JFrame {
// javaserver1 server1;
ServerSocket ss = null;
thread_start_server tss;
public volatile String line="-";
/** Creates new form server */
public server() {
initComponents();
}
public void sendit()
{
textArea1.appendText("i am back \n");
// textArea1.appendText(line+"\n");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
textArea1 = new java.awt.TextArea();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 270, 380, -1));
jLabel1.setText("Disconnected");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 40, 150, -1));
jButton1.setText("Listen");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 81, -1));
jButton2.setText("Stop listening");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 120, -1));
textArea1.setColumns(1);
getContentPane().add(textArea1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 380, 190));
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
try{
//tss = new thread_start_server();
//tss.close();
}
catch (Exception e) {
}
jLabel1.setText("Disconnected");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
// textArea1.appendText("MPIKA STO LISTEN\n");
/* try{
ss =new ServerSocket(5598);
connectionhandler ch;
String sline;
// Tell the world we're ready to go
*/
//line="-";
tss = new thread_start_server();
textArea1.appendText("Starting thread \n");
tss.start();
textArea1.appendText("Server listening \n");
jLabel1.setText("Listening");
/*server1=new javaserver1();
// Keep accepting connections forever
while (true) {
// Grab the next incoming connection
Socket s = ss.accept();
ch=new connectionhandler();
ch.ConnectionHandler(s);
jLabel1.setText("Listening");
}
}
catch(Exception e)
{
}
*/
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public void startit()
{
textArea1.appendText("helo\n");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new server().show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
public java.awt.TextArea textArea1;
// End of variables declaration
}
this is what is inside the thread. i read input from socket and i process it. i want line by line to be shown in the main program where a textbox exists.
String tmp = input.readLine(); //read from the stream
alltemp="";
alltemp=alltemp+tmp;
String sline;
position = alltemp.indexOf("\\");
if (position == -1)
{
//Exit Do
}
JOptionPane.ERROR_MESSAGE );
sline = alltemp.substring(0,position);
sl=new server();
sl.line=sline;
//the public variable line which exists in the main program becomes as ///sline but function sendit() it is not called from the main and textbox is not updated with the line
sl.sendit();
alltemp = alltemp.substring(position+2);
}
}
catch (Exception e) {
System.out.println("ERROR " + connection.getInetAddress()
+ " " + " " + " " + e);
}
finally {
try {
connection.close();
}
catch (IOException e) {
}
}