0

I am (still) a beginner in Java, and I created a little software which contains a main frame.

I need to cover all the Desktop behind my software such as a windows 98 installing screen : (I need the black and blue screen behing, covering all the task bar etc).

enter image description here

In order to do this, I used GraphicsDevice which goes full screen. It is exactly what I needed :

public class Fond_noir extends JFrame {

    private boolean isFullScreen = false;
    private GraphicsDevice device;

    public Fond_noir(int etat) {


      GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
       this.device  = env.getDefaultScreenDevice();

        initFullScreen(etat);
    }

    private void initFullScreen(int etat) {
        isFullScreen = device.isFullScreenSupported();

        if (etat==0)
        {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        }
        if (etat==1)
        {
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        }
        setUndecorated(isFullScreen);
        setResizable(!isFullScreen);
        if (isFullScreen) {

            // Full-screen mode
            device.setFullScreenWindow(this);
            validate();
        } else {
            // Windowed mode
            this.setExtendedState(MAXIMIZED_BOTH);
            this.setVisible(true);
        }
    }


}

Then, I call this method in a main somewhere else, (there's no problem with this) :

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {


            new Fond_noir(0);
            Choix_Langue inst = new Choix_Langue(); // main frame
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);

        } }  );  }

But the problem is, that my main frame can't show-up, and it's hidden behind my fullscreen.. I'd like the opposite ! Or when I click on my main frame in my task bar (aften using the window key of my keyboard ofc..) I can only see my main frame, and the fullscreen is not showing-up with the frame

=> Is there a way to show both my frame and my GraphicsDevice ? Using "priorities" between them..?

Thanks for reading !

  • well ... there 'll still be the alt-tab. you won't be able to completely block out the other applications, I guess – Stultuske May 15 '14 at 10:58
  • I agree, but my problem is to do the exact same thing as the installing screen of windows 98 – XxRoboticaXx May 15 '14 at 10:59
  • 1
    you can write code that 'calculates' the 'width and height' of your screen, and you can use those to create a background JFrame. on load, you open up a second JFrame or a JDialog. have you tried that? if it stays hidden, you'll need to set the focus to the smaller screen – Stultuske May 15 '14 at 11:07
  • thanks again for your answer ! Actually, I tried this but I can't hide the task bar with a JFrame, and I really need to cover all the screen.. If there's a way to create a fullscreen with a JFrame I'll do your technique – XxRoboticaXx May 15 '14 at 11:10
  • 1
    this thread might help you out: http://stackoverflow.com/questions/19435048/hide-windows-taskbar-in-java – Stultuske May 15 '14 at 11:11
  • thanks, I'll try with this. If I suceed, I will come back here. – XxRoboticaXx May 15 '14 at 11:16

3 Answers3

4

Use this:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setAlwaysOnTop(true)

Undecorated will remove the titlebars. Also instead of trying to show both the frames seperately. Add the small one to the bigger one.

bigFrame.add(smallFrame);
bigFrame.setVisible(true);

Example to show that it works:

example

ManyQuestions
  • 1,069
  • 1
  • 16
  • 34
3

I'm not sure you need to go full screen exclusive mode, for example, you can size a border-less frame to fit the default screen size and make it always on top to help it cover all other windows in the system and then simply use a JDialog as the primary interface to work with the user, for example...

Desktop

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class FullScreenBackground {

    public static void main(String[] args) {
        new FullScreenBackground();
    }

    public FullScreenBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

                JFrame frame = new JFrame("Testing");
                frame.setAlwaysOnTop(true);
                frame.setUndecorated(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new BackgroundPane());
                frame.setLocation(0, 0);
                frame.setSize(dim);
                frame.setVisible(true);

                JDialog dialog = new JDialog(frame);
                dialog.setContentPane(new InstallPane());
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        });
    }

    public class InstallPane extends JPanel {

        public InstallPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("<html><h1>Welcome to my fancy pancy background screen<h1></html>"), gbc);
        }

    }

    public class BackgroundPane extends JPanel {

        private BufferedImage bg;

        public BackgroundPane() {
        }

        @Override
        public void invalidate() {
            super.invalidate(); 
            bg = null;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg == null) {
                bg = new BufferedImage(1, getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = bg.createGraphics();
                LinearGradientPaint lgp = new LinearGradientPaint(
                                new Point(0, 0),
                                new Point(0, getHeight()),
                                new float[]{0f, 1f},
                                new Color[]{Color.BLACK, Color.BLUE}
                );
                g2d.setPaint(lgp);
                g2d.fillRect(0, 0, 1, getHeight());
            }
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

    }

}

Updated

If changing all the "frames" is not hard, you could consider making the following changes to the above example...

JFrame frame = new JFrame("Testing");
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BackgroundPane());
frame.setLocation(0, 0);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setSize(dim);
// This will stop the background window from become focused,
// potentially hiding the other windows
frame.setFocusableWindowState(false);
frame.setFocusable(false);
frame.setVisible(true);

JFrame dialog = new JFrame();
// Will need to add this to each frame...
dialog.setAlwaysOnTop(true);
dialog.setContentPane(new InstallPane());
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);

The other problem you might face is the fact that alwaysOnTop is platform dependent, meaning that it might behaviour differently on different platforms.

Changing extends JFrame to extends JDialog really would be a simpler and more stable change...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • It's the same problem as peeskillet. It works fine but my software is almost done and I can't change all the content of my frames, and using Jdialog.. My boss just told my one week ago I had to cover the full screen ! If I had known earlier, I would have done your answer.. – XxRoboticaXx May 15 '14 at 11:49
  • @crashystar Rather then using Full Screen Exclusive mode `device.setFullScreenWindow(this);` I've simply used a very large, borderless frame which is set to always be on top. Full Screen Exclusive Mode will prevent all other windows from appearing above this window and also has some issues related to the way that input events are handled... – MadProgrammer May 15 '14 at 11:51
  • 1
    @XxRoboticaXx You should have considered using a `CardLayout` based on a `JPanel`, then the top level container would have become irrelevant ;) – MadProgrammer May 15 '14 at 11:52
  • @XxRoboticaXx Check the update for a possible (messy) solution for using `JFrame`s instead of `JDialog`s... – MadProgrammer May 15 '14 at 12:03
  • It seems pretty nice. I am just trying using a JWindow, then I come back to your answer. – XxRoboticaXx May 15 '14 at 12:03
  • @XxRoboticaXx `JWindow` should suffice as well (for the background window), but in your case, you would just about end up with the same result – MadProgrammer May 15 '14 at 12:05
  • Your're right.. Using JWindows works because if I click on the Icon of my main frame, I can display both my frames and my black window behind. It's better than earlier but I have to look for my frame on my task bar... I have about 500 frames (100 *5 languages) .. So if it's possible not to change every frame it would be better ^^' !! – XxRoboticaXx May 15 '14 at 12:13
  • See why we don't recommend extending from `JFrame`. One frame, lots of panels...better design ;) – MadProgrammer May 15 '14 at 12:15
  • I think along the same lines, but I had to continue on an existing software, so I didn't created the main things.. I think I got a lot of advice, so thank you everyone. I will try with all your answers. – XxRoboticaXx May 15 '14 at 12:25
  • @XxRoboticaXx Story of my life. They give me shit, I polish it, they complain it's still shit... – MadProgrammer May 15 '14 at 12:26
  • Lol ! Btw, You won't believe it... They put the line "this.setState(Frame.ICONIFIED);" in the main frame (I agree : my bad, i didn't see it.. I wasn't focused on that).. While tyring to change my frame to a dialog I saw the line, deleted it and now : JWindow + jFrame are displaying together.. – XxRoboticaXx May 15 '14 at 12:37
  • @xxroboticaxx Something else you might want to look into is [localisation AKA Internationalization](http://docs.oracle.com/javase/tutorial/i18n/) – MadProgrammer May 15 '14 at 20:16
0

I found a solution with all your anwers.

Instead of using another frame I used a JWindow for the background :

public class Fond_noir extends JWindow{
    Panel panel = new Panel();


    public Fond_noir(int etat) {

       if (etat==0)

           {
           setSize(2300,4000);
           setLocationRelativeTo(null);
           setVisible(true);

           }

       if (etat==1)

           {
             dispose();
            }

        panel.setBackground(Color.black);
        add(panel);
        }

class Panel extends JPanel{

        public void paintComponent(Graphics g){
            super.paintComponent(g);

        }
    }
}

Then while trying to change the "extends JFrame" of my main frame to "extends JDialog", it made me delete this horrible line in the code : this.setState(Frame.ICONIFIED); !!!! It explains why I had to look for my icon all the time.. So I kept my JFrame.. So now it opens a background window AND the frame at the same time :)

Thank you everyone ! Next time I won't use that much frames.