Results 1 to 3 of 3
  1. #1
    Zamereon is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Alignment in GridLayout

    I have a few nested panels and I want to center align the contents of the cells.
    An example of what I have is:

    Java Code:
    Container witIncrementPane = new Panel();
    witIncrementPane.setLayout(new GridLayout (1,3,5,1));
    final JButton witUpButton = new JButton(upArrow);
    witUpButton.setBackground(Color.white);
    final JButton witDownButton = new JButton(downArrow);
    witDownButton.setBackground(Color.white);
    witIncrementPane.add(witUpButton);
    witIncrementPane.add(witIncrementLabel);
    witIncrementPane.add(witDownButton);
    I want the buttons and label to all be centered.

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    (I think) In a gridlayout, buttons will take up the entire space of the cell they are in therefore making it impossible to center them. To center text of a JLabel:
    Java Code:
    JLabel label = new JLabel("text", JLabel.CENTER);

  3. #3
    AndyV is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    You could use greater vgap and hgap values, with GridLayout.

    Or, use a Box with combination of GridLayout(1,1). I've used this trick:
    Java Code:
    	Box buttonBox = Box.createHorizontalBox();
    	buttonBox.createHorizontalGlue();
    	buttonBox.add(cancelButton);
    	buttonBox.add(new JPanel()); // dummy
    	buttonBox.add(backButton);
    	buttonBox.add(nextButton);
    	container.add(buttonBox);

Similar Threads

  1. help! Gridlayout
    By eiramae in forum Java Applets
    Replies: 3
    Last Post: 02-19-2011, 05:11 AM
  2. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 07:54 PM
  3. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  4. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 10:16 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 11:48 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
  •