简体   繁体   中英

Swing: change to a another panel in a JFrame after a long running method call

My Java application working flow:

  1. get a input keyword, then translate it to a query to HTTP POST
  2. new a Jframe, witch shows some words like "keyword processing... pls waiting."
  3. do the POST in [1], it may take many minutes.
  4. after POST returns a result, extract the data I need.
  5. show the data in the Jframe, which replaces the content in [2]

I first try to code the flow logic in Jframe's constructor. But the frame will hang on the HTTP POST stage, and doesn't show the words in [2]. After POST done, it shows the data in [5] directly.

Cause jumping the stage [2], users may think the process blocking means the something wrong, it does not do the search work.

Now I add a JOptionPane.showMessageDialog in stage[2]. It stops the process because needing a mouse click on "OK" Button in the dialog. In that moment, the panels in the frame shows the words, "keyword processing... pls waiting.".

I'd like to know why the stop point of the MessageDialog can paint the old panel, which means the words in [2]. Such as revalidate() or repaint() do not paint the old panel.

Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See Concurrency in Swing for details and the fix (a SwingWorker ).


BTW - Use a CardLayout as shown in this answer for changing panels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM