I have been trying to get the window to automatically maximize using Netbeans.
I've probably looked through 4 or 5 pages of Google for an answer.
The web pages always provide something like this:
public void run() {
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
I am using Netbeans 6.9.1
Does this no longer work? Is there another way to do this?
Also, if you find your answer on a web page, please provide the link so I can look into this further. Thanks in advance for any input! :)
Regarding setExtendedState()
, "Note that if the state is not supported on a given platform, nothing will happen."
If that's not relevant, an sscce may be helpful.
Addendum: This example seems to function correctly:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/questions/5207425 */
public class NewJavaGUI extends JPanel {
private void display() {
JFrame f = new JFrame("NewJavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJavaGUI().display();
}
});
}
}
Addendum: The relevant state constants appear to form a coherent set. In particular, MAXIMIZED_HORIZ | MAXIMIZED_VERT == MAXIMIZED_BOTH
:
NORMAL 0 0000 MAXIMIZED_HORIZ 2 0010 MAXIMIZED_VERT 4 0100 MAXIMIZED_BOTH 6 0110