setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
System.out.println(\"--------> \"+getContentPane().getWidth
You haven't realized (i.e. setVisible(true)
) the JFrame
yet. And therefore, it has no size since it hasn't laid out its components.
The reason why you got 0 is because you didn't call any of the pack(), setSize(int, int) or setSize(Dimension). This is only when calling one of these method that the layout of your frame will be computed.
JFrame frame = new JFrame("My Frame");
frame.setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.setUndecorated(true);
frame.pack(); // Important line!!!
frame.setVisible(true);
System.out.println("--------> "+getContentPane().getWidth());