Application Window Size Problem.
Hi,
Using: NetBeans IDE 6.8 on Linux/Ubuntu
Note: V. 6.8 is the current release version for the Ubuntu package.
I'm writing a Java desktop app, when I run the application the initial size of the application's window is less than half the width of the main panel and less than half the height as well, as designed in the designer. The application consists of a jPanel (the main window) which contains 3 jPanels each of which contains various components, jTextFields, jComboBoxes, jLabels and jButtons.
It can be manually resized to the correct size but I can't get it to start at the correct size. I've tried changing the size with setSize and preferredSize to various values and the minimumSize as well, but no matter what sizes I enter the application is always the same initial size when I run it, less than half of what it should be in width and height.
How do I get the app to start at the exact size of main panel as I have designed it in the design editor?
Here is a screen cap of the size when the app starts:
http://www.zilefile.com/files/12394_...pStartSize.png
Here is a screen cap of the size when it's been resized (manually) to the correct size:
http://www.zilefile.com/files/12395_...avaAppFull.png
Many thanks.
Re: Application Window Size Problem.
What methods are you calling initially that would affect the size? Do you call pack()?
Re: Application Window Size Problem.
Quote:
Originally Posted by
Norm
What methods are you calling initially that would affect the size? Do you call pack()?
None that I am aware of (or that I can find), and no I am not calling pack(), should I be?
Thanks.
Re: Application Window Size Problem.
No, pack and setSize dont' work well together.
I don't understand why setSize() doesn't work. Can you make a small program that compiles and demonstrates the problem and post it here?
I just noticed that an IDE is involved. What code does it put in the GUI that could effect the window size?
Re: Application Window Size Problem.
Calling pack() results in the Window being sized to the size computed by the layout manager(s) and is generally the best way to ensure you window appears at the optimal size to display all of the components.
Calling setSize() on the frame will force the frame to the specified size which may mean some of the components do not display properly. Calling setSize() on individual components is likely to have no effect as most layout managers use one or a combination of minimumSize, preferredSize and/or maximumSize.
Of course if you aren't using a layout manager then calling pack() probably has no effect and I guess you have to calculate the required frame size yourself. I say guess because I always use layout managers, I've never yet found a reason not to.
Re: Application Window Size Problem.
Thanks guys and sorry to take a week (almost) to reply again - I had a bit of a minor family crisis and had to go away for a few days.
I think my problem stemmed from switching between layout managers several times early on. Having wasted several hours on the size problem and never actually succeeding in changing the size programatically at all, I decided to start the GUI again from scratch this morning. 2 hours later and I am finished and all is excellent. I must confess to finding building non-trivial Java GUIs very hard work, even using Free Design with Netbeans. But them I'm hardly the first person to say this.
Thanks again.
Re: Application Window Size Problem.
Quote:
I must confess to finding building non-trivial Java GUIs very hard work, even using Free Design with Netbeans. But them I'm hardly the first person to say this.
Personally I haven't found a GUI builder that I like (although to be fair I stopped looking at them several years ago) so i just hand code the GUI. It's not hard to do when you get used to it.
Re: Application Window Size Problem.
Quote:
Originally Posted by
keang
Personally I haven't found a GUI builder that I like (although to be fair I stopped looking at them several years ago) so i just hand code the GUI. It's not hard to do when you get used to it.
That sounds like a good idea, and one I will try.
Can I ask what layout manager you use for an app like this (image below), GridBagLayout?
http://www.zilefile.com/files/12395_...avaAppFull.png
Thanks.
Re: Application Window Size Problem.
You have 3 distinct panels in that frame, the problem here is making sure the text boxes align across the panels. Assuming exact alignment is not necessary I'd probably do most of this with a layout manager I wrote for laying a components in variable sized grids. You can get it from here
The main panel would have a VariableGridLayout and be setup with a single column. The 3 sub-panels are added to it.
Each sub-panel would have a VariableGridLayout with the requisite number of columns. The components would be added to each panel in a left to right and top to bottom order. The only issue is in the top panel where the second column has 2 rows with an input field split into two. For both these rows you need to place the 2 input fields into a panel with a GridLayout with 2 columns and add this panel rather than the individual components.
Each sub panel also requires a TitledBorder to give the etched edge and title.
Re: Application Window Size Problem.
Quote:
Originally Posted by
keang
You have 3 distinct panels in that frame, the problem here is making sure the text boxes align across the panels. Assuming exact alignment is not necessary I'd probably do most of this with a layout manager I wrote for laying a components in variable sized grids. You can get it from
here
The main panel would have a VariableGridLayout and be setup with a single column. The 3 sub-panels are added to it.
Each sub-panel would have a VariableGridLayout with the requisite number of columns. The components would be added to each panel in a left to right and top to bottom order. The only issue is in the top panel where the second column has 2 rows with an input field split into two. For both these rows you need to place the 2 input fields into a panel with a GridLayout with 2 columns and add this panel rather than the individual components.
Each sub panel also requires a TitledBorder to give the etched edge and title.
Many thanks for the how-to guide. I'll try it when I have an afternoon free, sometime in 2013 probably - just kidding. :)
Re: Application Window Size Problem.
If you do need the components to line up over the panels I suppose you could set the preferred size of the components so the layout manager handles each panel in the same manner.
As for using GridBagLayout, yes you can use that but it is very complex and generally there are ways of doing layouts with multiple panels and layout managers that give similar results with less complexity.
Re: Application Window Size Problem.
Hi keang,
Firstly I'm going to give your VariableGridLayout class a go today.
BUT the links on your download page for Date Selector, Swing Extras, and Hexagonal Buttons all link to DateSelector.zip.
I managed to download SwingExtras.jar by guessing the URL which then gave me this page, the likes of which I've never seen before, have a look:
http://www.keang.co.uk/downloads/SwingExtras.jar
I've no idea if this is the current version. Is it?
I was wondering if you had a simple sample class or project that uses VariableGridLayout which you could post / PM, just to get me started please?
Many thanks.
Re: Application Window Size Problem.
Quote:
BUT the links on your download page for Date Selector, Swing Extras, and Hexagonal Buttons all link to DateSelector.zip.
Thanks for letting me know. I've fixed it now.
Good guess but not quite correct. It's actually in a zip file as the code is there as well as the jar, so you need http://www.keang.co.uk/downloads/SwingExtras.zip. You can either include the jar in your project or just the VariableGridLayout.java file (in the uk\co\keang\swingaddons directory) and paste it into your project remembering to change the package statement.
Re: Application Window Size Problem.
Quote:
Originally Posted by
keang
Thanks for letting me know. I've fixed it now.
No problem.
Any chance of a simple sample class or project that uses VariableGridLayout which you could post or PM me, just to get me started with it please?
Thanks.
Re: Application Window Size Problem.
It's a direct drop in for GridLayout.
The only difference between the two is that with GridLayout you get fixed size cells ie all columns have the same width and all rows have the same height whereas with VariableGridLayout each column is sized to the width of the widest component in the column and each row is sized to the highest component in the row.
Re: Application Window Size Problem.
Okay, I'll give it a go. Fingers crossed. Thanks again.
Re: Application Window Size Problem.
Hi again Keang,
I finally had a full afternoon free to give your VariableGridLayout class a go. It is excellent and works brilliantly, I am pleased with the results so far (still work in progress though). Thanks so much for telling me about it, and also for creating it !!
There is one thing that I just can't work out, which is not your class specific, but maybe you (or anyone reading this) can help with, as I have just wasted an hour trying to solve it.
Look at the image linked below, how do I add some padding between the edges of the TitledBorder and the components inside? In other words so there is a gap on the left between the left side of the border line and the left column of labels, likewise on the right side between the border line and the buttons (also at the top and bottom)?
http://www.zilefile.com/files/12580_..._DB_Screen.png
Many thanks again.
Re: Application Window Size Problem.
Quote:
It is excellent and works brilliantly, I am pleased with the results so far (still work in progress though). Thanks so much for telling me about it, and also for creating it !!
I'm glad you found it useful.
Quote:
how do I add some padding between the edges of the TitledBorder and the components inside?
The easiest way is to use a CompoundBorder which effectively allows you to add two borders. You create the CompoundBorder with the instances of 2 other borders. For the inner border use an EmptyBorder with the padding you require and for the outer border use the TitledBorder you are currently using.
Re: Application Window Size Problem.
EDIT:
OKAY forget all the below - I got the inner and outer borders the wrong way around when creating the CompoundBorder !! Oops.
Thanks again, all sorted. :)
Quote:
Originally Posted by
keang
The easiest way is to use a CompoundBorder which effectively allows you to add two borders. You create the CompoundBorder with the instances of 2 other borders. For the inner border use an EmptyBorder with the padding you require and for the outer border use the TitledBorder you are currently using.
Thanks again. Ok last question as it's the same subject, otherwise I've probably taken your patience with me too far and I'll start a new thread. :)
What you suggest is what I had already done to create some padding between the main window (a JFrame) and the edges of the 3 panels. These 2 lines of code is what I used and it worked perfectly.
Code:
searchPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Search"));
searchPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), searchPanel.getBorder()));
What I am trying to do is add padding on the inside of the 3 panels between the inside edges of the panels and the components inside, I did not realize that it was possible to add borders to components in the same way.
BUT it does not work in the way I wanted. In the top panel ('Search') I added padding to the left of the left col of compnents, the labels (Title, Director, Year, ...), I added padding to the right of the right col of components, the buttons (Search DVDs, Get All DVDs, ...), and to the bottom of the bottom row of components, the Notes label along to the Clear Search button.
See this screen grab - on the left it looks fine, there is a gap between the panel line and the col of labels, but the right side buttons and bottom row is not what I want!
http://www.zilefile.com/files/12581_...Screen_Pad.png
This is the code I used:
Code:
int padTop = 0;
int padLeft = 10;
int padBottom = 0;
int padRight = 0;
titleSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), titleSearchLabel.getBorder()));
directorSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), directorSearchLabel.getBorder()));
yearSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), yearSearchLabel.getBorder()));
lengthSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), lengthSearchLabel.getBorder()));
genresSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), genresSearchLabel.getBorder()));
notesSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), notesSearchLabel.getBorder()));
padTop = 0;
padLeft = 0;
padBottom = 0;
padRight = 10;
searchDVDsButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchDVDsButton.getBorder()));
searchDVDsGetAllButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchDVDsGetAllButton.getBorder()));
searchDVDsMostRecentButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchDVDsMostRecentButton.getBorder()));
searchDVDsRandomButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchDVDsRandomButton.getBorder()));
searchDVDsCustomSqlButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchDVDsCustomSqlButton.getBorder()));
searchClearButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchClearButton.getBorder()));
padTop = 0;
padLeft = 0;
padBottom = 10;
padRight = 0;
notesSearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), notesSearchLabel.getBorder()));
notesSearchField.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), notesSearchField.getBorder()));
orderBySearchLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), orderBySearchLabel.getBorder()));
orderBySearchCombo.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), orderBySearchCombo.getBorder()));
searchClearButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight), searchClearButton.getBorder()));
I then tried using just an empty border and not a compound one, using code like this for all the components, but that had very similar results to the code above.
Code:
titleSearchLabel.setBorder(BorderFactory.createEmptyBorder(padTop, padLeft, padBottom, padRight));
Where have I gone wrong?
Many thanks again and sorry to be so inexperienced with all this.
Re: Application Window Size Problem.
Adding borders to all the components is way too much effort, to get a gap inside the titled border you just need to add a CompoundBorder to the panels containing the titled borders but you need to read the API docs before using a new class.
CompoundBorder's constructor takes 2 arguments, the first argument is the outer border and the second argument is the inner border (personally I would have done this the other way around but we have to work with what we have been given). So you need to add the titled border and then the empty border ie:
Quote:
Border titledBorder = BorderFactory.createTitledBorder("Search"));
Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
searchPanel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder));
Re: Application Window Size Problem.
Quote:
Originally Posted by
keang
CompoundBorder's constructor takes 2 arguments, the first argument is the outer border and the second argument is the inner border (personally I would have done this the other way around but we have to work with what we have been given). So you need to add the titled border and then the empty border ie:
Hi again Keang,
Silly of me to get the inner and outer borders the wrong way around (I take it you saw my last post edit before you replied?).
Everything in the GUI is now done and I thought you might like to see the excellent results I've attained using your VariableGridLayout class. It is so very much better than what I managed using the GUI designer. Many thanks for all your help and indeed the class itself which, I have no doubt, I will use again and again.
Here's the finished GUI:
http://www.zilefile.com/files/12582_...VD_UI_Done.png
Thanks again, best wishes, etc..
Re: Application Window Size Problem.
Quote:
I take it you saw my last post edit before you replied?
No, I must have been reading your post and writing my reply when you posted the edit, but no harm done.
Quote:
Everything in the GUI is now done and I thought you might like to see the excellent results
Thanks. Yes that does look good - well done.
Quote:
Many thanks for all your help...
My pleasure.