java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to align your components in horizontal or vertical layout
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4096
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to align your components in horizontal or vertical layout E-mail
User Rating: / 130
PoorBest 
BoxLayout

This Java Swing tip illustrates a layout that helps in aligning your compnents in horizontal and vertical manner. This is done by using BoxLayout in your application. BoxLayout is a layout manager that either stacks its components on top of each other or places them in a row your choice.

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

public class BoxLayoutDemo extends JFrame {
   public BoxLayoutDemo()
   {
      super"Demostrating BoxLayout" );
      final int SIZE = 3;

      Container c = getContentPane();
      c.setLayoutnew BorderLayout3030 ) );

      Box boxes[] new Box];

      boxes= Box.createHorizontalBox();
      boxes= Box.createVerticalBox();
      boxes= Box.createHorizontalBox();
      boxes= Box.createVerticalBox();

      // add buttons to boxes[ 0 ]
      for int i = 0; i < SIZE; i++ )
         boxes].addnew JButton"boxes[0]: " + i ) );

      // create strut and add buttons to boxes[ 1 ]
      for int i = 0; i < SIZE; i++ ) {
         boxes].addBox.createVerticalStrut25 ) );
         boxes].addnew JButton"boxes[1]: " + i ) );
      }

      // create horizontal glue and add buttons to boxes[ 2 ]
      for int i = 0; i < SIZE; i++ ) {
         boxes].addBox.createHorizontalGlue() );
         boxes].addnew JButton"boxes[2]: " + i ) );
      }

      // create rigid area and add buttons to boxes[ 3 ]
      for int i = 0; i < SIZE; i++ ) {
         boxes].add(
            Box.createRigidAreanew Dimension12) ) );
         boxes].addnew JButton"boxes[3]: " + i ) );
      }

      // create horizontal glue and add buttons to panel
      JPanel panel = new JPanel();
      panel.setLayout(
         new BoxLayoutpanel, BoxLayout.Y_AXIS ) );

      for int i = 0; i < SIZE; i++ ) {
         panel.addBox.createGlue() );
         panel.addnew JButton"panel: " + i ) );
      }

      // place panels on frame
      c.addboxes], BorderLayout.NORTH );
      c.addboxes], BorderLayout.EAST );
      c.addboxes], BorderLayout.SOUTH );
      c.addboxes], BorderLayout.WEST );
      c.addpanel, BorderLayout.CENTER );

      setSize350300 );
      show();
   }

   public static void mainString args[] )
   {
      BoxLayoutDemo app = new BoxLayoutDemo();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit);
            }
         }
      );
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.