[java] JFrame in full screen Java

I will be doing a project soon and I will have to use full screen mode in it.

It will draw some graphics in the window. It would be convienient if I use JFrame or something similar.

I don't know what the final resolution of the screen will be. Please tell me if the graphics will be automaticly rescaled?

JFrame jf = new JFrame();
jf.setSize(1650,1080);
//make it fullscreen;
//now is everything is going to be rescaled so it looks like the original?

This question is related to java swing fullscreen

The answer is


You can use properties tool.
Set 2 properties for your JFrame:

extendedState 6
resizeable    true


Easiest fix ever:

for ( Window w : Window.getWindows() ) {
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow( w );
}

Just use this code :

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

public class FullscreenJFrame extends JFrame {
    private JPanel contentPane = new JPanel();
    private JButton fullscreenButton = new JButton("Fullscreen Mode");
    private boolean Am_I_In_FullScreen = false;
    private int PrevX, PrevY, PrevWidth, PrevHeight;

    public static void main(String[] args) {
        FullscreenJFrame frame = new FullscreenJFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setVisible(true);
    }

    public FullscreenJFrame() {
        super("My FullscreenJFrame");

        setContentPane(contentPane);

        // From Here starts the trick
        FullScreenEffect effect = new FullScreenEffect();

        fullscreenButton.addActionListener(effect);

        contentPane.add(fullscreenButton);
        fullscreenButton.setVisible(true);
    }

    private class FullScreenEffect implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if (Am_I_In_FullScreen == false) {
                PrevX = getX();
                PrevY = getY();
                PrevWidth = getWidth();
                PrevHeight = getHeight();

                // Destroys the whole JFrame but keeps organized every Component
                // Needed if you want to use Undecorated JFrame dispose() is the
                // reason that this trick doesn't work with videos.
                dispose();
                setUndecorated(true);

                setBounds(0, 0, getToolkit().getScreenSize().width,
                        getToolkit().getScreenSize().height);
                setVisible(true);
                Am_I_In_FullScreen = true;
            } else {
                setVisible(true);
                setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
                dispose();
                setUndecorated(false);
                setVisible(true);
                Am_I_In_FullScreen = false;
            }
        }
    }
}

I hope this helps.


JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));

This just makes the frame the size of the screen


If you want put your frame in full-screen mode (like a movie in full-screen), check these answers.

The classes java.awt.GraphicsEnvironment and java.awt.GraphicsDevice are used for put an app in full-screen mode on the one screen (the dispositive).

e.g.:

static GraphicsDevice device = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getScreenDevices()[0];

public static void main(String[] args) {

    final JFrame frame = new JFrame("Display Mode");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setUndecorated(true);

    JButton btn1 = new JButton("Full-Screen");
    btn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            device.setFullScreenWindow(frame);
        }
    });
    JButton btn2 = new JButton("Normal");
    btn2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            device.setFullScreenWindow(null);
        }
    });

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    panel.add(btn1);
    panel.add(btn2);
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);

}

Values highlighted that I mean:

JFrame - Properties


First of all, that is the resolution you would want to use, 1650,1080.

Now add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

If you have issues with the components on the JFrame, then after you have added all the components using the frame.add(component) method, add the following statement.

frame.pack();

Set 2 properties below:

  1. extendedState = 6
  2. resizeable = true

It works for me.


One way is to use the Extended State. This asks the underlying OS to maximize the JFrame.

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Other approach would be to manually maximize the screen for you requirement.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
setLocationRelativeTo(null);

But this has pitfalls in Ubuntu OS. The work around I found was this.

if (SystemHelper.isUnix()) {
    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(p);
        }
    });
}

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
setLocationRelativeTo(null);

In Fedora the above problem is not present. But there are complications involved with Gnome or KDE. So better be careful. Hope this helps.


you can simply do like this -

public void FullScreen() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            final View v = this.activity.getWindow().getDecorView();
            v.setSystemUiVisibility(8);
        }
        else if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = this.activity.getWindow().getDecorView();
            final int uiOptions = 4102;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

it will helps you use your object instant of the frame

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);


You only need this:

JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

When you use the MAXIMIZED_BOTH modifier, it will max all the way across the window (height and width).

There are some that suggested using this:

frame.setUndecorated(true);

I won't recommend it, because your window won't have a header, thus no close/restore/minimize button.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to fullscreen

Make div 100% Width of Browser Window How to show imageView full screen on imageView click? Embed youtube videos that play in fullscreen automatically How to hide navigation bar permanently in android activity? Video 100% width and height How to open a web page automatically in full screen mode Full-screen responsive background image JFrame in full screen Java How to set maximum fullscreen in vmware? Chrome Fullscreen API