New JFrame with new thread

2.8k Views Asked by At

I created a JFrame with combo boxes and a button which will create a new thread and continue to do an action. I want a new JFrame to start with every new thread to output logs to the new JFrame. But even if I put the code related to the JFrame in the new thread and close that JFrame it ends the whole program instead of that running thread. What the best approach to making what I want possible? I simply want a new JFrame to open with each new thread started and when I close that JFrame it would end that thread.

Regards!

2

There are 2 best solutions below

2
On BEST ANSWER

By default, closing a JFrame will simply hide it (see the documentation for setDefaultCloseOperation()). If closing a window is exiting your application, this must be due to your own code. You're not, by chance, calling setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE), are you?

1
On

Here are some ideas:

  • Don't block the event dispatch thread; use SwingWorker instead, as shown here.

  • Don't use multiple frames; use panels in a container having a suitable layout.