Solved

JAVA GridLayout:  Width and Height are Customizable?

Posted on 2009-03-30
17
2,178 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now