• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2242
  • Last Modified:

JAVA GridLayout: Width and Height are Customizable?

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
duta
Asked:
duta
  • 6
  • 6
  • 2
  • +3
1 Solution
 
CEHJCommented:
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
 
ksivananthCommented:
You may want to go with GridbagLayout!
0
 
CEHJCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
objectsCommented:
> 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
 
Thomas4019Commented:
A flow layout and then setPreferredSize() on each object you add can mimic a grid layout.
0
 
dutaAuthor Commented:
Thank you so much all of you.
I will take a little time to try all of your kind tips.

Thanks again!
0
 
objectsCommented:
if you can let me know the desired layout you are after I can help with the required layout manager(s)

0
 
dutaAuthor Commented:
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
 
objectsCommented:
you can set the preferred size of the nested panels you add to control there size


0
 
dutaAuthor Commented:
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
 
objectsCommented:
the center will resize its panel to use any available space so is not a good choice if you want a fixed size

0
 
dutaAuthor Commented:
How about West or East?

Thanks!
0
 
objectsCommented:
panel.setPreferredSize(new Dimension(width, height));

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

0
 
dutaAuthor Commented:
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
 
objectsCommented:
you missed the set, should be setPreferredSize

0
 
dutaAuthor Commented:
You are awesome!

Thanks a lot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 6
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now