Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Summary - GUI Layouts

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share

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

   p.setLayout(layo);

Layouts (FlowLayout, BorderLayout, GridLayout, BoxLayout, CardLayout)

FlowLayout - Arranges widgets left-to-right, top-to-bottom.
flow = new FlowLayout(); Creates FlowLayout, centered with 5 pixel gaps.
flow = new FlowLayout(align); Specifies alignment (FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT).
flow = new FlowLayout(align, h, v); Specifies alignment and horizontal (h) and vertical (v) gaps in pixels.
p.add(widget); Adds widget to the next left-to-right, top-to-bottom position.
BorderLayout - Lays out components in BorderLayout.NORTH, EAST, SOUTH, WEST, and CENTER sections.
bord = new BorderLayout(); Creates BorderLayout. Widgets added with constraint to tell where.
bord = new BorderLayout(h, v); Creates BorderLayout with horizonal and vertical gaps sizes in pixels.
p.add(widget, pos); Adds widget to one of the 5 border layout regions, pos (see list above).
GridLayout - Lays out components in equal sized rectangular grid, added r-t-l, top-to-bottom.
grid = new GridLayout(r, c); Creates GridLayout with specified rows and columns.
grid = new GridLayout(r,c,h,v); As above but also specifies horizontal and vertical space between cells.
p.add(widget); Adds widget to the next left-to-right, top-to-bottom cell.
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 Fred Swartz

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy