Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

GridBagLayout - strange resize behaviour

Thread view: 
Nihil Baxter - 19 Dec 2003 09:29 GMT
GridBagLayout - strange resize behaviour
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

Hi there,

I have done a Swing application using GridBagLayout - looks good,
works fine - but, there is this strange resize behavior.

I have JLabels followed by JTextFields togehter on a JPanel. I reduced
it to 3 rows on the grid.

The Labels are aligned left and don't change their size by resizing,
only the TextFields do.

The problem goes like this:

when I resize the frame horizontally it happens that the space between
the Labels and the Panel, which is 0 pxl, jumps to 1 maybe 2 pxl.

It does not happen every resize, just keep resizing and it will
appear.

thanks for your help

nihil baxter
°°°°°°°°°°°°°°°°°°°°°

import javax.swing.*;
import java.awt.*;

public class GBLTest {

   public void init(JPanel panel) {
       Dimension defaultDim = new Dimension(120,20);
       panel.setLayout(new BorderLayout());
       JPanel grid = new JPanel(new GridBagLayout());
       GridBagConstraints cons = new GridBagConstraints();

       JLabel label;
       JTextField textField;

       cons.fill = GridBagConstraints.HORIZONTAL;
       cons.anchor = GridBagConstraints.NORTH;
       //Row 1
       label = new JLabel("Label 1");
       label.setPreferredSize(defaultDim);
       label.setMinimumSize(defaultDim);

       cons.gridy = 0;
       cons.gridx = 0;
       cons.weightx = 0;
       grid.add(label,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       cons.weightx = 0.5;
       grid.add(textField,cons);

       //Row 2
       label = new JLabel("Label 2");
       label.setPreferredSize(defaultDim);
       label.setMinimumSize(defaultDim);

       cons.gridy = 1;
       cons.gridx = 0;
       cons.weightx = 0;
       grid.add(label,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       cons.weightx = 0.5;
       grid.add(textField,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       grid.add(textField,cons);

       //Row 3
       label = new JLabel("Label 3");
       label.setPreferredSize(defaultDim);
       label.setMinimumSize(defaultDim);

       cons.gridy = 2;
       cons.gridx = 0;
       cons.weightx = 0;
       grid.add(label,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       cons.weightx = 0.5;
       grid.add(textField,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       grid.add(textField,cons);
       //--
       textField = new JTextField();
       textField.setPreferredSize(defaultDim);
       textField.setMinimumSize(defaultDim);

       cons.gridx = GridBagConstraints.RELATIVE;
       grid.add(textField,cons);

       panel.add(grid,BorderLayout.NORTH);
   }

   public static void main(String[] args) {
       JFrame frame = new JFrame("GridBagLayoutTest");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       GBLTest gblTest = new GBLTest();
       gblTest.init((JPanel)frame.getContentPane());
       frame.pack();
       frame.show();
   }
}
Andrew Thompson - 19 Dec 2003 17:26 GMT
> GridBagLayout - strange resize behaviour
> ????????????????????????????????????????
..I usually do not jmp into qns about gbl, since I
am no expert on them, but I will on this one..

> I have done a Swing application using GridBagLayout - looks good,
> works fine - but, there is this strange resize behavior.
>
> I have JLabels followed by JTextFields togehter on a JPanel. I reduced
> it to 3 rows on the grid.
...
> when I resize the frame horizontally it happens that the space between
> the Labels and the Panel, which is 0 pxl, jumps to 1 maybe 2 pxl.

I compiled and ran your code so's I could get
an easy (ok lazy) picture of what you mean..

Then it struck me.
You have _3_ rows and the layout gives
0,1 or 2 px space.

I bet if you had _4_ rows that space
would become 0, 1, 2 or _3_ px.

The GBL does not add a pixel to any of the
3 in the row until it can add a single pixel to
_each_ of the three, leading to either 0, 1,
or 2 spare.

Adding a border to the various elements may
help make clear if that is the case (always a
good strategy for chasing down fine layout
problems)

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.