7

I can't seem to run a form on IntelliJ GUI builder

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.

I assume the code to initialize the views are auto generated. Right now I only have a JPanel and somehow it is not auto-initialized even thought it's clearly visible on the designer.

It's a Gradle project and I've chosen to run with the generated main function.

What do I have to do to get it working?

public class MyForm {

    private JPanel jPanel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyForm");
        frame.setContentPane(new MyForm().jPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2798694
  • 1,023
  • 11
  • 26

2 Answers2

20

By default, the IntelliJ IDEA UI designer works by generating bytecode, which is unfortunately not supported with Gradle builds. You can change it to generate source code in Settings | Editor | GUI Designer.

yole
  • 92,896
  • 20
  • 260
  • 197
  • 3
    This worked, but doing a gradle rebuild didn't fix it right away until I had specifically right clicked on the Forms Java file and selected recompile. – Kevin Sadler Jul 24 '17 at 09:55
  • I had the same problem with Gradle but this answer did not solve it, even after rebuilding the Form separately. I ended up converting the project to Maven and now it compiles and runs just fine. – moonlightcheese May 26 '19 at 08:05
3

see Intellij (Swing) GUI not compiling because of Gradle

solved by changing intellij settings (also see the screenshot)

i've checked:

  • automaticaly import ...
  • build and run using - set - intellij
  • run tests using - set - intellij
  • Gradle JVM - set - Use project JDK
Andrew
  • 221
  • 2
  • 4