How to create JFrame in the same thread so that it blocks?

前端 未结 2 960
北海茫月
北海茫月 2021-01-17 08:15

For debug purposes, I need to draw image on the screen in a simple window.

Swing handles all it\'s events in a separate message loop thread. That means that if I do

相关标签:
2条回答
  • 2021-01-17 08:53

    How to create JFrame in the same thread so that it blocks?

    Use a modal JDialog.

    JOptionPane.showMessageDialog(null, scrollPane, message, javax.swing.JOptionPane.INFORMATION_MESSAGE);
    

    This has a flaw though - you can't see the debug window on the taskbar.

    Don't use null for the dialog owner. Make sure you specify the owner JFrame. Whenever you click on the taskbar icon the frame and child dialog will both show up.

    0 讨论(0)
  • 2021-01-17 08:59

    Build your GUI outside the loop once and then use

     // read new component data
     screenshot = MSWindow.screenshot();
     // modify components
     label = new JLabel(new ImageIcon(screenshot));     
    
     // force the frame to repaint its chilfren
     frame.revalidate();
     frame.pack();
    

    to "refresh" it inside the loop.

    0 讨论(0)
提交回复
热议问题