Problem with JDialog in Netbeans?

Netbeans supports Java programmer a lot. Especially in designing graphical user interfaces (GUIs). Matisse –  the built-in GUI-Builder – is a great help in building GUIs exploratively.

Unfortunately you run in errors if you use Netbeans’ JDialog template in existing projects. But solving this issue is not a big deal.

This how-to describes how to integrate a JDialog in an existing project and how you have to invoke it. Of course this tutorial is made for novices.

Differences between JDialog and JFrame

The main difference between both components is the optional property modal of JDialog. That means that you have to close the dialog before you can go ahead with the program.

If a JDialog is not modal it looks alike a JFrame. But in Windows only a JDialog app does not appear in the taskbar.

Create a JDialog

I recommend to create a JDialog in a new class always. That increases the clarity of the code. But it is useful to put it in the same package of the invoking class if nothing speaks against it.

Use Netbeans JDialog-Wizard for this task. Create the JDialog with “File | New…”. In “Categories” choose” Swing GUI Forms” and as a file type JDialog Form.

Screenshot Netbeans

Unfortunately Netbeans create the JDialog class with a main-method. In almost all cases this make no sense. Especially if you try to add a JDialog to an existing project where a main-method already exists generally.

To solve this problem, delete the main method or comment it out.

Netbeans

If the app contains the JDialog class only, you have to preserve the main method of course.

Now you can design the dialog with Matisse as you like.

Invoke JDialog from another class

If your JDialog class is named “NewJDialog” you can call it as described below:

NewJDialog dialog = new NewJDialog(this, true);
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);

You have to rename NewJDialog to your JDialog class name. The assigned parameter true to the constructor makes the dialog modal. Refer information above.

Invoking the method setLocationRelative with this, centered the dialog on the JFrame from which JDialog is invoked. This is useful on huge screens and not maximized windows because of less mouse distances. null instead of this centered the dialog on the screen.

setVisible is self-explanatory.

Edit template of JDialog Form

To avoid deleting the main method every time you create a new JDialog, altering or creating a new template solves the problem.

Go to menu “Tools | Templates” open the group “Swing GUI Forms”. Choose “JDialog Form” and click on the button “Duplicate”:

Netbeans - Tpl duplicate

Give the new template a new template and file name:

Netbeans - Tpl new name

Finally open the new template in the editor …

Netbeans - Tpl open editor

… delete the main method as described above and save it.

Now the new template is available at “File | New File … | Swing GUI Forms”.

Conclusion

At existing projects Netbeans’ well-meant integrated main method in new JDialog classes is more an annoyance than a help. But it easy to delete. I do not know why Netbeans do not offer the opportunity to omit the main method creation in the JDialog wizard.

Invoking JDialogs created by Netbeans happens as usual through the constructor.

Picture Credits

Netbeans Screenshot: Public Domain (CC0)


3 comments

Leave a Reply to sartoris Cancel reply

Your email address will not be published. Required fields are marked *