3

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));
       jd.setLocationRelativeTo(null);
       jd.add(pan);
       jd.setModal(true);
       jd.setVisible(true);   
    }

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

Razinar
  • 727
  • 3
  • 13
  • 21
  • 2
    Use `JDialog#pack` and take a look at [How to set the look and feel](https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html) – MadProgrammer May 26 '15 at 10:27
  • See these related Q&A: [jSeparator looks - Preview Design vs Run File (netbeans)](http://stackoverflow.com/a/25066510/1795530) and [Java: Look and Feel](http://stackoverflow.com/a/22760337/1795530). – dic19 May 26 '15 at 11:58
  • Note: you have 11 points now so you'll be able to post pictures soon. – dic19 May 26 '15 at 12:04

1 Answers1

2

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)

Workaround

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

http://forums.netbeans.org/ptopic28011.html

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.

Community
  • 1
  • 1
Gurce
  • 592
  • 7
  • 18
  • 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: http://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
  • Hi sir. I can't find my session.xml in my appdata. Do you have any ideas where I can find them? There are really no sessions.xml and i can't really find my netbeans project in the appdata. there's a netbeans folder in my appdata but it does not contain my session.xml. I'm looking for your response. I appreciate even an "I'm sorry. as of now, I don't have an idea," sir! A reply will be greatly appreciated! – jerome_mjt Oct 03 '17 at 12:50
  • Hi Jerome, sorry I don't have any good answers off-hand. Just curious though, what version of Netbeans are you using? As for myself, I tend to use versions between v6.8 to v7.0.1. Were you able to replicate the issue with the steps I provided? See the "Problem Replication Steps" above and try make a simple dummy project following those steps. If the issue triggers for you also, it can be some confirmation that we've got the same issue in common. – Gurce Oct 05 '17 at 05:01
  • I'd better clarify a mistake, I had said: "swing storing the size of your window so that it can restore it upon next open" This isn't the case. Swing itself doesn't do this, but a defunct framework built on-top of Swing called the "Swing Application Framework" (SAF, for short). SAF tried to do helpful things for the benefit of your app, such as let the app preserve its last position+size upon restarting. In this case though, the help became a hindrance. Older versions of NB used this framework (about v6-v7?) before they moved onto their own framework called Netbeans Platform. – Gurce Jun 24 '19 at 05:17