Labeled button row using BorderLayout

  • Follow


I have a JPanel with BorderLayout.
I want to have a label (icon) in the west area, and a row of buttons
in the center.
When the user stretches the component vertically, the icon remains
centered vertically, but the button row stays pegged to the top of the
center area. How can I get the buttons to remain centered vertically?

I am placing a JPanel in the center, with flow layout. Then I add the
buttons to that center panel:

         JPanel centerPanel = new JPanel();
         centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );
         centerPanel.setAlignmentX( Component.CENTER_ALIGNMENT );
         //  ... add buttons to centerPanel


         JLabel Label= new JlLabel();
         Label.setIcon(myIcon);

         JPanel p = new JPanel( new BorderLayout() );
         p.setAlignmentX( Component.CENTER_ALIGNMENT );
         p.setAlignmentY( Component.CENTER_ALIGNMENT );
         p.add( centerPanel, BorderLayout.CENTER );
         p.add( Label, BorderLayout.WEST );

--
Fred K
0
Reply Fred 9/30/2010 7:41:01 PM

On 9/30/2010 12:41 PM, Fred wrote:
> I have a JPanel with BorderLayout.
> I want to have a label (icon) in the west area, and a row of buttons
> in the center.
> When the user stretches the component vertically, the icon remains
> centered vertically, but the button row stays pegged to the top of the
> center area. How can I get the buttons to remain centered vertically?
>
> I am placing a JPanel in the center, with flow layout. Then I add the
> buttons to that center panel:
>
>           JPanel centerPanel = new JPanel();
>           centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );
>           centerPanel.setAlignmentX( Component.CENTER_ALIGNMENT );
>           //  ... add buttons to centerPanel
>
>
>           JLabel Label= new JlLabel();
>           Label.setIcon(myIcon);
>
>           JPanel p = new JPanel( new BorderLayout() );
>           p.setAlignmentX( Component.CENTER_ALIGNMENT );
>           p.setAlignmentY( Component.CENTER_ALIGNMENT );
>           p.add( centerPanel, BorderLayout.CENTER );
>           p.add( Label, BorderLayout.WEST );
>
> --
> Fred K

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

public class test extends JPanel {
     public test() {
         super(new GridBagLayout());

         GridBagConstraints c = new GridBagConstraints();
         c.gridx = c.gridy = 0;

         JButton b1 = new JButton("One");
         JButton b2 = new JButton("Two");
         JButton b3 = new JButton("Three");
         JButton b4 = new JButton("Four");

         add(b1,c);
         ++c.gridy;
         add(b2,c);
         ++c.gridy;
         add(b3,c);
         ++c.gridy;
         add(b4,c);

     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 JLabel l = new JLabel("Label");
                 f.add(l,BorderLayout.WEST);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply Knute 9/30/2010 8:34:15 PM


On 9/30/2010 12:41 PM, Fred wrote:
> How can I get the buttons to remain centered vertically?
>
> I am placing a JPanel in the center, with flow layout. Then I add the
> buttons to that center panel:
>
>           JPanel centerPanel = new JPanel();
>           centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );


You can use a Box layout to do this, and insert stretchy "glue" bits 
before and after the component to get a centered effect.

In the example below, I make two Boxes.  One is a vertical layout, and 
holds the stretchy glue bits.  It has just one component, which is the 
entire row of buttons.

Then the row of buttons itself is just a box with a horizontal 
configuration (and no glue).

For more one Box and glue, see the Swing tutorial:

<http://download.oracle.com/javase/tutorial/uiswing/layout/box.html>


package test;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BorderLayoutTest {

    public static void main( String[] args )
    {
       SwingUtilities.invokeLater( new Runnable() {

          public void run()
          {
             createAndShowGui();
          }

       } );
    }

    private static void createAndShowGui()
    {
       JFrame frame = new JFrame();
       Box centerPanel = Box.createVerticalBox();
       Box buttonRow = Box.createHorizontalBox();
       buttonRow.add( new JButton( "Button 1" ) );
       buttonRow.add( new JButton( "Button 2" ) );
       buttonRow.add( new JButton( "Button 3" ) );
       buttonRow.add( new JButton( "Button 4" ) );

       centerPanel.add( Box.createVerticalGlue() );
       centerPanel.add( buttonRow );
       centerPanel.add( Box.createVerticalGlue() );
       frame.add(  centerPanel );
       frame.add( new JLabel( "West" ), BorderLayout.WEST );

       frame.pack();
       frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       frame.setLocationRelativeTo( null );
       frame.setVisible( true );
    }
}
0
Reply markspace 9/30/2010 11:04:47 PM

On Sep 30, 4:04=A0pm, markspace <nos...@nowhere.com> wrote:
> On 9/30/2010 12:41 PM, Fred wrote:
>
> > How can I get the buttons to remain centered vertically?
>
> > I am placing a JPanel in the center, with flow layout. Then I add the
> > buttons to that center panel:
>
> > =A0 =A0 =A0 =A0 =A0 JPanel centerPanel =3D new JPanel();
> > =A0 =A0 =A0 =A0 =A0 centerPanel.setAlignmentY( Component.CENTER_ALIGNME=
NT );
>
> You can use a Box layout to do this, and insert stretchy "glue" bits
> before and after the component to get a centered effect.
>
> In the example below, I make two Boxes. =A0One is a vertical layout, and
> holds the stretchy glue bits. =A0It has just one component, which is the
> entire row of buttons.
>
> Then the row of buttons itself is just a box with a horizontal
> configuration (and no glue).
>
> For more one Box and glue, see the Swing tutorial:
>
> <http://download.oracle.com/javase/tutorial/uiswing/layout/box.html>
>
> package test;
>
> import java.awt.BorderLayout;
> import java.awt.Component;
> import javax.swing.Box;
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.SwingUtilities;
>
> public class BorderLayoutTest {
>
> =A0 =A0 public static void main( String[] args )
> =A0 =A0 {
> =A0 =A0 =A0 =A0SwingUtilities.invokeLater( new Runnable() {
>
> =A0 =A0 =A0 =A0 =A0 public void run()
> =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0createAndShowGui();
> =A0 =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0} );
> =A0 =A0 }
>
> =A0 =A0 private static void createAndShowGui()
> =A0 =A0 {
> =A0 =A0 =A0 =A0JFrame frame =3D new JFrame();
> =A0 =A0 =A0 =A0Box centerPanel =3D Box.createVerticalBox();
> =A0 =A0 =A0 =A0Box buttonRow =3D Box.createHorizontalBox();
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 1" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 2" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 3" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 4" ) );
>
> =A0 =A0 =A0 =A0centerPanel.add( Box.createVerticalGlue() );
> =A0 =A0 =A0 =A0centerPanel.add( buttonRow );
> =A0 =A0 =A0 =A0centerPanel.add( Box.createVerticalGlue() );
> =A0 =A0 =A0 =A0frame.add( =A0centerPanel );
> =A0 =A0 =A0 =A0frame.add( new JLabel( "West" ), BorderLayout.WEST );
>
> =A0 =A0 =A0 =A0frame.pack();
> =A0 =A0 =A0 =A0frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
> =A0 =A0 =A0 =A0frame.setLocationRelativeTo( null );
> =A0 =A0 =A0 =A0frame.setVisible( true );
> =A0 =A0 }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

That works great. It also works better than other things I tried when
you add elements - it resizes properly on revalidate().
--
Fred K
0
Reply Fred 10/5/2010 6:22:56 PM

3 Replies
303 Views

(page loaded in 0.068 seconds)

Similiar Articles:













7/24/2012 12:25:05 AM


Reply: