CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Sep 2009
    Posts
    62

    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:



    Here is a screen cap of the size when it's been resized (manually) to the correct size:



    Many thanks.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Application Window Size Problem.

    What methods are you calling initially that would affect the size? Do you call pack()?
    Norm

  3. #3
    Join Date
    Sep 2009
    Posts
    62

    Re: Application Window Size Problem.

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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?
    Norm

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Sep 2009
    Posts
    62

    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Application Window Size Problem.

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Sep 2009
    Posts
    62

    Re: Application Window Size Problem.

    Quote Originally Posted by keang View Post
    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.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Sep 2009
    Posts
    62

    Re: Application Window Size Problem.

    Quote Originally Posted by keang View Post
    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.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Sep 2009
    Posts
    62

    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.

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Application Window Size Problem.

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  14. #14
    Join Date
    Sep 2009
    Posts
    62

    Re: Application Window Size Problem.

    Quote Originally Posted by keang View Post
    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.

  15. #15
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured