Domanda

So I have a main frame that sometimes produces an error frame by pressing on one of the JButtons. I want to make it so when the error frame pops, main frame is inactive and you are unable to press any buttons. Is there an easy way for that?

È stato utile?

Soluzione

What you are referring to is modality1, for which case a modal JDialog is more appropriate than a JFrame. See more at How to Use Dialogs

Also, if this is a simple message popup, a simple JOptionPane would be more suitable. You can also see how to use JOptionPanes in the link above.

Here's a simple JOPtionPane example. Though I don't use a frame in the example, if you do have a frame, the JOptionPane will block any user interaction.

import javax.swing.JOptionPane;
public class SimpleErrorMessage {
    public static void main(String[] args) {
       JOptionPane.showMessageDialog(
               null, "Error!", "Error Message", JOptionPane.ERROR_MESSAGE);
    }  
}

enter image description here

1. A dialog box can be either modeless or modal. A modal dialog box is one that blocks input to some other top-level windows in the application, except for any windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press

Altri suggerimenti

You can use a glass pane to make main frame "inactive": http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top