1

I'm trying to create simple java application, using IntelliJ Swing UI designer. To show my problem, i've created this sample project

import javax.swing.*;
import java.awt.*;
public class GUIForm extends JFrame {
    private JButton firstButtonButton;
    private JButton secondButtonButton;
    private JPanel mainPanel;

    public GUIForm() throws HeadlessException {
        setTitle("MyApp");
        setContentPane(mainPanel);
        setVisible(true);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
public class Main {
    public static void main(String[] args) {
        GUIForm guiForm = new GUIForm();
    }
}

And GUIForm.form file looking like this form

When i'm running Main in IntelliJ, everything looks fine: GUI

However, when i'm trying to run app, i'm getting error

D:\Projects\Java\testSwing\src\main\java>javac Main
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
        at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
        at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:680)
        at GUIForm.<init>(GUIForm.java:11)
        at Main.main(Main.java:3)

Is there some step, that i'm missing? I can initalize those components, and even set them their properties, but then what's the point of that graphic tool to design UI? Summarizing, Can i include data from the *.form file to the final *.jar file?

[EDIT]

I tried following changes:

import javax.swing.*;
import java.awt.*;

public class GUIForm extends JFrame {
    private JButton firstButtonButton = new JButton();
    private JButton secondButtonButton = new JButton();
    private JPanel mainPanel = new JPanel();

    public GUIForm() throws HeadlessException {
        super();
        setTitle("MyApp");
        setContentPane(mainPanel);
        setVisible(true);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel.add(firstButtonButton);
        mainPanel.add(secondButtonButton);
    }
}

And this somewhat shows my problem. I did initalize those components, added them to mainPanel, but in effect i've created new buttons, that have nothing to do with the buttons in GUIForm.form file, none of thier parameters are being propageted to graphical interface. GUIAfterAddingButtons

On the other hand, initializing only JPanel, resultd in plane window only with title.

Mike S
  • 11
  • 2
  • 1
    You never initialized `mainPanel` i.e. `mainPanel = new JPanel()`... Also, you should call `super()` in your constructor, since it extends `JFrame`. – Mr. Polywhirl Jul 17 '23 at 16:09
  • Please see the comments in https://youtrack.jetbrains.com/issue/IDEABKL-6689/. – CrazyCoder Jul 17 '23 at 16:14
  • What does this question have to do with Maven? Please explain the connection because I don't see it. Also, the error message is telling you *exactly* what is wrong, no? – Hovercraft Full Of Eels Jul 17 '23 at 16:22
  • @HovercraftFullOfEels Please read the issue linked above. Maven does not compile IntelliJ IDEA form files, so bytecode is not instrumented into classes and the UI components are not initialized. The issue I linked mentions several workarounds to this problem. If you don't understand how the question is related to Maven, just skip it. – CrazyCoder Jul 17 '23 at 17:00
  • After apllying hints from linked solutions, everything seem to work fine. Thank you kindly for your support! – Mike S Jul 17 '23 at 19:34

0 Answers0