Solved

JAVA GridLayout:  Width and Height are Customizable?

Posted on 2009-03-30
17
2,183 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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24020843
0
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now