Solved

JAVA GridLayout:  Width and Height are Customizable?

Posted on 2009-03-30
17
2,189 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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