[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JAVA GridLayout:  Width and Height are Customizable?

Posted on 2009-03-30
17
Medium Priority
?
2,221 Views
Last Modified: 2012-05-06
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!
0
Comment
Question by:duta
  • 6
  • 6
  • 2
  • +3
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24019743
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 24019960
You may want to go with GridbagLayout!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24020093
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 24023847
> 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
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24024603
A flow layout and then setPreferredSize() on each object you add can mimic a grid layout.
0
 

Author Comment

by:duta
ID: 24025026
Thank you so much all of you.
I will take a little time to try all of your kind tips.

Thanks again!
0
 
LVL 92

Expert Comment

by:objects
ID: 24025041
if you can let me know the desired layout you are after I can help with the required layout manager(s)

0
 

Author Comment

by:duta
ID: 24025270
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
 
LVL 92

Expert Comment

by:objects
ID: 24025294
you can set the preferred size of the nested panels you add to control there size


0
 

Author Comment

by:duta
ID: 24025332
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
 
LVL 92

Expert Comment

by:objects
ID: 24025350
the center will resize its panel to use any available space so is not a good choice if you want a fixed size

0
 

Author Comment

by:duta
ID: 24025357
How about West or East?

Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 24025383
panel.setPreferredSize(new Dimension(width, height));

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

0
 

Author Comment

by:duta
ID: 24025490
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24025519
you missed the set, should be setPreferredSize

0
 

Author Closing Comment

by:duta
ID: 31564353
You are awesome!

Thanks a lot!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question