Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2008, 11:20 PM
Member
 
Join Date: Jan 2008
Posts: 14
Rep Power: 0
dim_ath is on a distinguished road
Question 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
  #2 (permalink)  
Old 01-09-2008, 12:37 AM
Member
 
Join Date: Jan 2008
Posts: 1
Rep Power: 0
lord_visionz is on a distinguished road
Default
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
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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
Rep Power: 0
dim_ath is on a distinguished road
Default
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
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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
Rep Power: 0
undertow is on a distinguished road
Send a message via AIM to undertow Send a message via Skype™ to undertow
Default
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
Rep Power: 0
Mark_Petrov is on a distinguished road
Smile
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
Reply

Bookmarks

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alignment Issue... chanduseec JavaServer Faces 1 09-30-2008 07:47 AM
[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 of textfield in awt nitinborge5 New To Java 2 07-30-2007 12:16 PM


All times are GMT +2. The time now is 04:52 AM.



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