Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning a JFrame black when opening another window

I wan't to make my main JFrame become darken when the focus is on another window. This is an example from the game Football Manager 2012. First the main window is selected and it looks like it should, then when it is loading, it turns darker and unselectable. I wan't to have this effects on my own application, but im not really sure how, not even sure what to google?

Im guessing its a JWindow that appears and the JFram becomes unselectable in the background. I'm planing to use it on a help-window in my application, that is a JWindow right now.

enter image description hereenter image description here

like image 282
Handsken Avatar asked Nov 08 '11 09:11

Handsken


2 Answers

Andrew Thompson has the right idea, only it's easier to use the glass pane feature of your frame's JRootPane. Here's some working code: In your frame class, invoke

getRootPane().setGlassPane(new JComponent() {
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0, 0, 0, 100));
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
});

Then, to show the "curtain", invoke

getRootPane().getGlassPane().setVisible(true);

In the code above, change the alpha transparency value of 100 in order to find the suitable darkness.


..wan't the JFrame to go back to normal after the new window is closed. I tried setVisible(false) but it didn't work.

It works in this example.

Shadowed Frame

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ShadowedFrame extends JFrame {

    ShadowedFrame() {
        super("Shadowed Frame");
        getRootPane().setGlassPane(new JComponent() {
            public void paintComponent(Graphics g) {
                g.setColor(new Color(0, 0, 0, 100));
                g.fillRect(0, 0, getWidth(), getHeight());
                super.paintComponent(g);
            }
        });
        JButton popDialog = new JButton("Block Frame");
        popDialog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                getRootPane().getGlassPane().setVisible(true);
                JOptionPane.showMessageDialog(ShadowedFrame.this, "Shady!");
                getRootPane().getGlassPane().setVisible(false);
            }
        });
        setContentPane(popDialog);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setSize(350,180);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ShadowedFrame().setVisible(true);
            }
        });
    }
}
like image 107
Ingo Kegel Avatar answered Sep 19 '22 01:09

Ingo Kegel


(Untested, but..) Seems like a good task for a JLayeredPane. Create a JComponent that is set transparent and add that to the top level of the layered pane. In the paintComponent(Graphics) method of the component, set a semi-transparent color and fill the full area with it. In normal use (non-dimmed), call customComponent.setVisible(false).

Update

Or, as Ingo pointed out, use the glass pane.

I'm guessing its a JWindow that appears and the JFrame becomes unselectable in the background

It is more likely a modal JDialog. When a modal dialog is visible, the frame/window that is the owner becomes inaccessible (cannot be clicked on).

like image 35
Andrew Thompson Avatar answered Sep 18 '22 01:09

Andrew Thompson