88413

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

Question:

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 the following:

while(true) { //Get screenshot BufferedImage screenshot = MSWindow.screenshot(); //Create JFrame JFrame frame = new JFrame(); Container main = frame.getContentPane(); //This layout should force the JLabel as large as the window, am I right? main.setLayout(new GridLayout(1,1)); //Create JLabel to display the screenshot JLabel label = new JLabel(new ImageIcon(screenshot)); main.add(label); frame.pack(); frame.setVisible(true); //Delay is allways good when meddling up with dangerous things Thread.sleep(2000); }

... I end up with many many JFrames.

<img alt="image description" class="b-lazy" data-src="https://i.stack.imgur.com/YAhbU.png" data-original="https://i.stack.imgur.com/YAhbU.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

I used to use JDialog which is blocking and stops thread until you press OK:

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. Sometimes, it's hard to find where the window ended up. This is why I want to switch to JFrame.

My question is straight up this: How to make <strong>current thread wait until JFrame is closed</strong>?

Answer1:

<blockquote>

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

</blockquote>

Use a modal JDialog.

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

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

</blockquote>

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.

Answer2:

Build your GUI <strong>outside</strong> the loop <strong>once</strong> 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.

Recommend

  • Setting rowcount and column count for grid layout dynamically
  • How to interchange between 2 timers? in Java GUI
  • Java 7 resize a JFrame (my GUI): the componentListener is not getting fired
  • Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException if-statement
  • I have accidentally uninstalled jack server while building Android AOSP
  • Help with eraser from buffered Image
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • JPanel and JButton, cannot figure how to layout 2 simple buttons
  • create images clickable on jpanel
  • How to create vertical TitledBorder in JPanel (javax swing)
  • Destroy instance of class then create instance of it again
  • Why does my components go out of boundaries, please help me to align it with the necessary code
  • GridBagLayout padding
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • Binary Tree Traversal Sum Of Each Depth
  • Small video playback
  • R h2o.glm - issue with max_active_predictors
  • Selecting a subset of data in ServiceStack.OrmLite
  • Are there any issues with placing image as background on an extended JFrame?
  • IE6 changes DOCTYPE to a bad one
  • allocating memory to an array of string
  • Doctrine/Symfony entity generator and generating entity from one table
  • C#: Import/Export Settings into/from a File
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Default parameter as generic type
  • How can I display the parent menu item's description using Wordpress walkers?
  • What is Eclipse's Declaration View used for?
  • Problems to linebreak with an int in JLabel
  • output of program is not same as passed argument
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • Traverse Array and Display in markup
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres