1

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! :)

JT White
  • 517
  • 4
  • 9
  • 21
  • 1
    The code you've provided appears to be just fine - why do you think this has something to do with NetBeans? What happens if you try this in Eclipse or even from the command line? Also, you might want to post some more of your code so we can see if there's anything that might be cancelling this out. – no.good.at.coding Mar 06 '11 at 00:09
  • remove `myFrame.getExtendedState()` and leave only JFrame.MAXIMIZED_BOTH. The states are not (bit)set. *Toolkit.isFrameStateSupported returns always false on compound state even if all parts are supported; if part of state is not supported, state is not supported. MAXIMIZED_BOTH is not a compound state.* – bestsss Mar 06 '11 at 00:09
  • @trashgod, read the remark, I know well what the doc states (the remark is from the `private boolean java.awt.Frame.isFrameStateSupported(int state)` – bestsss Mar 06 '11 at 00:17
  • @bestsss: Ah, I missed your comment update. I'm not sure how your suggestion would change anything. I see the same result either way. – trashgod Mar 06 '11 at 00:29

5 Answers5

5

to maximize your form at startup you have to let netbeans do it in its rigth time! You can accomplish this through the JFrame's windowOpened event:

In the JFrame's Properties window, click the Events button;

Click the ellipsis (...) button next to the windowOpened event;

In the Handler dialog box, add a handler called formWindowOpened (as suggested by NetBeans);

Within the formWindowOpened method in the Source Editor, paste the following code:

Code:

    setExtendedState(JFrame.MAXIMIZED_BOTH); 

Good luck!

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
5

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
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    the problem is that the states cannot be set via OR even if the doc states they are bitset, simply you cant have NORMAL | MAXIMIZED_XXX state at the same time – bestsss Mar 06 '11 at 00:16
  • 1
    @bestsss: Sorry, I'm not following: `2 | 4 == 6`. – trashgod Mar 06 '11 at 00:35
  • @bestsss: Not at all; I appreciate the opportunity to clarify my understanding. – trashgod Mar 06 '11 at 00:49
  • @trashgod Thank you so much, this worked perfect... I think I understand now. You've been a great help! Hopefully this will show on Google for others to view as well! – JT White Mar 08 '11 at 21:20
  • @JT White: Excellent! You can accept this answer by clicking on the gray check-mark so it turns green. – trashgod Mar 08 '11 at 21:34
1

Just insert the code bellow

public Project () {
  setExtendedState(MAXIMIZED_BOTH);
}
Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52
0

Put the code below to the initComponents();

public Home() {
        initComponents();
        this.setExtendedState(MAXIMIZED_BOTH);
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Put the below code above initcomponents();:

 public Test() {
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);

    this.setUndecorated(true);
    //use this command to remove the maximize,minimize,close option from the 
    //title.        

     initComponents();

 }
Endre Both
  • 5,540
  • 1
  • 26
  • 31