8

I have a Gradle project and I am trying to create a GUI (Swing, using Intellij); however, I keep receiving an error on compile. The exact same GUI code can be run on a standard Java project and compiles fine.

GUI

package gui;
import javax.swing.*;

public class ApplicationGUI {
    private JPanel rootPanel;
    private JLabel testLabel;

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

Gradle

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'ca.uhn.hapi.fhir:hapi-fhir-base:3.7.0'
    compile 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:3.7.0'
    compile 'ca.uhn.hapi.fhir:hapi-fhir-client:3.7.0'
}

Error

Task :ApplicationGUI.main() FAILED Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null. at javax.swing.JRootPane.setContentPane(JRootPane.java:621) at javax.swing.JFrame.setContentPane(JFrame.java:698) at gui.ApplicationGUI.main(ApplicationGUI.java:11)

In my settings I have the GUI Designer set to Java Source Code.

What am I doing wrong?

Cheers

EDIT1:

I can confirm that the above code works 100% fine with Maven..

Liam G
  • 771
  • 2
  • 9
  • 26
  • 4
    This has nothing to do with gradle, `rootPanel` is `null`, and you can't set a `null` value as the content pane . – Arnaud Apr 25 '19 at 07:43
  • Well I don't have to set anything in my other project without Gradle? When I initialise it I don't have any of my GUI components? As in, they are not present/visible on the form. – Liam G Apr 25 '19 at 07:50
  • please find a solution [there](https://stackoverflow.com/questions/24185378/intellij-idea-13-ui-designer-and-automatic-gradle-building) ! – dinguot May 06 '19 at 14:53

4 Answers4

19

Solved by changing intellij settings:

I've checked:

  • automatically import ...
  • build and run using - set - intellij
  • run tests using - set - intellij
  • Gradle JVM - set - Use project JDK

Screenshot of IntelliJ

My code was:

package com.mygdx.game.desktop;

import javax.swing.*;
import com.badlogic.gdx.backends.lwjgl.LwjglAWTCanvas;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MainForm {
    private JPanel mainpanel_1;
    private JPanel visualizationPanel_1;
    private JButton button1;

    private static MyGdxGame visualization;
    private JFrame mainFrame;

    public MainForm(JFrame frame) {
        mainFrame = frame;
    }

    public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("My First Swing Example");
            frame.setResizable(false);
            frame.setMinimumSize(new Dimension(1300, 850));
            frame.setSize(1300, 850);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            MainForm mf = new MainForm(frame);
            if(mf.mainpanel_1 != null) {
                frame.setContentPane(mf.mainpanel_1);
            }

            visualization = new MyGdxGame();

            LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
            config.width = 1200;
            config.height = 800;
            config.forceExit = false;
            config.resizable = false;
            LwjglAWTCanvas lwjglCanvas = new LwjglAWTCanvas(visualization, config);
            mf.visualizationPanel_1.add(lwjglCanvas.getCanvas());

            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    lwjglCanvas.stop();
                    System.exit(0);
                }


            });

            frame.pack();

            // Setting the frame visibility to true
            frame.setVisible(true);
        });
    }


}
Draken
  • 3,134
  • 13
  • 34
  • 54
Andrew
  • 221
  • 2
  • 4
2

Just change execution from Gradle to Intellij won't fix the main problem because it will keep not working when the Gradle jar were built.

I did the following to fix the problem and keep using Gradle.

Into Editor -> GUI Designer -> Generate GUI into, Choose Java Source

add Intellij forms dependency to your gradle project

implementation 'com.intellij:forms_rt:7.0.3'

Go the the GUI for editor and do some change (like create a new component or change a label) this way Intellij will generate the necessary code.

Run the main class

deFreitas
  • 4,196
  • 2
  • 33
  • 43
0

This seems to be an issue between Gradle and Intellij. A solution is to migrate to Maven. The .form file (generated by Intellij) for the GUI should be initialised at the line frame.setContentPane(new ApplicationGUI().rootPanel); but this does not happen. There are no issues when using Maven.

Liam G
  • 771
  • 2
  • 9
  • 26
-1

As mentioned in the comments, your rootPanel is never initialized thus it's null. JFrame has a jpanel contentPane already by default so you don't really need the below line, unless you intend to replace it :

frame.setContentPane(new ApplicationGUI().rootPanel);

public void setContentPane(Container contentPane)

Sets the contentPane property.This method is called by the constructor. Swing's painting architecture requires an opaque JComponentin the containment hiearchy. This is typically provided by thecontent pane. If you replace the content pane it is recommended youreplace it with an opaque JComponent.

Below's some sample code creating a frame and adding a label to it.

public class ApplicationGUI{
    public static void main(String[] args) {
        JLabel aLabel = new JLabel("some text");
        JFrame frame = new JFrame("ApplicationGUI");
        frame.getContentPane().add(aLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • Okay so this lets the GUI be shown but none of the contents are visible? – Liam G Apr 25 '19 at 08:11
  • Also, how is the rootPanel null? Doesn't the `new` keyword sort that out? – Liam G Apr 25 '19 at 08:21
  • _none of the contents are visible_ : because you didn't add any contents to your JFrame. _Doesn't the new keyword sort that out_ : No because it's called on your class ApplicationGUI, and not on its instance variable rootPanel. To initialize it you can do `private JPanel rootPanel = new JPanel();` – nullPointer Apr 25 '19 at 08:24
  • Okay so how do I add them to the JFrame? `frame.add(testLabel)` doesn't work because it doesn't recognise `testLabel`. Also I tried to initialise the JPanel like that but nothing changed. – Liam G Apr 25 '19 at 08:32
  • 1
    Okay so that works, but I have an `ApplicationGUI.form` file with all of my elements inside it. How would I attach this file or at least include all of its elements in their correct position? – Liam G Apr 25 '19 at 08:39
  • I am not familiar with IntelliJ, please read some tutorials on how to load .form files – nullPointer Apr 25 '19 at 08:51