Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1478 > unrolled thread

GridBagLayout not behavin

Started by"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
First post2011-04-27 15:33 +0000
Last post2011-04-27 15:33 +0000
Articles 11 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  GridBagLayout not behavin "Qu0ll" <qu0ll@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
    Re: GridBagLayout not beh "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
      Re: GridBagLayout not beh "Qu0ll" <qu0ll@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
        Re: GridBagLayout not beh "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
          Re: GridBagLayout not beh "Qu0ll" <qu0ll@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
        Re: GridBagLayout not beh "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
          Re: GridBagLayout not beh "Qu0ll" <qu0ll@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
            Re: GridBagLayout not beh "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
    Re: GridBagLayout not beh "Axel Hallez" <axel.hallez@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
      Re: GridBagLayout not beh "Qu0ll" <qu0ll@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
        Re: GridBagLayout not beh "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000

#1478 — GridBagLayout not behavin

From"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectGridBagLayout not behavin
Message-ID<4625ea23$0$25471$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
  To: comp.lang.java.gui
I have a problem with GridBagLayout that I would appreciate some help with 
in solving.

In the following simple example I am trying to have a user label and text 
field plus a password label and field evenly spaced across the panel with a 
database field below it and a button all the way over to the right taking up 
very little space.  To do this I have set up a 2 x 16 grid but it's not 
behaving as I believe I am specifying it.  The problem is that the button is 
taking up the same amount of space as the password field even though the 
password field has a grid width of 4 and the button only 1.  The result is 
that the top line is not evenly spaced and too much room is allocated for 
the button.

Why is this?  What's wrong with my GridBagLayout definition or usage?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class GBTest extends JFrame {

 private JPanel panel = new JPanel();
 private JLabel userLabel = new JLabel("User:");
 private JLabel passwordLabel = new JLabel("Password:");
 private JLabel dbLabel = new JLabel("Database:");
 private JTextField userTextField = new JTextField();
 private JTextField dbTextField = new JTextField();
 private JPasswordField passwordField = new JPasswordField();
 private JButton dbButton = new JButton("...");

 public GBTest() {
  setLayout(new BorderLayout());
  panel.setLayout(new GridBagLayout());

  userLabel.setText("User:");
  GridBagConstraints gbc = new GridBagConstraints();
  gbc.gridx = 0;
  gbc.gridy = 0;
  gbc.gridwidth = 4;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(userLabel, gbc);

  gbc.gridx = 4;
  gbc.gridy = 0;
  gbc.gridwidth = 4;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 0.5;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 4);
  gbc.ipadx = 40;
  panel.add(userTextField, gbc);

  gbc = new GridBagConstraints();
  gbc.gridx = 8;
  gbc.gridy = 0;
  gbc.gridwidth = 4;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(passwordLabel, gbc);

  gbc.gridx = 12;
  gbc.gridy = 0;
  gbc.gridwidth = 4;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 0.5;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 8);
  gbc.ipadx = 0;
  panel.add(passwordField, gbc);

  gbc.gridx = 0;
  gbc.gridy = 1;
  gbc.gridwidth = 4;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(dbLabel, gbc);

  gbc.gridx = 4;
  gbc.gridy = 1;
  gbc.gridwidth = 11;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 10;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(dbTextField, gbc);

  gbc.gridx = 15;
  gbc.gridy = 1;
  gbc.gridwidth = 1;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.weightx = 0.0;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 8);
  gbc.ipadx = 0;
  panel.add(dbButton, gbc);

  add(panel, BorderLayout.CENTER);

  pack();
  setPreferredSize(new Dimension(400, 250));
  setSize(new Dimension(400, 250));
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    new GBTest().setVisible(true);
   }
  });
 }
}

P.S. I am using Java 6 on Windows Vista if that's relevant.  It is not PLAF 
dependent.

-- 
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1484 — Re: GridBagLayout not beh

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<46262fa9$0$6950$fa0fcedb@news.zen.co.uk>
In reply to#1478
  To: comp.lang.java.gui
Qu0ll wrote:
> I have a problem with GridBagLayout that I would appreciate some help 
> with in solving.
> 
> In the following simple example I am trying to have a user label and 
> text field plus a password label and field evenly spaced across the 
> panel with a database field below it and a button all the way over to 
> the right taking up very little space.  To do this I have set up a 2 x 
> 16 grid but it's not behaving as I believe I am specifying it.  The 
> problem is that the button is taking up the same amount of space as the 
> password field even though the password field has a grid width of 4 and 
> the button only 1.  The result is that the top line is not evenly spaced 
> and too much room is allocated for the button.
> 
> Why is this?  What's wrong with my GridBagLayout definition or usage?
> 
> import java.awt.BorderLayout;
> import java.awt.Dimension;
> import java.awt.EventQueue;
> import java.awt.GridBagConstraints;
> import java.awt.GridBagLayout;
> import java.awt.Insets;
> 
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JPasswordField;
> import javax.swing.JTextField;
> 
> public class GBTest extends JFrame {
> 
> private JPanel panel = new JPanel();
> private JLabel userLabel = new JLabel("User:");
> private JLabel passwordLabel = new JLabel("Password:");
> private JLabel dbLabel = new JLabel("Database:");
> private JTextField userTextField = new JTextField();
> private JTextField dbTextField = new JTextField();
> private JPasswordField passwordField = new JPasswordField();
> private JButton dbButton = new JButton("...");
> 
> public GBTest() {
>  setLayout(new BorderLayout());
>  panel.setLayout(new GridBagLayout());
> 
>  userLabel.setText("User:");
>  GridBagConstraints gbc = new GridBagConstraints();
>  gbc.gridx = 0;
>  gbc.gridy = 0;
>  gbc.gridwidth = 4;
>  gbc.anchor = GridBagConstraints.EAST;
>  gbc.insets = new Insets(0, 4, 18, 0);
>  gbc.ipadx = 0;
>  panel.add(userLabel, gbc);
> 
>  gbc.gridx = 4;
>  gbc.gridy = 0;
>  gbc.gridwidth = 4;
>  gbc.fill = GridBagConstraints.HORIZONTAL;
>  gbc.weightx = 0.5;
>  gbc.weighty = 0.0;
>  gbc.insets = new Insets(0, 4, 18, 4);
>  gbc.ipadx = 40;
>  panel.add(userTextField, gbc);
> 
>  gbc = new GridBagConstraints();
>  gbc.gridx = 8;
>  gbc.gridy = 0;
>  gbc.gridwidth = 4;
>  gbc.anchor = GridBagConstraints.EAST;
>  gbc.insets = new Insets(0, 4, 18, 0);
>  gbc.ipadx = 0;
>  panel.add(passwordLabel, gbc);
> 
>  gbc.gridx = 12;
>  gbc.gridy = 0;
>  gbc.gridwidth = 4;
>  gbc.fill = GridBagConstraints.HORIZONTAL;
>  gbc.weightx = 0.5;
>  gbc.weighty = 0.0;
>  gbc.insets = new Insets(0, 4, 18, 8);
>  gbc.ipadx = 0;
>  panel.add(passwordField, gbc);
> 
>  gbc.gridx = 0;
>  gbc.gridy = 1;
>  gbc.gridwidth = 4;
>  gbc.anchor = GridBagConstraints.EAST;
>  gbc.insets = new Insets(0, 4, 18, 0);
>  gbc.ipadx = 0;
>  panel.add(dbLabel, gbc);
> 
>  gbc.gridx = 4;
>  gbc.gridy = 1;
>  gbc.gridwidth = 11;
>  gbc.fill = GridBagConstraints.HORIZONTAL;
>  gbc.weightx = 10;
>  gbc.weighty = 0.0;
>  gbc.insets = new Insets(0, 4, 18, 0);
>  gbc.ipadx = 0;
>  panel.add(dbTextField, gbc);
> 
>  gbc.gridx = 15;
>  gbc.gridy = 1;
>  gbc.gridwidth = 1;
>  gbc.fill = GridBagConstraints.HORIZONTAL;
>  gbc.anchor = GridBagConstraints.EAST;
>  gbc.weightx = 0.0;
>  gbc.weighty = 0.0;
>  gbc.insets = new Insets(0, 4, 18, 8);
>  gbc.ipadx = 0;
>  panel.add(dbButton, gbc);
> 
>  add(panel, BorderLayout.CENTER);
> 
>  pack();
>  setPreferredSize(new Dimension(400, 250));
>  setSize(new Dimension(400, 250));
>  setLocationRelativeTo(null);
>  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> }
> 
> public static void main(String[] args) {
>  EventQueue.invokeLater(new Runnable() {
>   public void run() {
>    new GBTest().setVisible(true);
>   }
>  });
> }
> }
> 
> P.S. I am using Java 6 on Windows Vista if that's relevant.  It is not 
> PLAF dependent.
> 

After
         panel.add(dbButton, gbc);
I added
         gbc.gridx = 1;
         gbc.gridy = 2;
         gbc.gridwidth = 1;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.anchor = GridBagConstraints.EAST;
         gbc.weightx = 0.0;
         gbc.weighty = 0.0;
         gbc.insets = new Insets(0, 4, 18, 8);
         gbc.ipadx = 0;
         for (int x=0; x < 16; x++) {
             gbc.gridx=x;
             panel.add(new JLabel(Integer.toString(x)), gbc);
         }
The results are interesting.
I think you could use fewer than 16 columns and this might make things 
more predictable. I suspect some columns are shrinking to zero width 
because no component STARTS in them.

Personally I'd use MigLayout and have about 1/10th the code.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1486 — Re: GridBagLayout not beh

From"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<462636b9$0$25454$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
In reply to#1484
  To: comp.lang.java.gui
"Ian Wilson" <scobloke2@infotop.co.uk> wrote in message 
news:46262fa9$0$6950$fa0fcedb@news.zen.co.uk...

[snip]

> After
>         panel.add(dbButton, gbc);
> I added
>         gbc.gridx = 1;
>         gbc.gridy = 2;
>         gbc.gridwidth = 1;
>         gbc.fill = GridBagConstraints.HORIZONTAL;
>         gbc.anchor = GridBagConstraints.EAST;
>         gbc.weightx = 0.0;
>         gbc.weighty = 0.0;
>         gbc.insets = new Insets(0, 4, 18, 8);
>         gbc.ipadx = 0;
>         for (int x=0; x < 16; x++) {
>             gbc.gridx=x;
>             panel.add(new JLabel(Integer.toString(x)), gbc);
>         }
> The results are interesting.
> I think you could use fewer than 16 columns and this might make things 
> more predictable. I suspect some columns are shrinking to zero width 
> because no component STARTS in them.

I have indeed tried fewer columns and the results are identical.  I have 
tried as few as 6 columns which is the minimum required to get the results I 
desire.  It just doesn't seem to work as it should.

> Personally I'd use MigLayout and have about 1/10th the code.

I have never used it but I really need to understand why this code is 
failing.  Any other ideas?

-- 
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1492 — Re: GridBagLayout not beh

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<462732e8$0$10724$db0fefd9@news.zen.co.uk>
In reply to#1486
  To: comp.lang.java.gui
Qu0ll wrote:
> "Ian Wilson" <scobloke2@infotop.co.uk> wrote
> 
> [snip]
> 
>> I think you could use fewer than 16 columns and this might make things 
>> more predictable. I suspect some columns are shrinking to zero width 
>> because no component STARTS in them.
> 
> I have indeed tried fewer columns and the results are identical.  I have 
> tried as few as 6 columns which is the minimum required to get the 
> results I desire.  It just doesn't seem to work as it should.
> 
>> Personally I'd use MigLayout and have about 1/10th the code.
> 
> I have never used it but I really need to understand why this code is 
> failing.  Any other ideas?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class GBTest extends JFrame {

     private JPanel panel = new JPanel();
     private JLabel userLabel = new JLabel("User:");
     private JLabel passwordLabel = new JLabel("Password:");
     private JLabel dbLabel = new JLabel("Database:");
     private JTextField userTextField = new JTextField();
     private JTextField dbTextField = new JTextField();
     private JPasswordField passwordField = new JPasswordField(8);

     private JButton dbButton = new JButton("...");

     public GBTest() {
         setLayout(new BorderLayout());
         panel.setLayout(new GridBagLayout());

         userLabel.setText("User:");
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.gridx = 0;
         gbc.gridy = 0;
         gbc.gridwidth = 1;
         gbc.anchor = GridBagConstraints.EAST;
         gbc.insets = new Insets(0, 4, 18, 0);
         gbc.ipadx = 0;
         panel.add(userLabel, gbc);

         gbc.gridx = 1;
         gbc.gridy = 0;
         gbc.gridwidth = 1;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.weightx = 0.5;
         gbc.weighty = 0.0;
         gbc.insets = new Insets(0, 4, 18, 4);
         gbc.ipadx = 40;
         panel.add(userTextField, gbc);

         gbc = new GridBagConstraints();
         gbc.gridx = 2;
         gbc.gridy = 0;
         gbc.gridwidth = 1;
         gbc.anchor = GridBagConstraints.EAST;
         gbc.insets = new Insets(0, 4, 18, 0);
         gbc.ipadx = 0;
         panel.add(passwordLabel, gbc);

         gbc.gridx = 3;
         gbc.gridy = 0;
         gbc.gridwidth = 2;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.weightx = 0.5;
         gbc.weighty = 0.0;
         gbc.insets = new Insets(0, 4, 18, 8);
         gbc.ipadx = 0;
         panel.add(passwordField, gbc);

         gbc.gridx = 0;
         gbc.gridy = 1;
         gbc.gridwidth = 1;
         gbc.anchor = GridBagConstraints.EAST;
         gbc.insets = new Insets(0, 4, 18, 0);
         gbc.ipadx = 0;
         panel.add(dbLabel, gbc);

         gbc.gridx = 1;
         gbc.gridy = 1;
         gbc.gridwidth = 3;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.weightx = 10;
         gbc.weighty = 0.0;
         gbc.insets = new Insets(0, 4, 18, 0);
         gbc.ipadx = 0;
         panel.add(dbTextField, gbc);

         gbc.gridx = 4;
         gbc.gridy = 1;
         gbc.gridwidth = 1;
         gbc.fill = GridBagConstraints.NONE;
         gbc.anchor = GridBagConstraints.EAST;
         gbc.weightx = 0.0;
         gbc.weighty = 0.0;
         gbc.insets = new Insets(0, 4, 18, 8);
         gbc.ipadx = 0;
         panel.add(dbButton, gbc);

         add(panel, BorderLayout.CENTER);

         pack();
         setPreferredSize(new Dimension(400, 250));
         setSize(new Dimension(400, 250));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new GBTest().setVisible(true);
             }
         });
     }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1498 — Re: GridBagLayout not beh

From"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<462769b7$0$25486$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
In reply to#1492
  To: comp.lang.java.gui
"Ian Wilson" <scobloke2@infotop.co.uk> wrote in message 
news:462732e8$0$10724$db0fefd9@news.zen.co.uk...

> import java.awt.BorderLayout;
> import java.awt.Dimension;
> import java.awt.EventQueue;
> import java.awt.GridBagConstraints;
> import java.awt.GridBagLayout;
> import java.awt.Insets;
>
> import javax.swing.Box;
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JPasswordField;
> import javax.swing.JTextField;
>
> public class GBTest extends JFrame {
>
>     private JPanel panel = new JPanel();
>     private JLabel userLabel = new JLabel("User:");
>     private JLabel passwordLabel = new JLabel("Password:");
>     private JLabel dbLabel = new JLabel("Database:");
>     private JTextField userTextField = new JTextField();
>     private JTextField dbTextField = new JTextField();
>     private JPasswordField passwordField = new JPasswordField(8);
>
>     private JButton dbButton = new JButton("...");
>
>     public GBTest() {
>         setLayout(new BorderLayout());
>         panel.setLayout(new GridBagLayout());
>
>         userLabel.setText("User:");
>         GridBagConstraints gbc = new GridBagConstraints();
>         gbc.gridx = 0;
>         gbc.gridy = 0;
>         gbc.gridwidth = 1;
>         gbc.anchor = GridBagConstraints.EAST;
>         gbc.insets = new Insets(0, 4, 18, 0);
>         gbc.ipadx = 0;
>         panel.add(userLabel, gbc);
>
>         gbc.gridx = 1;
>         gbc.gridy = 0;
>         gbc.gridwidth = 1;
>         gbc.fill = GridBagConstraints.HORIZONTAL;
>         gbc.weightx = 0.5;
>         gbc.weighty = 0.0;
>         gbc.insets = new Insets(0, 4, 18, 4);
>         gbc.ipadx = 40;
>         panel.add(userTextField, gbc);
>
>         gbc = new GridBagConstraints();
>         gbc.gridx = 2;
>         gbc.gridy = 0;
>         gbc.gridwidth = 1;
>         gbc.anchor = GridBagConstraints.EAST;
>         gbc.insets = new Insets(0, 4, 18, 0);
>         gbc.ipadx = 0;
>         panel.add(passwordLabel, gbc);
>
>         gbc.gridx = 3;
>         gbc.gridy = 0;
>         gbc.gridwidth = 2;
>         gbc.fill = GridBagConstraints.HORIZONTAL;
>         gbc.weightx = 0.5;
>         gbc.weighty = 0.0;
>         gbc.insets = new Insets(0, 4, 18, 8);
>         gbc.ipadx = 0;
>         panel.add(passwordField, gbc);
>
>         gbc.gridx = 0;
>         gbc.gridy = 1;
>         gbc.gridwidth = 1;
>         gbc.anchor = GridBagConstraints.EAST;
>         gbc.insets = new Insets(0, 4, 18, 0);
>         gbc.ipadx = 0;
>         panel.add(dbLabel, gbc);
>
>         gbc.gridx = 1;
>         gbc.gridy = 1;
>         gbc.gridwidth = 3;
>         gbc.fill = GridBagConstraints.HORIZONTAL;
>         gbc.weightx = 10;
>         gbc.weighty = 0.0;
>         gbc.insets = new Insets(0, 4, 18, 0);
>         gbc.ipadx = 0;
>         panel.add(dbTextField, gbc);
>
>         gbc.gridx = 4;
>         gbc.gridy = 1;
>         gbc.gridwidth = 1;
>         gbc.fill = GridBagConstraints.NONE;
>         gbc.anchor = GridBagConstraints.EAST;
>         gbc.weightx = 0.0;
>         gbc.weighty = 0.0;
>         gbc.insets = new Insets(0, 4, 18, 8);
>         gbc.ipadx = 0;
>         panel.add(dbButton, gbc);
>
>         add(panel, BorderLayout.CENTER);
>
>         pack();
>         setPreferredSize(new Dimension(400, 250));
>         setSize(new Dimension(400, 250));
>         setLocationRelativeTo(null);
>         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>     }
>
>     public static void main(String[] args) {
>         EventQueue.invokeLater(new Runnable() {
>             public void run() {
>                 new GBTest().setVisible(true);
>             }
>         });
>     }
> }

Hmm, not quite there but closer - thanks.  Perhaps GridBagLayout is not the 
answer.

-- 
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1493 — Re: GridBagLayout not beh

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<46273424$0$10730$db0fefd9@news.zen.co.uk>
In reply to#1486
  To: comp.lang.java.gui
Qu0ll wrote:
> "Ian Wilson" <scobloke2@infotop.co.uk> wrote 
> [snip]
> 
>> I think you could use fewer than 16 columns and this might make things 
>> more predictable. I suspect some columns are shrinking to zero width 
>> because no component STARTS in them.
> 
> I have indeed tried fewer columns and the results are identical.  I have 
> tried as few as 6 columns which is the minimum required to get the 
> results I desire.  It just doesn't seem to work as it should.
> 
>> Personally I'd use MigLayout and have about 1/10th the code.
> 
> I have never used it but I really need to understand why this code is 
> failing.  Any other ideas?
> 


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class GBTest extends JFrame {
     private JPanel panel = new JPanel();
     private JLabel userLabel = new JLabel("User:");
     private JLabel passwordLabel = new JLabel("Password:");
     private JLabel dbLabel = new JLabel("Database:");
     private JTextField userTextField = new JTextField(10);
     private JTextField dbTextField = new JTextField(14);
     private JPasswordField passwordField = new JPasswordField(8);
     private JButton dbButton = new JButton("...");

     public GBTest() {
         setLayout(new BorderLayout());
         panel.setLayout(new MigLayout("wrap 4"));

         panel.add(userLabel);
         panel.add(userTextField);
         panel.add(passwordLabel);
         panel.add(passwordField, "span 2");
         panel.add(dbLabel);
         panel.add(dbTextField, "span 3");
         panel.add(dbButton);

         add(panel, BorderLayout.CENTER);

         pack();
         setPreferredSize(new Dimension(400, 250));
         setSize(new Dimension(400, 250));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new GBTest().setVisible(true);
             }
         });
     }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1495 — Re: GridBagLayout not beh

From"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<4627693f$0$25440$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
In reply to#1493
  To: comp.lang.java.gui
"Ian Wilson" <scobloke2@infotop.co.uk> wrote in message 
news:46273424$0$10730$db0fefd9@news.zen.co.uk...

> import java.awt.BorderLayout;
> import java.awt.Dimension;
> import java.awt.EventQueue;
> import java.awt.GridBagConstraints;
> import java.awt.Insets;
>
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JPasswordField;
> import javax.swing.JTextField;
>
> import net.miginfocom.swing.MigLayout;
>
> public class GBTest extends JFrame {
>     private JPanel panel = new JPanel();
>     private JLabel userLabel = new JLabel("User:");
>     private JLabel passwordLabel = new JLabel("Password:");
>     private JLabel dbLabel = new JLabel("Database:");
>     private JTextField userTextField = new JTextField(10);
>     private JTextField dbTextField = new JTextField(14);
>     private JPasswordField passwordField = new JPasswordField(8);
>     private JButton dbButton = new JButton("...");
>
>     public GBTest() {
>         setLayout(new BorderLayout());
>         panel.setLayout(new MigLayout("wrap 4"));
>
>         panel.add(userLabel);
>         panel.add(userTextField);
>         panel.add(passwordLabel);
>         panel.add(passwordField, "span 2");
>         panel.add(dbLabel);
>         panel.add(dbTextField, "span 3");
>         panel.add(dbButton);
>
>         add(panel, BorderLayout.CENTER);
>
>         pack();
>         setPreferredSize(new Dimension(400, 250));
>         setSize(new Dimension(400, 250));
>         setLocationRelativeTo(null);
>         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>     }
>
>     public static void main(String[] args) {
>         EventQueue.invokeLater(new Runnable() {
>             public void run() {
>                 new GBTest().setVisible(true);
>             }
>         });
>     }
> }

Thanks Ian.  I am impressed with the conciseness of this layout but 
unfortunately when I run that it puts the button on the next line and not 
next to the text field as desired.  Do you get the same result?  I have just 
downloaded the latest MiGLayout and am using Java 6 Update 2.

-- 
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1497 — Re: GridBagLayout not beh

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<46276cdb$0$19246$da0feed9@news.zen.co.uk>
In reply to#1495
  To: comp.lang.java.gui
Qu0ll wrote:
> "Ian Wilson" <scobloke2@infotop.co.uk> wrote in message 
> news:46273424$0$10730$db0fefd9@news.zen.co.uk...
> 
>> import java.awt.BorderLayout;
>> import java.awt.Dimension;
>> import java.awt.EventQueue;
>> import java.awt.GridBagConstraints;
>> import java.awt.Insets;
>>
>> import javax.swing.JButton;
>> import javax.swing.JFrame;
>> import javax.swing.JLabel;
>> import javax.swing.JPanel;
>> import javax.swing.JPasswordField;
>> import javax.swing.JTextField;
>>
>> import net.miginfocom.swing.MigLayout;
>>
>> public class GBTest extends JFrame {
>>     private JPanel panel = new JPanel();
>>     private JLabel userLabel = new JLabel("User:");
>>     private JLabel passwordLabel = new JLabel("Password:");
>>     private JLabel dbLabel = new JLabel("Database:");
>>     private JTextField userTextField = new JTextField(10);
>>     private JTextField dbTextField = new JTextField(14);
>>     private JPasswordField passwordField = new JPasswordField(8);
>>     private JButton dbButton = new JButton("...");
>>
>>     public GBTest() {
>>         setLayout(new BorderLayout());
>>         panel.setLayout(new MigLayout("wrap 4"));
>>
>>         panel.add(userLabel);
>>         panel.add(userTextField);
>>         panel.add(passwordLabel);
>>         panel.add(passwordField, "span 2");
>>         panel.add(dbLabel);
>>         panel.add(dbTextField, "span 3");
>>         panel.add(dbButton);
>>
>>         add(panel, BorderLayout.CENTER);
>>
>>         pack();
>>         setPreferredSize(new Dimension(400, 250));
>>         setSize(new Dimension(400, 250));
>>         setLocationRelativeTo(null);
>>         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>>     }
>>
>>     public static void main(String[] args) {
>>         EventQueue.invokeLater(new Runnable() {
>>             public void run() {
>>                 new GBTest().setVisible(true);
>>             }
>>         });
>>     }
>> }
> 
> 
> Thanks Ian.  I am impressed with the conciseness of this layout but 
> unfortunately when I run that it puts the button on the next line and 
> not next to the text field as desired.  Do you get the same result?  I 
> have just downloaded the latest MiGLayout and am using Java 6 Update 2.
> 

I must have an older version of MigLayout.

1) Try

         panel.setLayout(new MigLayout()); // ***

         panel.add(userLabel);
         panel.add(userTextField);
         panel.add(passwordLabel);
         panel.add(passwordField, "span 2, wrap"); // ***
         panel.add(dbLabel);
         panel.add(dbTextField, "span 3");
         panel.add(dbButton);

2) If that doesn't work read
http://www.miglayout.com/QuickStart.pdf
http://www.miglayout.com/whitepaper.html

I'd bear in mind how long you have spent learning GridBagLayout so far 
and allow yourself at least a tenth of that time to read the above 
documents and learn the basics of MigLayout.

MigLayout has some powerful, and hence complex looking, options, so 
don't try to understand all of them to start with. As you can see, you 
usually only need a very few directives to get the layout you want.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1494 — Re: GridBagLayout not beh

From"Axel Hallez" <axel.hallez@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<f077fo$ml2$1@gaudi2.UGent.be>
In reply to#1478
  To: comp.lang.java.gui
Qu0ll wrote:
> I have a problem with GridBagLayout that I would appreciate some help 
> with in solving.
> 
> In the following simple example I am trying to have a user label and 
> text field plus a password label and field evenly spaced across the 
> panel with a database field below it and a button all the way over to 
> the right taking up very little space.  To do this I have set up a 2 x 
> 16 grid but it's not behaving as I believe I am specifying it.  The 
> problem is that the button is taking up the same amount of space as the 
> password field even though the password field has a grid width of 4 and 
> the button only 1.  The result is that the top line is not evenly spaced 
> and too much room is allocated for the button.
> 
> Why is this?  What's wrong with my GridBagLayout definition or usage?
> 

First of all, you seem to think that the gridwidth is used to determine 
the size ratios of the components (eg. a component with gridwidth 2 must 
be double as wide as one with gridwidth 1).
This is not true. The width of a column is determined by the size of the 
components in that column.
However, if you have a component with a gridwidth of 2 (or more), gb 
layout makes sure that the total width of both columns is at least the 
size of the component. If one of those colums has no other components 
(that have their anchor in that column) this column is assigned a width 
of 0 and all of the width of the component is assigned to the other 
column. This is why the last column in your layout is as wide as the 
password field.

If there is space left in the container, the remaining space is 
distributed over the columns. The weights of the columns determines how 
much extra space is allocated to each column.

As far as I can see, the exact layout you want te achieve is not 
possible using gridbaglayout. The closest I could get is putting the 
dbButton next to the password field.

I've changed your code in this sense.

Hope this helps,
Axel Hallez



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class GBTest extends JFrame {

private JPanel panel = new JPanel();
private JLabel userLabel = new JLabel("User:");
private JLabel passwordLabel = new JLabel("Password:");
private JLabel dbLabel = new JLabel("Database:");
private JTextField userTextField = new JTextField();
private JTextField dbTextField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JButton dbButton = new JButton("...");

public GBTest() {
  setLayout(new BorderLayout());
  panel.setLayout(new GridBagLayout());

  userLabel.setText("User:");
  GridBagConstraints gbc = new GridBagConstraints();
  gbc.gridx = 0;
  gbc.gridy = 0;
  gbc.gridwidth = 1;
  gbc.weightx = 0.0;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(userLabel, gbc);

  gbc.gridx = 1;
  gbc.gridy = 0;
  gbc.gridwidth = 1;
  gbc.anchor = GridBagConstraints.WEST;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 1.0;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 4);
  gbc.ipadx = 0;
  panel.add(userTextField, gbc);

  gbc = new GridBagConstraints();
  gbc.gridx = 2;
  gbc.gridy = 0;
  gbc.weightx = 0.0;
  gbc.gridwidth = 1;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(passwordLabel, gbc);

  gbc.gridx = 3;
  gbc.gridy = 0;
  gbc.gridwidth = 1;
  gbc.anchor = GridBagConstraints.WEST;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 1.0;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 8);
  gbc.ipadx = 0;
  panel.add(passwordField, gbc);

  gbc.gridx = 0;
  gbc.gridy = 1;
  gbc.gridwidth = 1;
  gbc.weightx = 0.0;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(dbLabel, gbc);

  gbc.gridx = 1;
  gbc.gridy = 1;
  gbc.gridwidth = 3;
  gbc.anchor = GridBagConstraints.WEST;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.weightx = 0;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 0);
  gbc.ipadx = 0;
  panel.add(dbTextField, gbc);

  gbc.gridx = 4;
  gbc.gridy = 1;
  gbc.gridwidth = 1;
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.EAST;
  gbc.weightx = 0.0;
  gbc.weighty = 0.0;
  gbc.insets = new Insets(0, 4, 18, 8);
  gbc.ipadx = 0;
  panel.add(dbButton, gbc);

  add(panel, BorderLayout.CENTER);

  pack();
  setPreferredSize(new Dimension(400, 250));
  setSize(new Dimension(400, 250));
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    new GBTest().setVisible(true);
   }
  });
}
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1496 — Re: GridBagLayout not beh

From"Qu0ll" <qu0ll@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<46276a21$0$25499$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
In reply to#1494
  To: comp.lang.java.gui
"Axel Hallez" <Axel.Hallez@ugent.be> wrote in message 
news:f077fo$ml2$1@gaudi2.UGent.be...

> First of all, you seem to think that the gridwidth is used to determine 
> the size ratios of the components (eg. a component with gridwidth 2 must 
> be double as wide as one with gridwidth 1).
> This is not true. The width of a column is determined by the size of the 
> components in that column.
> However, if you have a component with a gridwidth of 2 (or more), gb 
> layout makes sure that the total width of both columns is at least the 
> size of the component. If one of those colums has no other components 
> (that have their anchor in that column) this column is assigned a width of 
> 0 and all of the width of the component is assigned to the other column. 
> This is why the last column in your layout is as wide as the password 
> field.
>
> If there is space left in the container, the remaining space is 
> distributed over the columns. The weights of the columns determines how 
> much extra space is allocated to each column.
>
> As far as I can see, the exact layout you want te achieve is not possible 
> using gridbaglayout. The closest I could get is putting the dbButton next 
> to the password field.
>
> I've changed your code in this sense.

[snip code]

OK thanks Axel for the detailed explanation.  I do feel now with this and 
other posts that a different layout manager is required.

-- 
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1500 — Re: GridBagLayout not beh

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: GridBagLayout not beh
Message-ID<70f07016494f2@uwe>
In reply to#1496
  To: comp.lang.java.gui
Qu0ll wrote:
..
>....I do feel now with this and 
>other posts that a different layout manager is required.

Do you want to stick to core J2SE* layouts?
I would probably approach this as a nested layout 
(but first I'd need to understand where the extra space 
on expanding, needs to go).

( * Ian's solution with the MigLayout looked elegantly 
short, but since I don't have MigLayout lying about 
the place, I was unable to 'take it for a test drive'. )

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200704/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web