Here is my project: https://github.com/Kolyall/GUIExample
MainClass
import javax.swing.*;
public class MainClass {
public static void main(String[] args) {
SwingUtilities.invokeLater(MainFrame::new);
}
}
MainFrame
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame {
public MainFrame() {
super("MainFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(850, 650));
setSize(new Dimension(850, 650));
JPanel rootPanel = new JPanel();
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS));
getContentPane().add(rootPanel);
setLocationRelativeTo(null);
setVisible(true);
ButtonsFrame buttonsFrame = new ButtonsFrame()
rootPanel.add(buttonsFrame.buttonsPanel);//java.lang.NullPointerException in this line
}
}
ButtonsFrame
import javax.swing.*;
public class ButtonsFrame {
public JButton button1Button;
public JButton button2Button;
public JPanel buttonsPanel;
}
ButtonsFrame.form
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.github.kolyall.test.ButtonsFrame">
<grid id="27dc6" binding="buttonsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="70295" class="javax.swing.JButton" binding="button1Button" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Button1"/>
</properties>
</component>
<vspacer id="3d1dd">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="76c52" class="javax.swing.JButton" binding="button2Button" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Button2"/>
</properties>
</component>
</children>
</grid>
</form>
But after run of MainClass.main()
The error occurs:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1095)
at java.awt.Container.add(Container.java:419)
at com.github.kolyall.test.MainFrame.<init>(MainFrame.java:29)
Seems like IntelliJ IDEA doesn't generate binary class files, but the option is on, I also tried witn "Java source code"
UPDATE: It's known issue of Idea Why swing GUI form builder doesn't generate binary class files/java source code?