I’m trying to make a custom JDialog using windowBuilder, but at the very beginning I faced some issues. So here they are:
I created simple Jdialog using windowBuilder. Here it’s code:
public class GroupFrame extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
GroupFrame dialog = new GroupFrame();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public GroupFrame() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
But then I want to change public static void main(String[] args) to something like public void show().
The new show() method code is:
public void show() {
try {
GroupFrame dialog = new GroupFrame();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
So when I’m triyng to call this changed method using the following code:
GroupFrame groupFrame = new GroupFrame();
groupFrame.show();
I’ve got the StackOverflowError (what a coincidence! ;D):
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Dialog.<init>(Unknown Source)
at java.awt.Dialog.<init>(Unknown Source)
at javax.swing.JDialog.<init>(Unknown Source)
at javax.swing.JDialog.<init>(Unknown Source)
at javax.swing.JDialog.<init>(Unknown Source)
at UILayer.GroupFrame.<init>(GroupFrame.java:32)
at UILayer.GroupFrame.show(GroupFrame.java:21)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at UILayer.GroupFrame.show(GroupFrame.java:23)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at UILayer.GroupFrame.show(GroupFrame.java:23)
(etc...)
Could anybody point me on what I’m doing wrong?
Thanks in advance!
You construct a GroupFrame, and the call the show() method, which constructs another GroupFrame. This is already wrong. But it’s even more wrong because then you call
setVisible(true)which internally calls theshow()method that you inadvertently overrode.The show method should be named something else, and should be static:
This method would thus be called as this:
And please, don’t catch Exception. And when you do it, don’t swallow the exception.