Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: Summary - GUI Layouts 2 - BoxLayout, CardLayout, GridbagLayout

 

In this section we will learn important methods of GUI Layouts.

 

Following Layouts are discussed:

 

a) BoxLayout

b) CardLayout

c) GridBagLayout

 

 

Layout, layo, becomes that layout manager for container p (a JPanel or Container) with the following.

   p.setLayout(layo);

Layouts (BoxLayout, CardLayout, GridbagLayout)

BoxLayout, Box - Lays out components in single row or col. Add "glue" and rigid areas to control spacing.
boxl = new BoxLayout(p, dir); Creates layout for container p. dir is BoxLayout.X_AXIS BoxLayout.Y_AXIS. Note: must have already created p.
box = new Box(dir); Creates a Container with vertical or horizontal layout.
p.add(widget); Adds widget to the next position.
These static Box methods create useful spacing components, which can be used in other layouts.
Box.createVerticalStrut(n) Creates a vertical spacer n pixels high.
Box.createHorizontalStrut(n) Creates a horizontal spacer n pixels wide.
Box.createRigidArea(dim) Spacer with width and height. dim is Dimension object, eg, new Dimension(10, 0).
Box.createHorizontalGlue() Creates expandable horizontal space.
Box.createVerticalGlue() Creates expandable vertical space.
new Box.Filler(mn,prf,mx) Creates a Box.Filler with min, preferred, and max sizes, each is Dimension object.
CardLayout - Panels are placed on top of each other like stack of cards -- only one visible at a time.
Used to produce installer or wizard style interface as steps in process, or to select palette. Use JTabbedPanel if user should have control.
crdMgr = new CardLayout(); Sets layout to CardLayout.
crdMgr = new CardLayout(h, v)); Also can specify horizontal and vertical padding.
p.add(cardPanel); Adds next card to panel.
p.add(cardPanel, name); Adds next card to panel. Use String name to select it later.
crdMgr.first(p); Panel p displays the first card in the layout of p.
crdMgr.next(p); Panel p displays the next card in the layout of p.
crdMgr.previous(p); Panel p displays the previous card in the layout of p.
crdMgr.last(p); Panel p displays the last card in the layout of p.
crdMgr.show(p, name); Panel p displays card with String name name in panel p.
GridBagLayout - Panel divided into rows&cols of possibly unequal size. Overall best, but difficult.
gbag = new GridBagLayout(); Creates GridBagLayout.
Must add with GridBagConstraints object (gbc below). Can (carefully) reuse same gbc object.
gbc = new GridBagConstraints(); Creates a new GridBagConsraints object.
 gbc.gridx = i; Column, counting from 0. Always start at 0. Don't use default (RELATIVE).
 gbc.gridy = i; Column, counting from 0. Always start at 0. Don't use default (RELATIVE)..
 gbc.gridwidth = i; Number of columns wide (default 1).
 gbc.gridheight = i; Number of rows high (default 1).
 gbc.weightx = d; Relative horizontal space to allocate if container expands. Default 0.0.
 gbc.weighty = d; Relative vertical space to allocate if container expands. Default 0.0.
 gbc.fill = fill; How to expand comp if space larger than preferred size. fill can be GridBagConstraints.NONE, GridBagConstraints.VERTICAL, GridBagConstraints.HORIZONTAL, GridBagConstraints.BOTH.
 gbc.anchor = anch; If component doesn't fill the space, tells how it should be aligned. anch can be one of these GridBagConstraints constants: CENTER (default), NORTH, SOUTH, NORTHEAST, SOUTHWEST, EAST, WEST, SOUTHEAST, NORTHWEST
 gbc.insets = ins; Space around component. Eg, gbc.insets = new Insets(10,5,5,10);
 gbc.ipadx = i; Horizontal padding internal to component. Can be negative!
 gbc.ipady = i; Vertical padding internal to component. Can be negative!

Copyleft 2005 Fred Swartz
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]