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.
On the other hand, initializing only JPanel, resultd in plane window only with title.