Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2008, 11:20 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
what layout to use for vertical alignment?
Hello everyone! i d like your help on the following:
i d like the label, textfield and button (panel) to be appeared the one
under the other and not all in the same line.
what layout manager need to use?
Thanks!


panel=new JPanel();
panel2=new JPanel();
label=new JLabel("Choose a number from 1 to 10:");
enterField = new JTextField(8);
button=new JButton("Send");
enterField.setEnabled( false );

panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(enterField);
panel.add(button);

displayArea = new JTextArea(10,25);
displayArea.setEnabled( false );
scroller = new JScrollPane( displayArea );
panel2.add(scroller);


Container container = getContentPane();
container.add( panel, BorderLayout.NORTH );
container.add( panel2, BorderLayout.SOUTH );

//setSize(400,250);
pack( );
setVisible( true );
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-09-2008, 12:37 AM
Member
 
Join Date: Jan 2008
Posts: 1
lord_visionz is on a distinguished road
You can use grid layout and set the number of colums as 1. That should do the trick.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-09-2008, 03:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; public class SomeLayouts { private JTabbedPane getContent() { JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("GridLayout", getGridTab()); tabbedPane.addTab("GridBag 1", getGridBagTab1()); tabbedPane.addTab("GridBag 2", getGridBagTab2()); return tabbedPane; } private JPanel getGridTab() { JPanel panel = new JPanel(new GridLayout(0,1)); panel.add(getLabel()); JPanel p = new JPanel(); p.add(getTextField()); panel.add(p); p = new JPanel(); p.add(getButton()); panel.add(p); return addToParent(panel); } private JPanel getGridBagTab1() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.gridwidth = GridBagConstraints.REMAINDER; panel.add(getLabel(), gbc); panel.add(getTextField(), gbc); panel.add(getButton(), gbc); return addToParent(panel); } private JPanel getGridBagTab2() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 1.0; gbc.anchor = GridBagConstraints.WEST; panel.add(getLabel(), gbc); gbc.anchor = GridBagConstraints.CENTER; panel.add(getTextField(), gbc); panel.add(getButton(), gbc); return addToParent(panel); } private JPanel addToParent(JPanel north) { JTextArea displayArea = new JTextArea(10,25); displayArea.setEnabled( false ); JPanel panel = new JPanel(new BorderLayout()); panel.add(north, "North"); panel.add(new JScrollPane( displayArea )); return panel; } private JLabel getLabel() { return new JLabel("Choose a number from 1 to 10:"); } private JTextField getTextField() { JTextField enterField = new JTextField(8); enterField.setEnabled( false ); return enterField; } private JButton getButton() { return new JButton("Send"); } public static void main(String[] args) { SomeLayouts test = new SomeLayouts(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = f.getContentPane(); container.add( test.getContent() ); // setSize(400,250); f.pack( ); f.setLocation(200,200); f.setVisible( true ); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-09-2008, 10:01 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
if i use GridLayout they aligned vertically but textField and Button cover
all the window something i dont like.

anything thought?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 01:51 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
textField and Button cover all the window
Add each one to a panel - the panel will show the component at its preferredSize and will itself expand to fill the grid cell. This was done in the getGridTab method of SomeLayouts:
Code:
JPanel p = new JPanel(); p.add(getTextField()); panel.add(p); p = new JPanel(); p.add(getButton()); panel.add(p);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-15-2008, 09:02 PM
undertow's Avatar
Member
 
Join Date: Jan 2008
Location: Colorado USA
Posts: 12
undertow is on a distinguished road
Send a message via AIM to undertow Send a message via Skype™ to undertow
If i get what you are looking for i would suggest nesting your related components into a jpanel; lay those related components for example a label and a text box. make several panels that hold the label and textbox and put those panels in a container panel with the BoxLayout.

The box layout manager is cool in that it respects all the prefered minimum and max sizes. With the Box layout you can stack components in what ever fashion you would want. just my 2-cents though.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-20-2008, 06:28 PM
Member
 
Join Date: Jan 2008
Posts: 3
Mark_Petrov is on a distinguished road
instead of:
Quote:
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
try using:

panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

with PAGE_AXIS, your components can be aligned vertically
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] alignment problem nanimtech JavaServer Pages (JSP) and JSTL 1 04-10-2008 02:23 PM
SWT Layout JavaForums Java Blogs 0 12-31-2007 05:53 PM
Layout Managers gmioannou AWT / Swing 1 12-24-2007 05:12 AM
Alignment Issue... chanduseec JavaServer Faces 0 08-13-2007 02:04 PM
alignment of textfield in awt nitinborge5 New To Java 2 07-30-2007 12:16 PM


All times are GMT +3. The time now is 02:34 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org