i have rather very basic question. I have one GUI class and another which does some background processing. Normally i have some methods in background class (which consist of multiple threads), which can return the result when called and i can use the result in GUI class.
But now i have this strange problem that the GUI has to display something, which requires background class to call external process and once connected to this process, the connection will remain open, and as such i can not return anything unless the stream is closed. However the process keeps sending data which needs to be displayed on GUI
If i put both in the same class, i can do so; by simple calling e.g JLabel.setText(what ever coming from external prcess in infinite while loop). However if it is in a different class, i can not access this JLabel or any such component from another class. If i declare them static, the GUI throws exception, is there anyway i can avoid this, or the only option to write the code together in the same class with the GUI
You have two problems to solve.
First, you need a means to communicate from one class to the other. There are many ways to solve this problem. For example, you could have the worker class publish events subscribed to by the GUI class. Or you could pass to the worker class a reference to the GUI object (possibly as an interface type).
Second, you need to update the GUI only from the event-dispatch thread. You can do this primitively with SwingUtilities.invokeAndWait(Runnable) or SwingUtilities.invokeLater(Runnable). Or you can use the higher-level framework provided by the SwingWorker class.