15

How would you make a JComponent (panel, frame, window, etc.) fullscreen, so that it also overlaps everything on the screen including the windows start bar?

I don't want to change the resolution or anything with the graphics device like bitdepth etc, I just want to overlap everything else.

3
  • 1
    What's wrong with the answers given so far?
    – Thomas
    Commented Mar 20, 2010 at 12:57
  • Yeah, seriously. Give it to Adamski - you won't get a better answer. Commented Mar 23, 2010 at 7:30
  • I mean, as long as you don't give a clue on what you don't like about the current answers, no-one is gonna be able (or willing) to come up with something else.
    – Thomas
    Commented Mar 23, 2010 at 17:59

5 Answers 5

20

Check out this tutorial describing Java's Full-Screen mode API.

Example code (taken from the tutorial). Note that the code operates on a Window so you would need to embed your JPanel with a Window (e.g. JFrame) in order to do this.

GraphicsDevice myDevice;
Window myWindow;

try {
    myDevice.setFullScreenWindow(myWindow);
    ...
} finally {
    myDevice.setFullScreenWindow(null);
}
10
+50

You can try some of the codes in this page, allowing a container to fill the screen (so it is not a solution for an individual component, but for a set of components within a container like a JFrame)

public class MainWindow extends JFrame
{
  public MainWindow()
  {
    super("Fullscreen");
    getContentPane().setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
    pack();
    setResizable(false);
    show();

    SwingUtilities.invokeLater(new Runnable() {
      public void run()
      {
        Point p = new Point(0, 0);
        SwingUtilities.convertPointToScreen(p, getContentPane());
        Point l = getLocation();
        l.x -= p.x;
        l.y -= p.y;
        setLocation(l);
      }
    });
  }
  ...
}
2
  • Can you please explain why this works. Specifically the part in the runnable, such as the lines following l.x -= p.x;. It works, but I want to fully understand it as well. Commented Feb 23, 2015 at 19:30
  • @Connorelsea It might be easier to ask that in a separate question: after 5+years, and don't remember all the details.
    – VonC
    Commented Feb 23, 2015 at 20:04
6

You need to use the following API: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html

Going full screen isn't as simple as making a large panel, you need to look into the underlying OS graphics. But your JPanel code should translate just fine.

1

I needed to search a lot, to do the same. Here is completely a working version of it by steps, so that i can find it later also, and use it.

Step 1: create a file called fullscreen.java

Step 2: copy this code and paste it as it is:

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

public class fullscreen extends Window 
{
  private Button button;

  public fullscreen() 
  {
    super(new Frame());
    button = new Button("Close");
    button.addActionListener(new ActionListener() 
    {
      public void actionPerformed(ActionEvent e) 
      {
        System.exit(0);
      }
    });

    setLayout(new FlowLayout());
    add(button);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0,0,screenSize.width, screenSize.height);
  }

  public static void main(String[] args) 
  {
    // This will take over your whole screen tested and works in my:
    // Fedora 12/13/14
    // CentOS 5.0
    // if this works for you, in other platforms, please leave a comments which OS it worked.
    // happy coding!
    new fullscreen().setVisible(true);
  }

}

Step 3: compile the code and run

Done.

0

If I were you I would try to make Java not draw the border of the Jframe, then make it take all the screen.

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import javax.swing.JFrame;


public class FenNoBorder extends JFrame {

    public FenNoBorder () {
        setUndecorated(true);
        setVisible(true);
        GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle maximumWindowBounds=graphicsEnvironment.getMaximumWindowBounds();
        setBounds(maximumWindowBounds);
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.