Hi

I wonder if someone could point me in the right direction, I have two class`s in my program one is a search thread the other is the swing gui.

I want to send updates and info from the thread to my gui but have not got any idea on how to do it.

I should mention i do have a third class that has the main in and runs the JPanel.

Any ideas on where I should be looking.

Thanks in advance

Steve

Recommended Answers

All 6 Replies

You can add get methods to your thread class. Assuming that the thread class instance is visible in your gui class, you can start it and you can call its get methods anywhere you want in the gui and get the latest values of the thread's attributes.

Thats a brilliant idea , I was trying to complicate things lol cool thanks for this.

On another note can I set my status update to pull the get method every 5 seconds or something like that ?

boolean b = true;
while (b) {
   try {
      Thread.sleep(5000);
      // call method
   catch (Exception e) {
   }
}

The problem would be where are you going to put that code and when will it stop. I would advise you not to put that code in the gui because you will never be able to exit the loop once it is inside. It needs to go to a thread, so it will run in parallel with the rest of the code.

Can elaborate more on that:
"On another note can I set my status update to pull the get method every 5 seconds or something like that ?"

Use a javax.swing.Timer to schedule the pull updates. Easy, Safe.

ps: Java 6 (1.6) introduced the SwingWorker class to handle exactly this kind of situation (background task with progress reports and a final result to the GUI) - it may be a bit late for your code, but have a look anyway

FYI, related to this.

thanks for everyone`s help , If i had know about Swing Worker before I had got this far I would have used it, What I have done is create a pop up gui when the search is running which the thread updates the Jtextarea with the status updates as and when they happen

Figured out today that writing a java program is relatively easy but writing one that is designed well is another matter.

This year in uni we are looking at design patterns so hopefully this will help me when writing my own programs.

Thanks for everyone's help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.