advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement



Title: Professional Java Programming
ISBN: 186100382x
US Price: $ 59.99
Canadian Price:
C$ 89.95
UK Price: £ 45.99
© Wrox Press Limited, US and UK.

Reviews : Java Books :
Professional Java Programming : Using Layout Managers

Guidelines for Using Layout Managers

Now that we've examined the advantages and disadvantages of the layout managers included with Java, it's appropriate to discuss some more general topics related to how to use layout managers.

Combining Layout Managers

In the previous discussions of layout managers, each one was treated independently of the other, but it's common practice for a user interface to use multiple layout managers. In fact, you'll often find it necessary or desirable to create a container that uses one type of layout manager and add child containers to that parent which use different types of layout managers. For example, suppose that you want to create a user interface like the one shown below. In this case, the component at the top is displayed using its preferred height and fills the width of the container. In addition, a row of buttons that are equal in size occupies the bottom, and a component in the center fills the remaining area.

To some extent, BorderLayout provides the functionality needed to create this component, but you cannot use it directly to create the bottom row of buttons. That's because BorderLayout allows only a single component to be added to a location, such as the SOUTH portion of its container. This problem can be resolved by adding the two buttons to a container such as a JPanel and adding that panel to the parent managed by a BorderLayout. Since the buttons should be given the same size, GridLayout is the obvious choice for the container that the buttons will be added to, and the code to implement this is shown below:

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

public class Embedded extends JFrame {
 
	public static void main(String[] args) {
		Embedded e = new Embedded();
		e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		e.setSize(400, 300);
		e.setVisible(true);
 
	}
 
	public Embedded() {
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		pane.add(getHeader(), BorderLayout.NORTH);
		pane.add(getTextArea(), BorderLayout.CENTER);
		pane.add(getButtonPanel(), BorderLayout.SOUTH);
	}
 
	protected JComponent getHeader() {
 		JLabel label = new JLabel("Embedded Layout Manager Test",
 					JLabel.CENTER);
		label.setFont(new Font("Courier", Font.BOLD, 24));
		return label;
	}
 
	protected JComponent getTextArea() {
 		return new JTextArea(10, 10);
 	}
 
	protected JComponent getButtonPanel() {
	 	JPanel inner = new JPanel();
		inner.setLayout(new GridLayout(1, 2, 10, 0));
		inner.add(new JButton("Ok"));
		inner.add(new JButton("Cancel"));
		return inner;
	}
}

As shown below, this code doesn't quite achieve the desired results, since the buttons have been stretched to fill the width of the container:

That's because the buttons' parent container is stretched by the BorderLayout so that its width is equal to the width of the frame, and that in turn causes the GridLayout to stretch the buttons to fill their parent container. To fix this problem, it's necessary to put the panel managed by the GridLayout into another container that won't stretch it. Since FlowLayout always displays components using their preferred size, we can use it to provide this behavior, so we'll define an additional FlowLayout-managed JPanel, add the button panel to it, and add the button panel to the content pane:

 
protected JComponent getButtonPanel() {
	JPanel inner = new JPanel();
	inner.setLayout(new GridLayout(1, 2, 10, 0));
	inner.add(new JButton("Ok"));
	inner.add(new JButton("Cancel"));
	JPanel outer = new JPanel();
	outer.setLayout(new FlowLayout());
	outer.add(inner);
	return outer;
	// return inner;
}

Finally, running this modified code produces the desired interface that was illustrated at the start of this section.

You'll often find it necessary to embed containers within other containers and to use different layout managers when doing so. If you're creating a complex user interface, it's often helpful to conceptually break the interface down into smaller, simpler portions that can be created using the existing layout managers. Those smaller pieces can then be created and combined into the large, complex interface instead of trying to produce the desired results with a single layout manager.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs