21

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.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200

5 Answers5

12

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

iirekm
  • 8,890
  • 5
  • 36
  • 46
5

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

PeGiannOS
  • 227
  • 4
  • 19
  • 1
    where do the magic numbers -10, -100, 30, and 110 come from (the amounts that setBounds is offset from the claimed screen size)? Is this portable with those same values? Will part of the window be off of the screen? – Gavin S. Yancey Nov 14 '13 at 02:40
  • @g.rocket you are right those numbers just fit for my app.A parametric one is setBounds(0,0,getToolkit().getScreenSize().width,getToolkit().getScreenSize().height); not setBounds(-10,-100,getToolkit().getScreenSize().width+30,getToolkit().getScreenSize().height+110); i will edit it thx – PeGiannOS Jan 28 '14 at 15:34
0

An example:

import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;




public class FullScreenJFrame extends JFrame{

    private GraphicsDevice vc;

    public FullScreenJFrame(){
     super();

     GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
     vc= e.getDefaultScreenDevice();



     JButton b = new JButton("exit");
     b.addActionListener(
             new ActionListener(){
                 public void actionPerformed(ActionEvent arg0) { 
                     dispose();
                     System.exit(0);

                 }
             }
             );
     this.setLayout(new FlowLayout());
     this.add(b);
     setFullScreen(this);
 }

 public void setFullScreen(JFrame f){

     f.setUndecorated(true);
     f.setResizable(false);
     vc.setFullScreenWindow(f);


 }

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

}
0
private Dimension screenSize; /* class level vars */
private swidth , sheight;

/*In GUI creating method:put this code: */
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
sheight = screenSize.height;
swidth = screenSize.width;
setBounds(0, 0, swidth, sheight-40);

NB: using swidth, sheight vars give you the liberty to further adjust.
Best way is to use int vars in place of - 40 e.g sheight/swidth - margin etc.
Here margin should come from parameter table. Getting full control of situation.
Direct usage also possible as: setBounds(0,0,screenSize.width, screenSize.height);

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
nukhan
  • 1
0

Use com.apple.eawt.FullScreenUtilities. And make sure to test that the system is running Mac OS.

public void enableFullscreen(Window window, boolean bool) {
    if (System.getProperty("os.name").startsWith("Mac OS")) {
        com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, bool);

    }

}