Java cannot call a simple JDialog built using eclipse WindowBuilder

I am trying to create a custom JDialog using windowBuilder, but at the very beginning I had some problems. So here they are:

I created a simple Jdialog using windowBuilder. Here is the 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 () .

New show () method:

 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 triya to call this modified method using the following code:

 GroupFrame groupFrame = new GroupFrame(); groupFrame.show(); 

I have a 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...) 

Can someone tell me what I am doing wrong?

Thanks in advance!

+6
source share
2 answers

You create a GroupFrame and call the show () method, which creates another GroupFrame. This is already wrong. But this is even more wrong, because you call setVisible(true) , which internally calls the show() method, which you inadvertently redo.

The show method must be called something else and must be static:

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

Thus, this method can be called the following:

 GroupFrame.createAndShow(); 

And please do not catch the exception. And when you do, do not swallow the exception.

+4
source

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

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

You need a separate show method that will make your dialog visible.

EDIT:

It is noted that your class extends JDialog , which means that if you define the show again, you technically redefine the method present in Dialog , and show is notrecated.

From the docs:

Outdated. Starting with JDK version 1.5, it is replaced with setVisible (boolean).

Makes the dialog visible. If the dialog and / or its owner have not yet set displayable, both become displayable. The dialogue will be before it becomes visible. If the dialogue is already visible, this will lead to dialogue in the foreground. If the dialog is modal and not yet visible, this call will not return until the dialog is hidden by calling hide or dispose. It is allowed to show modal dialogs from the event dispatch thread because the toolkit will start another event pump while the caller is blocked.

Use this instead:

 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 the main method, you call showMyDialog .

But the main method must be present in your class, if you want it to run as a Java Application , if some other class will call your show method, then you do not need the main method to exist in your class.

+3
source

Source: https://habr.com/ru/post/915384/


All Articles