Search code examples
javaswingmodal-dialogglasspane

Java Dialog setModal


Related to my previous question Java Project - how to freeze Frame, Is it possible to darken the color of the main screen (make EVERYTHING gray or black and white) to highlight the player dialog box? How can I do that?


Solution

  • See the Glass Pane example for a complete example and explanation.

    In general your glass pane can use an transparent background:

    JComponent glassPane = new JPanel();
    glassPane.setBackground( new Color(240, 20, 20, 100) );
    frame.setGlassPane( glassPane );
    

    Then when you want to show the dialog the basic code would be:

    glassPane.setVisible( true );
    JDialog dialog = new JDialog(...);
    // add components to dialog
    dialog.setVisible( true );
    glassPane.setVisible( false );