简体   繁体   中英

JDialog Freezes on Loss of Focus?

I have a program I'm working on that is supposed to facilitate the making of texture maps by allowing you to edit individual textures in the map. I have several places where I use JDialog's for things like input of the tile-size of a map, the initial size of a new map, and one that simply has a button on it that the user presses when he or she is done editing the selected textures with an external program (such as photoshop or paint.) However, any time one of these JDialog's is up, if the program loses focus it becomes completely unresponsive. Here's my code for the JDialog that pops up when you're editing selected textures externally:

JDialog editing = new JDialog(mainFrame, "Externally Editing");//mainFrame is the name of the JFrame containing everything.
JPanel pnl = new JPanel();
editing.setLayout(new BorderLayout());
pnl.setPreferredSize(new Dimension(150, 60));
editing.add(pnl);
editing.pack();
JButton button = new JButton("Done Editing");
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        editng = false;//Obviously, a boolean that I have outside of this method.
    }
});
pnl.add(button);
Thread.sleep(100);
editing.setVisible(true);
while(editng){System.out.print("");}//Doing nothing while the user is externally editing.. Unfortunately, if I don't have "System.out.print("");" it doesn't work.. Oh, Java..
new Thread(new Runnable(){public void run(){editing.dispose();}}).start();//Gotta dispose of it in a separate thread since AWT isn't Thread-safe... Ugh..

I would assume that it freezes in the AWT Thread that it creates/has for the JDialog, while my thread is just waiting for the button to be pressed.. Which can't happen because the JDialog is frozen.

Make your dialog modal. Your application will stop processing until the dialog is closed

Why this bogus roundabout? Simply dispose the dialog in the listener.

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