JAVA GridLayout: Width and Height are Customizable?

Dear Experts:

I would like to learn how to customize the width and height ( in pixels, not number of cols and rows) of a GridLayout.

Thanks a lot!
dutaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
you missed the set, should be setPreferredSize

0
 
CEHJCommented:
You can't really - it will simply divide up the full space of its parent Container equally. You might try to add Insets to the parent
0
 
ksivananthCommented:
You may want to go with GridbagLayout!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
CEHJCommented:
Something like the following should be OK
                        setContentPane(new JPanel() {
 
                            public Insets getInsets() {
                                return new Insets(40, 40, 40, 40);
                            }
 
                        });

Open in new window

0
 
objectsCommented:
> I would like to learn how to customize the width and height ( in pixels, not number of cols and rows) of a GridLayout.

Just set the width and height available in the container, eg. if you want a 3x2 grid of 20px x 10px cells then make the space available to it to be 60px x 20px.
0
 
Thomas4019Commented:
A flow layout and then setPreferredSize() on each object you add can mimic a grid layout.
0
 
dutaAuthor Commented:
Thank you so much all of you.
I will take a little time to try all of your kind tips.

Thanks again!
0
 
objectsCommented:
if you can let me know the desired layout you are after I can help with the required layout manager(s)

0
 
dutaAuthor Commented:
Savant, I created a simple GUI using BorderLayout and then nested GridLayout in each of the BorderLayout's five zones (N, S, W, C, East).

I wanted to have equal width in the GridLayout nested in West, Center, East zones for cosmetic reason.

Thanks a lot!

0
 
objectsCommented:
you can set the preferred size of the nested panels you add to control there size


0
 
dutaAuthor Commented:
Savant, can you kindly show me a sample code to use setPreferredSize() ?

In  Center zone of the BorderLayout, I nested a GridLayout (4, 1).

The GridLayout contains:

AgeLbl
AgeTxt

SalaryLbl
SalaryTxt

Thanks!

0
 
objectsCommented:
the center will resize its panel to use any available space so is not a good choice if you want a fixed size

0
 
dutaAuthor Commented:
How about West or East?

Thanks!
0
 
objectsCommented:
panel.setPreferredSize(new Dimension(width, height));

*but* only the width will have an impact, the height will get stretched to the available space

0
 
dutaAuthor Commented:
Savant, I tried your tip.

But I got an error message:

Error: C:\Documents and Settings\admin\Desktop\ProjectOne.java:111: cannot find symbol
symbol  : method PreferredSize(java.awt.Dimension)
location: class javax.swing.JPanel

Do I need ti import  a certain class to use the function?

Thanks!
0
 
dutaAuthor Commented:
You are awesome!

Thanks a lot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.