What is the correct way to display a Java JFrame
Window on Linux (and here under different Window Managers), Mac and Windows allways in the same way?
We have actually some trouble with a JFrame
, that is very inconsistent because on Windows the JFrame
is maximized, on my Linux Notebook with awesome WM, the Window is only packed, and under Mac the Window gets half maximized, so its bigger then on Linux but not packed...
We tried to set the Screen just to the Maximium Size, which seems to work very well under windows, but not under Mac and Linux (Awesome WM).
Here is a very simple Frame, which explains what we are doing currently:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameMaximize extends JFrame {
private static final long serialVersionUID = 1L;
public JFrameMaximize() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// A Fake Dimension, inside this panel are a lot of swing components
panel.setPreferredSize(new Dimension(333, 666));
setContentPane(panel);
// Now we want to set the Extended State
if (Toolkit.getDefaultToolkit().isFrameStateSupported(MAXIMIZED_BOTH)) {
setExtendedState(MAXIMIZED_BOTH);
} else {
System.out.println("Oh oh... i have a problem...");
// Now we need a Fullscreen but we dont have the size of a WindowManager Panels and
// stuff
Dimension max = Toolkit.getDefaultToolkit().getScreenSize();
// asume that the screen has a 20px bar on top (=awesome wm)
max.height -= 20;
setSize(max);
setLocation(0, 20);
// The alternative is to call pack() here, but then the window is totally broken, because of the internal components. Every Panel we load in here has a different size, so the window will scale up and down on every panel change.
}
}
public static void main(String... args) {
new JFrameMaximize();
}
}
the problem is that some Window Managers dont have the Maximize_both extendend state, so we have to calculate - which is impossible to know for every PC! How do we can set the window to a correct size on every window manager?
Is the only solution a fixed default size, which is given to the JFrame
? and the try to maximize the window?