Maximize jFrame size on startup using Netbeans IDE 5.5.1

I usally use Netbeans IDE for my Java application development. When I design my application GUI, I like to have my main GUI to be automatically maximized to the full Computer screen. I mean that the width and height of the main GUI windows should be equal to the computer screen width and height respectively after starting up the application.

We can achieve this in Netbeans using the following steps.

1) After adding a new JFrame Form .. with a name such as “MainUI.java”.

2) Click on MainUI.java in the Project window

3) In the Inspector windows under the Project window, click on the [JFrame]. Then, we will see that the Properties window on the right hand side is changed to [JFrame] Properties.

4) In the [JFrame] Properties windows, click on … next to the “preferredSize” property. A new [JFrame] preferredSize window will be poped up.

5) In the “preferredSize” property window, click on “Advanced…”.

6) Another “preferredSize — Advanced Initialization Code” windows is popped up. Check the box in front of “Generate Post-initialization Code”, and enter the code below.

//—————————————————– 

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dimension = tk.getScreenSize();
int screenWidth = dimension.width;
int screenHeight = dimension.height;
setPreferredSize(dimension);
//——————————————————–

7) Click OK and Close. Now, try to right click on the “MainUI.java” and then click “Run File”. The MainUI window should be maximized to the full screen. If there is a problem about Toolkit class not found exception, just right click anywhere in the source code and then click “Fix import”.

Advertisements
This entry was posted in Java. Bookmark the permalink.

28 Responses to Maximize jFrame size on startup using Netbeans IDE 5.5.1

  1. Nelson Cazonda says:

    Your code works but it doesn’t change turn the window into a maximized state. Try to this
    addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
    setExtendedState(MAXIMIZED_BOTH);
    }
    });

    on the construtor

  2. Ruds says:

    The first code is not working plz help me how to maxmize the JFrame on start up

  3. Tomppa says:

    thanx , nice work!

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    // construtor code
    addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
    setExtendedState(MAXIMIZED_BOTH);
    }
    });

  4. Ismail says:

    Thanks for your guids, Your guidelines are very clear!

  5. Cory says:

    Thanks Nelson, you saved me a lot of time. The maximized at startup code was troubling me.

  6. ephor says:

    thank you very much for the code.

  7. Kishor kshirsagar says:

    Hey !! Thanks for code it really works…..

  8. CopyCat says:

    Very good, thanx for the help!!!

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    // construtor code
    addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
    setExtendedState(MAXIMIZED_BOTH);
    }
    });

  9. chumaidi says:

    GREAT Comment !!!!

    Nelson Cazonda, on October 22nd, 2007 at 10:06 am Said:

    Thanks a lot……

  10. Firnas Tahir says:

    I tryed this methods in NetBeans 6.7.1 but it not work.
    finly I fornd onther way
    click the code in the JFrame propaties and change the “Form Size Policy” to “No Resize Code” and then write”javax.swing.JFrame.MAXIMIZED_BOTH” in JFrame proparties.extendedState

  11. Steve says:

    Thanks Firnas,

    I found that when I chose “No Resize Code” it worked fine but if I minimize it would make the windows incredibly small on my mac. I used “Generate Resize Code” and the “javax.swing.JFrame.MAXIMIZED_BOTH” you suggested above and now my app starts maximized and will minimize properly also.

    Steve.

  12. Brendan says:

    I am using netbeans 6.9 and had to play around with this a bit to get it working… here is my solution. Where “ProgNameView” = the name of your class which holds your GUI.

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    // constructor code
    this.getFrame().addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
    ProgNameView.this.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    });

  13. Brendan says:

    Further to my previous comment…. using windowOpened causes the window to be displayed briefly non-maximised. Try this instead:

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    // constructor code

    // Maximize the window when it first opens
    this.getFrame().addWindowListener(new WindowAdapter() {
    public void windowActivated(WindowEvent e) {
    ProgNameView.this.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    });

  14. Raj says:

    Thanks for the tut + the replies the Window adapter thing works perfectly for me =)

  15. anoop says:

    friends in the netbeans 6.9..there is no any advanced option in preferred size..then how can i get “Generate Post-initialization Code” in 6.9 version

    • mrtextminer says:
      1. Open the NetBeans IDE Properties window: Window –> Properties
      2. Click on the GUI component that you want to add your post-initialization code.
      3. Click on the Code tab on the Properties window. There, you will see a number of buttons that allow you to add your custom code.
  16. anoop says:

    frnds plz give reply as soon as possible

  17. anoop says:

    friends ..please tell me how to resize a image according to label using netbeans 6.9.
    means when i insert a image it cuts (if this is greater than label size) so please tell me the way so that image can automatically adjust according to label size.

  18. Henn Claus says:

    thanks nelzon,.. you’re helpful at all,…

  19. Adam says:

    Sample Code
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
    //new FormUtama().setExtendedState(MAXIMIZED_BOTH);
    new FormUtama().setVisible(true);
    }
    the first code like it and then you must change :
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
    //new FormUtama().setExtendedState(MAXIMIZED_BOTH);
    FormUtama frmUtama=new FormUtama();
    frmUtama.setVisible(true);
    frmUtama.setExtendedState(MAXIMIZED_BOTH);
    }

  20. shan k says:

    i use Netbeans 7.1 and it doesn’t have any advance options but i did try the @Nelson Cazonda and with a few manual adjustments (push a few buttons here there alignment) on the GUI it works . will let u know if it works in the entire project soon ! peace

  21. Pingback: Set JFrame Gui To Full Screen « RobotBASH

  22. Haneefa says:

    thanks …………

  23. Abhishek says:

    @Nelson Cazonda: Thank you sir….

  24. Afero says:

    In 7.3 it works very easy !
    just type ” javax.swing.JFrame.MAXIMIZED_BOTH” in the extendedstate property of the Jframe .

    Good luck !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s