I'm developing a simple Java application using NetBeans and its GUI editor. I'm stuck on creating a simple dialog: runtime it looks different from what I designed and from what is previewed in the editor. Basically, clicking on a button make my dialog appears.

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
       PanelDialogNuovoCliente pan = new PanelDialogNuovoCliente();
       JDialog jd=new JDialog();
       jd.setTitle("Nuovo Cliente");
       jd.setMinimumSize(new Dimension(500, 400));

The problem is that when the dialog appears it has a different look, the dialog window seems smaller and not all the components fit in it.

PanelDialogNuovoCliente is just a JPanel with some labels and JTextField.

Maximum, Minimum and Preferred sizes are all set to (500,400) from the JPanel properties in the editor.

JDialog minimum size is set to (500,400) from the code that I snipped.

Unfortunately I can't post a screenshot because I need at least 10 reputation but when I run the application Dialog's window is smaller compared to the one that I can see from the preview button in NetBean's GUI editor.

EDIT: Here's the screenshot. Runtime JDialog is on the left, while preview of it in netbeans is on the right. I tried to call JDialog#pack() just before setVisible(true) without success. I set nimbus look and feel for my app. Anyways if i try to preview the design from ide with nimbus l&f it looks perfect so i don't think that this one is the real problem enter image description here


Perhaps what you're experiencing is similar to this post:

NetBeans (Java Swing): Set window size

I remember experiencing something similar and shared my experiences in a post there.

Edit: (28/05/2015)

Just to clarify/elaborate, here are steps I've got to replicate (and resolve) the issue I encountered, which might be what you've faced.

Problem Replication Steps

  1. Create a new project with "File >> New Project..."
  2. Choose "Java >> Java Desktop Application"
  3. Click 'Next' button
  4. Project Name: "TestApp", then "Finish" button - You then get two tabs opening up in the Matisse editor, "TestView.java" (the app's main window) and "TestAboutBox.java".
  5. In the Matisse editor, I re-size the window to be a large size.
  6. I then press Ctrl+F5 to run it
  7. It runs and the window is the same size as in the ide.
  8. Upon closing the app, it writes data to a "~/.TestApp/mainFrame.session.xml" file on my linux system (I think this equates to "%APPDATA%\CompanyName\TestApp\mainFrame.session.xml" on a windows system)
  9. Taking a look inside this "mainFrame.session.xml" file, I see there's a "mainFrame" node that contains the x, y, width and height of the window.
  10. Back in the mattisse editor, I resize the window to be smaller.
  11. I then press Ctrl+F5 to run it again
  12. The app's window then appears at the larger size (ie, it didn't abide by the smaller size specified in the IDE)


I tried the workaround suggested in Tomas Pavek's post here:


Basically, these steps:

  1. Delete this "mainFrame.session.xml" file (or the folder that contains it)
  2. then do CTRL+F5 to run the app again

...and hey presto! It appears at the correct size that the IDE had specified.

  • Setting the window's (min | max | pref) size is not the solution but the problem. Swing is designed to work with layout managers because the app might run in different platforms with different Look and Feels. In this context fixed sizes are highly discouraged. As @MadProgrammer already mentioned the OP should call JDialog#pack() method instead which will call the the layout managers and make the dialog fit its content pane. – dic19 May 26 '15 at 12:19
  • 1
    Fair enough if that's the java way. I just have memories of reading forum posts advising the calling of pack() and it not working for me also. So I dug a bit further and shared what I could in forum post I mentioned before. My main insights were from here: forums.netbeans.org/ptopic28011.html The post from Tomas Pavek, he mentioned swing storing the size of your window so that it can restore it upon next open, which helped explain why the op there (and me, and possibly the op here) saw that the app no longer abided by the size we specified in the IDE's designer. – Gurce May 26 '15 at 22:22
  • 1
    It's the first time I read such a thing. I don't know if it's accurate, maybe it is, but I do know that there are two differences between preview design and runtime: 1) The Look and Feel: preview design uses System L&F and in runtime L&F is set to Metal L&F by default (unless we set it explicitly) 2) Matisse (NetBeans GUI editor) uses GroupLayout as layout manager which is designed for GUI builders. This layout manager uses constraints to make the components fit the window but those constraints usually break when the L&F is different (point 1). – dic19 May 27 '15 at 13:15
  • 1
    Cheers, I'll try to take those points on-board in my next java app dev foray. As a quick check of Tomas Pavek's scenario, I've jotted down some steps that replicate the issue I encountered. I'll add those notes to my answer, just in-case it's the same scenario the op is facing. – Gurce May 27 '15 at 23:32
  • 1+ for the update :) – dic19 May 28 '15 at 13:59

Your Answer

