Making a hello world app in a Form in Intellij

26,394

Solution 1

  1. Go to the class with the same name as the form.
  2. Press the keyboard shortcut for "Generate". It's Ctrl+N on Mac OS X, Alt+Ins on Windows. Alternatively, from the menu, select menu Code → Generate.

  3. Select "Form main()".

Now the main method is written and inserted for you. It will look something like this:

public static void main(String[] args) {
    JFrame frame = new JFrame("MyForm");
    frame.setContentPane(new MyForm().mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

Solution 2

I just did my first Intellij Swing App. Steve McLeod has the right instructions, however, when I tried to generate the main method using Alt+Insert => Generate main, I received an error message about one of my panels not being bound. So I clicked on the gui designer page (.form), selected my top panel, and gave it a name.

Everything else was named for me, but for some reason, the panel name was blank. Once I did that, I was able to switch over to the form .java class, press "Alt+Insert" and generate a constructor (not required, but needed).

From there, I followed Steve's advice to generate a main method. One thing that threw me off was the expectation that my Intellij generated .java class would extend or implement something swing related - it didn't. Swing only shows up in the Intellij generated main method (besides the private variables).

Share:
26,394
devoured elysium
Author by

devoured elysium

Updated on March 03, 2020

Comments

  • devoured elysium
    devoured elysium about 4 years

    I'm trying to make a hello world form in Intellij. I've created the form, but the question now is what code to make in main() to make the form run and show up?

    PS: all the tutorials around seem to only focus on "how to do forms on intellij" not in "how to actually make it run, then".

    Thanks

  • texnic
    texnic almost 11 years
    Note: The context menu that appears when pressing Alt+Ins depends on the position of cursor in the form java-file. If it is in the top two lines (above the comment lines /** Created with IntelliJ IDEA, the menu will only contain Generate Copyright. In other lines, all the methods mentioned in the answers here appear.
  • Dims
    Dims about 8 years
    What is mainPanel here?
  • Dims
    Dims about 8 years
    How can it work at all? Where did you set path to form file? Who processed this file?
  • Vrakfall
    Vrakfall over 7 years
    It sadly looks like the automatic generation option isn't present at all in the tool-tip on the version 2016.2. Sadness. Sadder that this is still in the official documentation. If someone please would like to report it to JetBrains, I'm in a rush and don't really have the time to do it for quite a long time.
  • Steve McLeod
    Steve McLeod over 7 years
    @Vrakfall I'm using 2016.2 and I still see the menu item "Form main()" as described in my answer.
  • Vrakfall
    Vrakfall over 7 years
    @SteveMcLeod Can you please screenshot it to me please? On my end, I don't see it at all. Look at my screenshot. Do I have to force an implementation or inheritance?
  • heinst
    heinst over 7 years
    I do not have the option either @Vrakfall did you ever figure it out?
  • Vrakfall
    Vrakfall over 6 years
    Nope. I moved on to other things for now. I'll try to leave an event trigger in my brain in case this happens. :P