This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Frame size in executable JAR enormous compared to running in Eclipse.  RSS feed

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried a ton of different ways to get the screen size in the executable jar to be the same as when I run it in eclipse, but the problem is, it just won't work.  When I start the application, the home screen is enormous and not all the buttons fit on the screen, regardless of the set preferred size method, as well as setting the dimensions equal to the screen size.  When I click the buttons that open a new screen through event handler, those are about 25% larger as well.  I've packed it before and after declaring the size to see if it makes a difference, and it doesn't.  It just won't fix. 





I used the group layout, because that seems to be the only one that will allow me to put the buttons where I want them.  I was using the null layout before, but I thought that was causing me the problem, but it wasn't.  It just won't adjust to an appropriate size.

Any help here would be appreciated.
 
Rancher
Posts: 2940
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

regardless of the set preferred size method,



you should not use the setPreferredSize() method. It is the job of the layout manager to determine the preferred size when you use the pack()

I used the group layout, because that seems to be the only one that will allow me to put the buttons where I want them. 



You should not be putting buttons where you want them. That sounds like using a null layout.

I see all kinds of numbers in the constraints of the GroupLayout. Its seems to me that these numbers are hardcoded to give the for a fixed size on a screen of a certain resolution.

I also see the buttons displaying larger than the should be. It appears to me the button size is hardcoded in the GroupLayout.

The point of using a layout manager (or nested panels using different layout managers) is to provide hints to the layout manager and let it dynamically position and size the components.

So, for example, if you want the buttons bigger you use code like:

button.setMargin( new Insets(...) );



Now the button can calculate its own preferred size using that extra information and the layout manager can use this information.

I have never used a GroupLayout because it is generally only used by IDE's. As humans we can typically write more logical layout code.

In your case I might suggest you could use a GridBagLayout. Your buttons basically appear in a grid.

Read the section from the Swing tutorial on How to Use GridBagLayout. The section on the constraints will show you how to add extra space between components.

In the future when you ask a question post a proper SSCCE. We should be able to compile/test your posted code.

You question is about component layout, so all the code related to the processing of your app (ie ActionListeners) is completely irrelevant to the stated question. And because we don't have access to all your processing classes we can't even compile the code.

So the code you post should only be directly related to the layout of the components.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you're saying there is no way to get the screen to look the way I want it to look? 

Many of my screens are using JLists and moving items from one to another, so it MUST look a certain way. 
 
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:So, you're saying there is no way to get the screen to look the way I want it to look? . . . 

No, that isn't what Rob is saying at all. Search for Cay Horstmann's GBC class (try starting here) because it makes GridBag easier to use.

The first stage to using GridBag is to draw what you want your GUI to look like.
 
Rob Camick
Rancher
Posts: 2940
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never said that.

I suggested you don't want to use hardcoding of values. The GroupLayout appears to be using hardcoded values.

I don't know what your desired layout is (the code doesn't compile).

You can achieve any layout you want with layout managers. The trick is that you are not forced to use a single layout manger. You can logically group components on a panels and each panel can have a different layout manager. When you put the panels together you have the complete layout.

So the layout should be defined by rules. Every panel needs to know how to handle resize of the frame. You can't just design the form to look good on your current screen. Screen sizes and screen resolutions change from user to user.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:So, you're saying there is no way to get the screen to look the way I want it to look? . . . 

No, that isn't what Rob is saying at all. Search for Cay Horstmann's GBC class (try starting here) because it makes GridBag easier to use.

The first stage to using GridBag is to draw what you want your GUI to look like.



The links on that page that they are referencing don't work.  They all go to a not found page.



I'm not really understanding this though.  Why can't you just make the builder draw the gridbag how you want it to be?    This seems like an awfully tedious way just to do something you should be able to just do with an editor.  So, do I have to re-start all the JFrames I made from scratch then?  The problem was, I wanted the screen to be full from the start of the application.  

 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just take out the action listeners, it should compile.  It's only because I have about 10 classes in that program, so it would take hours to copy and paste them all.  I'll try to make a template of what I'm doing later. 

I just basically was wondering how to use a layout, and position the components in a precise way at the same time as you can with a null layout. 
 
Rob Camick
Rancher
Posts: 2940
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just basically was wondering how to use a layout, and position the components in a precise way at the same time as you can with a null layout.



Again, you should NOT be doing this. You do not want to position components in a precise way. Things change.

1. Screen sizes change.
2.  Fonts can be different of each LAF.
3. The user can resize the frame

You want the layout to adapt. I already gave you some suggestions:

1. you can use setMargin(...) to give extra space to a button
2. you can use Borders to give extra space to panels and (some components that don't already use Borders)
3. you can use the layout manager to specify gaps between components.

Without a specific requirement we can't give specific suggestions.

It's only because I have about 10 classes in that program,



And I already told you we are not interested in those classes because they are not related to the problem.

If you just take out the action listeners, it should compile.



You are the one asking the question, so you are the one who should do this so we can be sure that we are testing the exact code you are testing.

 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . The links on that page that they are referencing don't work.  . . .

Link 1 You really need to watch that tutorial, which is the classic resource about GridBag. You don't actually need those links; the other two have vanished, but you can find them via Wayback Machine.

I'm not really understanding this though. . . . an awfully tedious way just to do something you should be able to just do with an editor.

You use an editor to write the grid bag constraints. The reason it appears tedious is that you have used GUI builders so much you have not learnt how to write your own layout code.

So, do I have to re-start all the JFrames . . .

No, you populate the frames with a proper layout and all the components will be visible from the word go.
 
Rob Camick
Rancher
Posts: 2940
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  I wanted the screen to be full from the start of the application.   



So the basic logic should be:


The frame will start maximized. If you ever click the mimiize button it will then be sized based on the size determined by the pack() method.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code here is what I want the frame to look like.  There is no layout though, so I have to figure out how to get it to be shaped exactly like this with a layout that isn't going to make it go off the screen when it is run by another machine. 

 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:. . . . If you ever click the mimiize button it will then be sized . . . .

Minimise or unmaximise?
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:This code here is what I want the frame to look like.  . . .

You don't use code to show what the frame should look like. You use a picture, which you might have on paper, on a drawing program, or in your head.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:This code here is what I want the frame to look like.  . . .

You don't use code to show what the frame should look like. You use a picture, which you might have on paper, on a drawing program, or in your head.




If you copy and paste the code into a compiler, it will compile to what I want it to look like, but I'll show a picture too
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a picture of what I want the home screen to look like.  The other screen is one of the screens with JLists where I want to move campers to and from cabins.  The last one is where you register a new camper.
cabin-assigner-layout.png
[Thumbnail for cabin-assigner-layout.png]
AddedCampers.png
[Thumbnail for AddedCampers.png]
camper-form.png
[Thumbnail for camper-form.png]
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did find one example where a guy typed coordinates he wanted everything at. 

Would it maybe work if I drew out a graph in (x,y) format, and then labeled the coordinates I wanted each button to be, and then type it out with code?
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . then labeled the coordinates I wanted each button to be, and then type it out with code?

No. Go back and see what Rob has written; that would only work if you keep the size of the entire display constant.
You draw the grid as you said and rather than coordinates label things with percentage of width and percentage of height.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:. . . then labeled the coordinates I wanted each button to be, and then type it out with code?

No. Go back and see what Rob has written; that would only work if you keep the size of the entire display constant.
You draw the grid as you said and rather than coordinates label things with percentage of width and percentage of height.



Rob said that you should not decide where the buttons go, but allow the layout manager to do that.  That's what I'm confused by.  I need the screen to look as how I placed it, or the entire application makes no sense to even develop in the first place.  Reading that makes as it seem as if you just create the buttons and hope that the layout manager puts them where you want them to go.   I guess I'm just not understanding what anyone is saying.
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Rob meant is that you don't tell the button to go at coordinatees (200, 250). What you do is tell the layout manager you want that button halfway down and slightly left of centre. In the case of gridbag, you do that with the numbers you pass to the constraints object. I presume you have found out about the Horstmann GBC class; it makes gridbag much easier to use.

There are eleven values you can put in the constraints; ten numeric and one an instance of Insets. I think you should probably give values to all eleven, even if some of those values are zero. Don't use null for insets, but you can use new Insets(0, 0, 0, 0);. Actually, you can possibly use the default values for insets and the ipad fields throughout.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm just afraid I have no idea what any of this even means as of now.  I'll have to redesign my entire templates most likely, because when I change the layout on them, many of my methods and listeners no longer work properly and are giving Null Pointer Exceptions because everything is rearranged.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I get the x and y coordinates part, and I get setting the length for a longer button, but I don't understand the ipady part on the link example.  Why do you set the length higher to make it longer, but to make it taller, you have to use ipady, and where did the number 40 come from?
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . many of my methods and listeners no longer work properly and are giving Null Pointer Exceptions because everything is rearranged.

No, the methods fail to work because they are in the wrong place. A display class is for displaying things, not for doing business logic; that should be in different classes. Rearranging things won't produce Exceptions like that; i tmeans you have deleted initialisation code or something like that.
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . many of my methods and listeners no longer work properly and are giving Null Pointer Exceptions because everything is rearranged.

No, the methods are failing to work because they are in the wrong place. A display class is for displaying things, not for doing business logic; that should be in different classes. Rearranging things won't produce Exceptions like that; it means you have deleted initialisation code or something like that.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:. . . then labeled the coordinates I wanted each button to be, and then type it out with code?

No. Go back and see what Rob has written; that would only work if you keep the size of the entire display constant.
You draw the grid as you said and rather than coordinates label things with percentage of width and percentage of height.



The link Rob gave said nothing about making buttons a percentage of width and height.  Every example in all links given were putting them at positions of X and Y. 

I'm beginning to wonder what Window Builders are even used for, because the way everyone is describing how to do this, they seem pretty useless.
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . The link Rob gave said nothing about making buttons a percentage of width and height.  . . . .

You work out the percentages from what you want the display to look like. If all your widths come to multiples of 12½%, for example, you will want the leftmost x to be 0 and the rightmost to be 7 so you get eight different positions. Similarly if all your heights are multiple of 10%, you will want the ys to range 0...9. Top left (xy) is always (0, 0) and in my example bottom right will be (7, 9).
You adjust the sizes of the components with their gridwidth and gridheight constraints.

Also look at Horstmann's GBC class or, better still, find a copy of his book which explains GBC in more detail.
 
Rob Camick
Rancher
Posts: 2940
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is more than one way to do a layout. You need to decide logically what happens when the user resizes the frame. Do the components shrink or do the space between the components shrink.

Different layout managers have different functionality with regard to resizing so you need to understand how each layout managers work.

A FlowLayout respects the size of components.

A GridLayout resets the size of components to match the size of the largest component in the panel.

A GridBagLayout provides constraints that allow you to configure different resizing functionality for each component.

A BoxLayout will resize all components but it respects the minimum/maximum sizes.

Using the above guidelines I came up with an example layout that uses the BoxLayout and the GridLayout.

The main layout is the vertical BoxLayout, so components are position top to bottom as they are added.

Then because you want the buttons to be the same size, I grouped each pair of buttons into a separate panel using a GridLayout, so each pair of buttons is the same size.

I used Insets on the buttons to make them larger.

I used a Border on the main panel to position components away from the edges of the panel.

I used "struts" to give fixed spacing between components and "glue" to give variable spacing between components.

Here is the code. Resize the frame to see how different components change location.



I could have used a GridBagLayout for the entire form, but I wanted to show how to nest panels with different layout managers.

So you should try using the GridBagLayout. Basically I see that you have 6 rows of components and 3 columns. So you will need to play with the grid bag constraints as you add component to make sure they are position and sized properly and have the proper gap between components.

Compare the two approaches. Every form will be different so you will need to understand each layout manager and when to use them.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:There is more than one way to do a layout. You need to decide logically what happens when the user resizes the frame. Do the components shrink or do the space between the components shrink.

Different layout managers have different functionality with regard to resizing so you need to understand how each layout managers work.

A FlowLayout respects the size of components.

A GridLayout resets the size of components to match the size of the largest component in the panel.

A GridBagLayout provides constraints that allow you to configure different resizing functionality for each component.

A BoxLayout will resize all components but it respects the minimum/maximum sizes.

Using the above guidelines I came up with an example layout that uses the BoxLayout and the GridLayout.

The main layout is the vertical BoxLayout, so components are position top to bottom as they are added.

Then because you want the buttons to be the same size, I grouped each pair of buttons into a separate panel using a GridLayout, so each pair of buttons is the same size.

I used Insets on the buttons to make them larger.

I used a Border on the main panel to position components away from the edges of the panel.

I used "struts" to give fixed spacing between components and "glue" to give variable spacing between components.

Here is the code. Resize the frame to see how different components change location.



I could have used a GridBagLayout for the entire form, but I wanted to show how to nest panels with different layout managers.

So you should try using the GridBagLayout. Basically I see that you have 6 rows of components and 3 columns. So you will need to play with the grid bag constraints as you add component to make sure they are position and sized properly and have the proper gap between components.

Compare the two approaches. Every form will be different so you will need to understand each layout manager and when to use them.





Wow, that saves me from having to create the main screen at least.  

I'm studying the box layout as well, but still having a difficult time understanding the vertical strut part. 


Okay, so given my 2nd picture with the 4 JLists and combo box, can you give any kind of hint of which layout works best with that? 

Also, for some reason, when using your code, it all works fine, but 4 of the buttons don't show when looking at it through the window builder in Eclipse. 
 
Rob Camick
Rancher
Posts: 2940
19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quote the entire answer. It makes scrolling through this question to difficult.

but still having a difficult time understanding the vertical strut part.



Read the tutorial or the API. I'm not sure what you don't understand. All you need to do is either:

1. comment the line to see how the layout changers or
2. change the number in the line to see how the layout changes.

This is the benefit of starting with working code. You can change one property at a time to see how it affects the layout.

can you give any kind of hint of which layout works best with that? 



Again, you need to define the rules of resizing. That will help suggest an approach to use.

I already gave you a second suggestion for the first layout to practice and use the GridBagLayout.

Same applies here. You basically have a grid with 3 rows and 3 columns so you could

1. Use the same approach as I gave you. That is use a vertical BoxLayout as the main layout and then add rows of components.

2. Use a 3 x 3 GridBagLayout.

Again, you learn by trying.

This is my last answer to this question. 26 replies to too many. A thread should have a single question with a couple of replies, which means the initial question need all the information required to solve the problem. So try a layout and if you have a problem ask another question post a proper SSCCE showing what you have tried and giving us all the all the requirements of the layout.

Again, the requirement is not to fill the entire screen because as a user you should not tell me how to use the space on my desktop. So you may start the application maximized, but if I decide to change the size of the frame, then the components should resize/reposition themselves logically.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF you could please just answer 1 more question...

Why is it not letting me use the setBorder method you used in a new frame?  It says I need to create a method for it, but on the other class, which I have all the same imports, it works.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:IF you could please just answer 1 more question...

Why is it not letting me use the setBorder method you used in a new frame?  It says I need to create a method for it, but on the other class, which I have all the same imports, it works.



Ignore that question.  I just needed to extend JPanel and not JFrame
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:


I could have used a GridBagLayout for the entire form, but I wanted to show how to nest panels with different layout managers.

So you should try using the GridBagLayout. Basically I see that you have 6 rows of components and 3 columns. So you will need to play with the grid bag constraints as you add component to make sure they are position and sized properly and have the proper gap between components.

Compare the two approaches. Every form will be different so you will need to understand each layout manager and when to use them.





I'd like to point out that in your code, you commented saying you were going to use a grid bag layout for the 6 buttons in the center, but you actually used a grid layout. 
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!