2

Okay, so I've got my menu system up and working from a JFrame. Everything seems to work really well, up until I click the button which starts a canvas. Now what the canvas does is intialize a JFrame which extends Canvas so I can't use a thread. Once the frame is up and running it calls a method which has a while true {} after this I am unable to close the frame. This has never been an issue before when running the canvas application using static void main. How can I fix this issue of the new JFrame not closing?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Malii
  • 448
  • 6
  • 17
  • Don't mix Swing & AWT components unless you have a very good reason. For this, it probably needs a `JPanel` instead of the `Canvas`. To turn probability into something more definite, provide a lot more details of the use-case. – Andrew Thompson Jul 31 '12 at 01:37

2 Answers2

5

How can I fix this issue of the new JFrame not closing?

Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of creating an infinite loop, implement a SwingWorker for long running tasks. See Concurrency in Swing for more details.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

1. Make this a rule of thumb when working with GUI application, that Always keep the UI work on the UI thread and Non-UI work on the Non-UI thread.

2. Second doNot mix up SWING AND AWT.

3. The main() method in Java Gui is not long lived, after scheduling the work in the Event Dispatcher Thread (EDT) the main() method quits. Now its solely the responsibility of the EDT to handle the GUI.

4. So never mixup the Non-UI process-intensive work, with the EDT.

Use EDT to handle the GUI.

Eg:

public static void main(String[] args){


     EventQueue.invokeLater(new Runnable(){

                myframe.setVisible(true);

       });


 }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75