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?
By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame. ICONIFIED) to minimize a JFrame and setState(JFrame. MAXIMIZED_BOTH) to maximize a JFrame.
If you just want to display the frame maximized the code is: frame. setExtendedState(JFrame. MAXIMIZED_BOTH); frame.
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.
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);
}
Use setExtendedState(int state)
, where state would be JFrame.MAXIMIZED_BOTH
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With