Making a hello world app in a Form in Intellij
Solution 1
- Go to the class with the same name as the form.
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.
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).
devoured elysium
Updated on March 03, 2020Comments
-
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 almost 11 yearsNote: 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 about 8 yearsWhat is
mainPanel
here? -
Dims about 8 yearsHow can it work at all? Where did you set path to form file? Who processed this file?
-
Vrakfall over 7 yearsIt 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 over 7 years@Vrakfall I'm using 2016.2 and I still see the menu item "Form main()" as described in my answer.
-
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 over 7 yearsI do not have the option either @Vrakfall did you ever figure it out?
-
Vrakfall over 6 yearsNope. I moved on to other things for now. I'll try to leave an event trigger in my brain in case this happens. :P