• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hard coding component size in GUI?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

When designing and implementing GUI, how do you set the component sizes? At first I though it would be ok to go with not setting the size at all, but this "large enough to contain" logic resulted in resized components when I tried to change the labels of the buttons in runtime. So I think maybe I should set them to a certain value.

But how do you guys do it? Do you hard code the component size, and is it ok?

Thanks.
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Hardcode is sometohng relay bad, and together with a UI can bring a lot of troubles. Use layout managers to avoid this.
The only stuff what you can "ar'code" are some insets(like button margins, etc) and some default size some text fields(by example - when the UI starts the text field must be able to show at least 20 characters).
Why do you change the labels runtime ?


Regards M.
 
Can Zheng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mihai,

Yup your suggestion is exactly what I meant. After using a layout manager I still need to hard code the components prefered/minimum/maximum size in many cases, and I'm quite uncomfortable with that. Is there a way we can get the UI code cleaner?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can
I am not shoew what you mean with :


Is there a way we can get the UI code cleaner



But don't set the the components prefered/minimum/maximum size "by hand" this is the layout manager task. This is a bad technique.

Regards M.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Can Zheng:
After using a layout manager I still need to hard code the components prefered/minimum/maximum size in many cases, and I'm quite uncomfortable with that. Is there a way we can get the UI code cleaner?



I think in many cases you cannot avoid setting some preferred sizes.
Take a look at the following link,it may be helpful for vanishing your doubts: Modify preferred size
[ August 09, 2006: Message edited by: Liviu Carausu ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Liviu

First thank for the link, it ruleZ


I think in many cases you cannot avoid setting some preferred sizes


I am not so shore about, if you decide to set the min,pref or max size you must be shore that you have the universal values for that sizes(valid under all l&f) otherwise your UI will look pretty strange after you change the l&f or the font.

the link (www.swingwiki.org...) just give a tip how to change the min size
Can you provide some examples ?

Also consider the :
http://www.swingwiki.org/workaround:fixed_position_layout

Regards M

[ August 08, 2006: Message edited by: Mihai Radulescu ]
[ August 08, 2006: Message edited by: Mihai Radulescu ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that whether using hardcoded component sizes or not depends on your GUI design.

If you are using resizable windows, for instance, you may prefer not to hardcode component sizes and let your layout do all the work.

On the other hand, if your windows are of a fixed-size as well as the rest of their components, you may be fine with hardcoded sizes as long as they fit in most screens. Therefore you may design your screens for a 800x600 resolution and count on them to look fine in bigger resolutions. That's what I would do.

That being said, I would rather use use components whose size is determined dynamically bu the layout manager. For this, I recommend you to create your GUI designs on paper, first, and carefully choose your layout manager.

It is very probable that you will need more than one to create the design that you draw on paper.

I hope that helps!
 
Liviu Carausu
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

From my experience, the Layout managers are also taking into account the component preferred or minimum/maximum size.

The following quotes are taken from the JDK5.0 Api documentation for GridBagLayout ( for example )

Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. The constraints object specifies where a component's display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout also considers each component's minimum and preferred sizes in order to determine a component's size.



Every layout respects more or less the component preferred/maximum/minimum size, another examples :

FlowLayout:

A flow layout lets each component assume its natural (preferred) size.



BorderLayout

The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.



So, as long as it concerns me, setting preferred sizes for some components in some situations, depending on your layout is not "dirty", it's just the way it goes.

I'm not talking now about the NullLayout, this is something different, the positioning of your components is based only on the getBounds() method of the corresponding component.

Greetings,

Liviu
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Liviu

I still don't understand why you need to set the preferred size for UI components ? Can you provide a real example ?

And now back to the pref size.
Each UI component must take care of its own pref size, depending on its content.By example a button take care of its icon and its text. As long as the text and icon are reasonable the JButton pref size will allow then to be showed.
The advantage is that you can change this content(internationalization or improvements) without to care about the pref size. I don't want to talk here about laf, font or other stuff which can influence the way how the component calculate its perf size.
But if you constrain it to have a specified pref size on the next changes you must take care again about this.
Let's say that you use buttons with 16x16 icons and you constrain the buttons on 16x16. If some one decide to change the icons size and make them bigger will be surprised to see that the buttons trim the images.
You can apply the same logic on button with text, you can find some really nice values which are looking nice with a English version but when you'll try to internationalize your UI ....

If you decide to use in this was this can cost you some points.

Regards M
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic