Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weightx and Weighty in Java GridBagLayout

Tags:

I have some trouble understanding these two properties. How should I give weight to components? How are these numbers calculated? I have tried to read several articles on the web but I do not understand it.

Thank you.

like image 820
LuckyLuke Avatar asked Apr 26 '11 11:04

LuckyLuke


People also ask

What is the difference between GridLayout and GridBagLayout?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

What is GridBagLayout in Java?

GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height.

Why do we need layout management what is GridBagLayout?

GridBagLayout FieldsIt is used to hold the overrides to the column minimum width. It is used to maintains the association between a component and its gridbag constraints. It is used to hold a gridbag constraints instance containing the default values. It is used to hold the layout information for the gridbag.

What is Gridwidth?

gridwidth is like span in html's table , it describes the number of columns that the component can span over/occupy. – MadProgrammer.


2 Answers

If the space within a Panel is greater than the preferredDimension of the components contained within, the weightx and weighty is used to distribute the extra space to the individual components.

use values from 0.0 to 1.0 (think of this a percentage).

  • weightx is horizontal spacing

  • weighty is vertical spacing

The most common scenario in desktops is that the side panes stay a fixed size (weightx/weighty = 0.0) and the center pane takes up the remaining space (weightx/weighty = 1.0). however, using variations, you can have complete control of how your application resizes the individual components as the Frame size changes.

like image 182
Codemwnci Avatar answered Sep 17 '22 19:09

Codemwnci


weightx and weighty are used to determine how to distribute space among columns and among rows.

This values are important for specifying resizing behavior.If you do not specify any of weightx or weighty, all the components will clump together in the center of their container. See the doc of GridBagLayout for more information.

For each column, the weight is related to the highest weightx specified for a component within that column. Similarly, each row's weight is related to the highest weighty specified for a component within that row.

like image 22
Harry Joy Avatar answered Sep 21 '22 19:09

Harry Joy