JPanel added but not displayed “in time”

前端 未结 3 984
囚心锁ツ
囚心锁ツ 2020-12-02 02:17

I have a JFrame that displays JPanels depending on the MenuItem you click. It works, but now I need to call a method once a JPanel is added to the frame and it is being show

3条回答
  •  萌比男神i
    2020-12-02 02:36

    The JPanel is not visible until Thread.sleep finishes. Why? What am I doing wrong?

    • don't block Event Dispatch Thread, Thread.sleep(int) block EDT, Swing GUI waiting untill this delay ended, and all change made during Thread.sleep(int) would not be visible on the screen, use Swing Timer instead, otherwise all changes to the Swing GUI must be wrapped into invokeLater()

    • Swing is single threaded and all updates to the visible GUI (or stated out of invokeLater) must be done on EDT, more in Concurency in Swing

    • for better help sooner post an SSCCE, short, runnable, compilable

提交回复
热议问题