Convertir un JFrame en negro al abrir otra ventana

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 here enter image description here

preguntado el 08 de noviembre de 11 a las 09:11

May be you are talking about Lightbox effect. If yes then este puedo ayudarte. -

2 Respuestas

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);
            }
        });
    }
}

respondido 10 nov., 11:15

Thanks it works fine, but i wan't the JFrame to go back to normal after the new window is closed. I tried setVisible(false) pero no funcionó. - Handsken

This works just as i wanted it! thanks. is it possible to make the JDialog an external class, i want to have some options in it, like choose color of the frame in the dialogbox? - Handsken

Or more lite, can JOptionPane.showMessageDialog(ShadowedFrame.this, "Shady!"); be a reference to another class, which extends a JDialog? - Handsken

@Handsken yes, you can show any modal dialog derived from JDialog instead of showing the JOptionPane. - Ingo Kegel

@Handsken Can't you? In any case it sounds like a separate question, one on which you should post your own SSCCE of (broken) code. It does not have to be as short as the one I posted, but trim out anything that is not necessary to see it break. Oh, and we probably won't need any screenshots, as pretty as they were. ;) - Andrew Thompson

(Untested, but..) Seems like a good task for a JLayeredPane. Crear un 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).

Noticias

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).

Respondido el 20 de junio de 20 a las 12:06

Thanks! I switched my JWindows to JDialogs, worked much better! :) - Handsken

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.