Solved

JAVA GridLayout:  Width and Height are Customizable?

Posted on 2009-03-30
17
2,197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

691 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