JFrame in full screen Java
Add:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
If you want put your frame in full-screen mode (like a movie in full-screen), check these answers.
- How can I do full screen in Java on OSX
- Java setFullScreenWindow() keep on top
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);
}
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.
Use setExtendedState(int state)
, where state would be JFrame.MAXIMIZED_BOTH
.