Results 1 to 7 of 7
  1. #1
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    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 );

  2. #2
    lord_visionz is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    You can use grid layout and set the number of colums as 1. That should do the trick.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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 );
        }
    }

  4. #4
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    if i use GridLayout they aligned vertically but textField and Button cover
    all the window something i dont like.

    anything thought?

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    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:
    Java Code:
    JPanel p = new JPanel();
    p.add(getTextField());
    panel.add(p);
    p = new JPanel();
    p.add(getButton());
    panel.add(p);

  6. #6
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    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.

  7. #7
    Mark_Petrov is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Smile

    instead of:
    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

Similar Threads

  1. Alignment Issue...
    By chanduseec in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 09-30-2008, 06:47 AM
  2. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 01:23 PM
  3. Layout Managers
    By gmioannou in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 04:12 AM
  4. alignment of textfield in awt
    By nitinborge5 in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •