Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a JFrame really fullscreen?

In my Java application I try to make a JFrame really fullscreen by using this code:

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MainFrame() {
        super();
        this.setTitle();
        this.setUndecorated(true);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);

        this.setVisible(true);
        //this.pack();
    }
}

But on my Mac I can still see the Dock and the top toolbar of the OSX. So how can I create a JFrame that really consumes my whole screen?

EDIT I have to add that I want to call that JFrame from a eclipse plugin.

like image 257
RoflcoptrException Avatar asked Apr 07 '12 14:04

RoflcoptrException


People also ask

How do I make a JPanel full screen?

If you just want to display the frame maximized the code is: frame. setExtendedState(JFrame. MAXIMIZED_BOTH); frame.

How do I fix the size of a JFrame?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.


Video Answer


2 Answers

I haven't tried it yet, but Java has fullscreen API, which should meet your needs:

http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html

like image 101
iirekm Avatar answered Oct 19 '22 06:10

iirekm


I know the answer. Firstly, I have to admit that the following trick won't work if you are making video or movie player or animation player. OK here is what i found after many tries:

Let's say that you want to make a JFrame (called frame) fullscreen when you press a button (called fullscreenButton).Then do the following :

import java.awt.*; 
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) {
         // TODO Auto-generated method stub

             if(Am_I_In_FullScreen == false){

                      PrevX = getX();
          PrevY = getY();
          PrevWidth = getWidth();
          PrevHeight = getHeight();

          dispose(); //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
                      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 you enjoyed it

like image 33
PeGiannOS Avatar answered Oct 19 '22 07:10

PeGiannOS