Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java can't call simple JDialog built using eclipse WindowBuilder

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!

like image 635
mr.nothing Avatar asked May 10 '12 10:05

mr.nothing


People also ask

How do I run WindowBuilder?

By default, java files will be opened by the java editor. To open with the windowbuilder editor, right click on a java file and select "Open With/Windowbuilder editor".

What is JDialog in Java?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.


2 Answers

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 the show() method that you inadvertently overrode.

The show method should be named something else, and should be static:

public static GroupFrame createAndShow() {
    GroupFrame dialog = new GroupFrame();
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);
}

This method would thus be called as this:

GroupFrame.createAndShow();

And please, don't catch Exception. And when you do it, don't swallow the exception.

like image 165
JB Nizet Avatar answered Sep 28 '22 07:09

JB Nizet


But then I want to change public static void main(String[] args) to something like public void show().

You cannot do that, main method with the exact signature is the starting point of a Java program (for most of the cases but not all).

What you need is a separate show method which will make your dialog visible

EDIT:

Noticed your class extends JDialog, which means if you define show again, you are technically overriding the method present in Dialog, moreover show is deprecated.

From the docs:

Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).

Makes the Dialog visible. If the dialog and/or its owner are not yet displayable, both are made displayable. The dialog will be validated prior to being made visible. If the dialog is already visible, this will bring the dialog to the front. If the dialog is modal and is not already visible, this call will not return until the dialog is hidden by calling hide or dispose. It is permissible to show modal dialogs from the event dispatching thread because the toolkit will ensure that another event pump runs while the one which invoked this method is blocked.

Instead use this:

public static void showMyDialog(){
    try {
         GroupFrame dialog = new GroupFrame();
         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
         dialog.setVisible(true);
    } catch (Exception e) {
         e.printStackTrace();
    }
}

and in your main method you call showMyDialog.

But main method has to be present in your class if you want it to run as a Java Application, if some other class is going to call your show method then you dont need main method existence in your class.

like image 31
mprabhat Avatar answered Sep 28 '22 05:09

mprabhat