aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing: Unable to bring down the size of JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing: Unable to bring down the size of JPanel" Watch "Swing: Unable to bring down the size of JPanel" New topic
Author

Swing: Unable to bring down the size of JPanel

Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
1. I am new to Swing and I am trying to put a JButton in to a JPanel. The JPanel is configured to work with BorderLayout. I want the panel to exactly fit the size of the button but I could not do it. The panel is large and there is ugly space sorrounding it.

2. I am trying to set the rows and columns of the text area. After I set them using setRows and setColumns of JTextArea methods, it shows in the console that they are set when I see them using getRows and getColumns but I could not actually see the text area widening up. It also affects the sizes of the labels...

Here is the Java code for the Interface.java file


Raghavan
SCJP 6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name; "handles" and joke names don't work at the Ranch. You can change your display name here. Thanks!

Now, as to your question: the preferred sizes of all components managed by a GridLayout are totally ignored. The size of the parent is divided into a grid of equal sized boxes, and the components are sized to fit the boxes. This is documented on GridLayout's Javadoc page.

We've got a forum devoted to Swing and AWT issues here. I will move this there for you.
[ December 20, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ernest Friedman-Hill:
...the preferred sizes of all components managed by a GridLayout are totally ignored. The size of the parent is divided into a grid of equal sized boxes, and the components are sized to fit the boxes. This is documented on GridLayout's Javadoc page.


I wouldn't say that GridLayout total ignores the preferred sizes of the components. Consider this demo: all the components end up with size 200x200:
With frame.pack, containers *may* (depending on how they're nested etc..) ask their layout managers to compute a preferred size, and in turn that *may* cause the layout managers to ask the container's components about their preferred sizes. In the above example, the components say 100x200 or 200x100, and the GridLayout combines that to come up with a 400x400 container, divided into four 200x200 cells.

As for the original posting, the key thing to realize is that in a GridLayout, all rows will have the same height and all columns will have the same width, and that is why the button has so much space around it. You're going to have to use a different layout, like GridBagLayout, or nest layouts further, to get what you want.


There is no emoticon for what I am feeling!
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
1. I have now tried GridBagLayout,,,now I am keep components in places where I wanted them but the first panel and the third panel are not of the same size as its supposed to be. The parameters set for both of these panels are the same but somehow they differ in size or I might have done a non-evident mistake....help me with this...

2. I have another doubt...when I import java.awt.*, why do I have to import java.awt.event.* for using ActionListener...why don't the former import work?


Here is the new code...

[ December 20, 2005: Message edited by: Raghavan Chockalingam ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
1. Here's a start. Note that with GridBagLayout, you may not need to nest panels within panel to the same extent as you would with simpler layout managers.
2.That's the way imports are defined to work. As far as importing is concerned there's no notion of a "sub-package", so importing java.awt.* doesn't import any of the packages like java.awt.event. The spelling of the names is just coincidental.
 
 
subject: Swing: Unable to bring down the size of JPanel