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 Notes


Layouts tell Java where to put components in containers (JPanel, content pane, etc). Every panel (and other container) has a default layout, but it's better to set the layout explicitly for clarity.

Create a new layout object (using one of its constructors) and use the container's setLayout method to set the layout. Each layout has its own way to resize components to fit the layout, and you must become familiar with these.

Tools. Creating your layouts by hand is simple for the simple layouts, but for really good layouts using the difficult GridBagLayout, you might want to use a program to help you. A good review of some of these programs is at Java GUI Builders (www.fullspan.com/articles/java-gui-builders.html) by Mitch Stuart.

General advice

  • Initial Iterations. To quickly get the first iterations of a program running, use FlowLayout. The result is often ugly, but if there are only a few components, it's quick. FlowLayout is rarely the correct layout to use in a finished program.
  • Simple programs. BorderLayout is often a good layout for simple programs. More complicated layouts can often be nesting BorderLayouts within other BorderLayouts. This and FlowLayout are the most important layouts for beginners.
  • Grids of equal sized elements (eg, a calculator keypad), can be created with with GridLayout.
  • Single Rows or colums of components are sometimes best done with BoxLayout.
  • More complicated layouts may require GridBagLayout. Getting a GridBagLayout to work correctly can be time-consuming, but it produces excellent results.

More notes

  • FlowLayout - left to right, top to bottom. Good for initial testing. FlowLayout does not respect preferred size information, so it may make variable size elements (eg, a graphics panel) extremely small.
  • BorderLayout - north, east, south, west, and center areas. Has various rules for how components are stretched to fit these areas. Both common and useful.
  • GridLayout - equal sized grid elements.
  • BoxLayout and Boxes - horizontal or vertical sequence of components.
  • GridBagLayout - unequal sized grid. Can produce excellent results, but can be difficult to use.
  • SpringLayout was added in Java 1.4, but is difficult for humans. It is rumored to be intended for authomatic layout programs..
  • CardLayout is good for something like a wizard interface which shows one of several possible layouts (think about a stack of index cards). Tabbed panes are something like a card layout with tabs.
  • The Null layout (absolute positioning) is possible, but is a bad idea.
  • Tabbed panes are something like a card layout with tabs. They are not implemented as a layout, but as a type of panel.
  • Non-standard layout managers. Because of deficiencies in the standard Java layout managers, several good, free, alternatives exist.
  • You can even define your own layout manager, but this isn't a trivial amount of work. Sun's How to Build a Layout Manager article, java.sun.com/developer/technicalArticles/Interviews/LayoutMgr/, gives a description of the process, altho it is rather unclearly written.


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]