Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 22nd, 2005, 01:47 PM
Registered User
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding two components to a BoxLayout

I am using a swing JPanel, with the standard flow layout. I have 3 components in the panel; JLabel, JTextField and JButton.
I want to have it so I have the JLabel on its own line, then the JTextField and JButton next to each other on the next line.

A BorderLayout does not work because it seems you cant have two components in the same (CENTER) area.
A box layout does not work because it puts all 3 on a separate lines.

Is a GridBagLayout the only way to achieve this?


Old May 28th, 2005, 02:56 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

see other answer

Don't Stand on your head - you'll get footprints in your hair
Old June 24th, 2005, 06:27 PM
Authorized User
Join Date: Jul 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths

This is how I'd do it, but it may not be the ideal way -- I have a background in postscript and html coding, so i tend to build things up using tables (in this case i'm going to use a box to accomplish the same thing) as a way of formatting. I find this to be pretty flexible:

Box hbLine1 = Box.createHorizontalBox();
Box hbLine2 = Box.createHorizontalBox();
Box vBox = Box.createVerticalBox();

hbLine1.add(new JLabel("blah"));
hbLine2.add(new JTextField());
hbLine2.add(new JButton("Click Me"));


 Now your vBox contains the configuration of controls in the way you want it ... just by adding your vBox, regardless of layout, the components will be placed like you describe, so you can use whatever layout makes since for you -- for example you could add the vBox to the CENTER of the BorderLayout

Hope That Helps

Meredith Shaebanyan

Similar Threads
Thread Thread Starter Forum Replies Last Post
Existing Components Thami SQL Server 2005 0 August 8th, 2008 02:51 AM
charting components? jhughes96 Visual Studio 2005 1 April 3rd, 2006 12:00 PM
Accesing COM+ components from c# txerra General .NET 2 June 24th, 2005 05:23 AM
How to write the COM components in C# stonewall C# 2 June 22nd, 2005 08:19 PM
Accesing Com+ components from c# txerra C# 1 June 22nd, 2005 06:10 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.