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);

[434 byte] By [stefanosna] at [2007-10-2 7:33:12]
# 1

What do you mean "call a function of the main program"?

The thread can call any public method of any class. If you want communication between the two threads, then there are various ways to achieve that, depending on exactly what is meant by "communication."

"Not working" contains no useful information for somebody trying to help you. Post the relevant bit of code, and details of what "not working" means. Paste in the exact error message if there is one.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-16 21:13:25 > top of Java-index,Java Essentials,New To Java...
# 2

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) {

}

}

stefanosna at 2007-7-16 21:13:25 > top of Java-index,Java Essentials,New To Java...