I am trying to wirte java-program.
My Idea:
The main class launches thread for socket connection, also the main class launches Frame with GUI.
package dialogsubsystem2;
import InternetConnection.SocketConnectionThread;
/**
*
* @author ACER
*/
public class DialogSubsystemLauncher {
public SocketConnectionThread connectionSocket = new SocketConnectionThread("192.0.0.100", 2002);
public DialogSubsystemLauncher() {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DialogSubsystemLayoutFrame().setVisible(true);
}
});
connectionSocket.start();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
DialogSubsystemLauncher dlgLnch = new DialogSubsystemLauncher();
}
}
The created Frame launches another frames by request from user.
I want to show information about the socket connection (from socket connection thread) on statusbar of all Frames.
But I can’t to access the socket connection thread from Frames.
How should I solve this problem?
How should I launch socket connection thread to have access to it from any Frame?
You need that one of the objects has a reference to the other. I would point to the frame having a refence to the thread (since the thread may be blocked at times by I/O).
Just make the constructor of the Frame accept a thread object and pass the one you created(or add a set method to it).
EDIT:
In answer to aclarations in comments.
It is to the other classes (the Frames) that you create that you need to pass the reference.
From there, you can use
this.socketConnectionanywhere in the class to operate with it.