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


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

TotallyGridBag

Started by"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
First post2011-04-27 15:29 +0000
Last post2011-04-27 15:29 +0000
Articles 3 — 2 participants

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


Contents

  TotallyGridBag "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
    Re: TotallyGridBag "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
      Re: TotallyGridBag "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000

#706 — TotallyGridBag

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectTotallyGridBag
Message-ID<cuadnUiRd5qTkDPYnZ2dnUVZ8tignZ2d@bt.com>
  To: comp.lang.java.gui
I was putting together some comparisons of Layout Managers and am a bit 
stuck with GridBagLayout. In the example below, if you resize the frame 
so it is a bit narrower, the Password JTextField collapses to zero width.

I recognised this as something I'd seen in madbean's TotallyGridBag 
animation[1]. So I watched that over again, only to discover that the 
hero gives up before solving it, so I couldn't copy his solution :-(

Clues?

------------------------------------------------------------------
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FormSmallGridBag extends JPanel {
     FormSmallGridBag() {
         setLayout(new GridBagLayout());
         GridBagConstraints gc = new GridBagConstraints();
         gc.gridx = 0;
         gc.gridy = 0;
         gc.anchor = GridBagConstraints.LINE_END;
         add(new JLabel("Name:"), gc);
         gc.gridx = 1;
         gc.gridy = 0;
         gc.anchor = GridBagConstraints.LINE_START;
         gc.fill = GridBagConstraints.HORIZONTAL;
         gc.weightx = 0.9;
         add(new JTextField(16), gc);
         gc.gridx = 0;
         gc.gridy = 1;
         gc.fill = GridBagConstraints.NONE;
         gc.weightx = 0.0; // reset to default
         add(new JLabel("Password:"), gc);
         gc.gridx = 1;
         gc.gridy = 1;
         add(new JTextField(10), gc);  // Collapses if frame shrunk!
     }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame("Form Small - GridBagLayout");
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.add(new FormSmallGridBag());
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}
------------------------------------------------------------------

[1] http://madbean.com/anim/totallygridbag

---
 * 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]


#707

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
Message-ID<1169035511.688189.5730@m58g2000cwm.googlegroups.com>
In reply to#706
  To: comp.lang.java.gui
Ian Wilson wrote:
> I was putting together some comparisons of Layout Managers and am a bit
> stuck with GridBagLayout.

(chuckle) When I saw you recommending it
on another groups, I was almost about to add
that I preferred nested layouts, for any number
of reasons.

>...In the example below, if you resize the frame
> so it is a bit narrower, the Password JTextField collapses to zero width.

Yep.

> Clues?
>
> ------------------------------------------------------------------
> import java.awt.GridBagConstraints;
> import java.awt.GridBagLayout;
>
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JTextField;
> import javax.swing.SwingUtilities;
>
> public class FormSmallGridBag extends JPanel {
>      FormSmallGridBag() {
>          setLayout(new GridBagLayout());
>          GridBagConstraints gc = new GridBagConstraints();
>          gc.gridx = 0;
>          gc.gridy = 0;
>          gc.anchor = GridBagConstraints.LINE_END;
>          add(new JLabel("Name:"), gc);
>          gc.gridx = 1;
>          gc.gridy = 0;
>          gc.anchor = GridBagConstraints.LINE_START;
>          gc.fill = GridBagConstraints.HORIZONTAL;
>          gc.weightx = 0.9;
>          add(new JTextField(16), gc);
>          gc.gridx = 0;
>          gc.gridy = 1;
>          gc.fill = GridBagConstraints.NONE;
>          gc.weightx = 0.0; // reset to default
>          add(new JLabel("Password:"), gc);
>          gc.gridx = 1;
>          gc.gridy = 1;

         JTextField tf = new JTextField(10);
         tf.setMinimumSize( new java.awt.Dimension(30,20) );
         add(tf, gc);  // (Does not) Collapses (too far) if frame
shrunk!

>      }
>
>      public static void main(String[] args) {
>          SwingUtilities.invokeLater(new Runnable() {
>              public void run() {
>                  JFrame f = new JFrame("Form Small - GridBagLayout");
>                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>                  f.add(new FormSmallGridBag());
>                  f.pack();
>                  f.setVisible(true);
>              }
>          });
>      }
> }
> ------------------------------------------------------------------

GBL usually behaves until it strikes a component
with no minimum size set, but cannot give it its
preferred size.  Then it simply reduces it to '0'.

That is one of the reasons I avoid it.  I have
encountered no layout besides GBL that you
need to worry about setting a minimum size.

Andrew T.

---
 * 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]


#708

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
Message-ID<vbqdnZQwjtwNhzPYnZ2dnUVZ8sSrnZ2d@bt.com>
In reply to#707
  To: comp.lang.java.gui
Andrew Thompson wrote:
> Ian Wilson wrote:
> 
>> I was putting together some comparisons of Layout Managers and am a bit
>> stuck with GridBagLayout.
> 
> 
> (chuckle) When I saw you recommending it
> on another groups, I was almost about to add
> that I preferred nested layouts, for any number
> of reasons.
> 

I usually recommend MigLayout but I recognise that some learners might 
want to stick with Sun's layout managers. :-)

> 
>> ...In the example below, if you resize the frame
>> so it is a bit narrower, the Password JTextField collapses to zero width.
>> Clues?
>>
<snip>
>          JTextField tf = new JTextField(10);
>          tf.setMinimumSize( new java.awt.Dimension(30,20) );
>          add(tf, gc);  // (Does not) Collapses (too far) if frame
> shrunk!
> 
<snip>

Thanks Andrew :-)

> GBL usually behaves until it strikes a component
> with no minimum size set, but cannot give it its
> preferred size.  Then it simply reduces it to '0'.
> 
> That is one of the reasons I avoid it.  I have
> encountered no layout besides GBL that you
> need to worry about setting a minimum size.

I'd be happier if component's constructors set min and max sizes 
different from preferred (e.g. by half/twice unless programmer set) and 
if all Sun's layout managers used them consistently. Doubtless there are 
good reasons for the way things are.

---
 * 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