Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old May 22nd, 2005, 02:48 PM
Registered User
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default JPanel components on same line

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?

Reply With Quote
  #2 (permalink)  
Old May 28th, 2005, 03:53 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Put the JTextField and JButton into another JPanel and add the (sub)JPanel and the JButton to the main JPanel?

Don't Stand on your head - you'll get footprints in your hair
Reply With Quote
  #3 (permalink)  
Old September 1st, 2005, 07:18 PM
Authorized User
Join Date: Jul 2003
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths

disclaimer, i didn't check any of the syntax, so just use this as a concept -- dont try to copy and paste the code:

Box hBox = Box.createHorizontalBox();
Box vBox = Box.createVerticalBox();

hBox.add(new JTextField("blah"));
hBox.add (new JButton("Click Me"));

vBox.add(new JLabel("enter Blah"));

you get something like

| 1 | <--- vertical box
| |
|-------- |
| | |
| 2 | 3 | <--- horizontal box nested in vBox

ciderpunx suggestion works too -- I like to do it this way so I don't have to create a lot of other classes. But I have some background in web stuff / using tables for layout so I'm used to thinking in that way -- if you're not, that solution may seem a bit stilted and hard to use to you; in that case, go with the sub-JPanels.

Meredith Shaebanyan

Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to draw indicator line in owc line chart AlexOo General .NET 0 July 9th, 2007 11:32 PM
How to paste a bitmap into a JPanel? AndrewH Java GUI 1 May 1st, 2007 01:19 AM
How to read file line by line in EVC++ iriskab Visual C++ 0 September 27th, 2006 02:39 PM
Reading line by line from a .txt file x_ray VB.NET 2002/2003 Basics 5 February 10th, 2006 01:55 PM
Can I add a JComboBox twice to the same jpanel david.byrne Java GUI 4 May 14th, 2005 12:42 PM

All times are GMT -4. The time now is 01:17 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.