Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do you layout these components centered

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do you layout these components centered

    Here is my program, its like a 3 number hi-low game.



    I am using for the top, middle, and bottom rows. Then inside those panels I use gridlayout to layout the objects. I want to know if instead of them being in their respective grids on the left hand side, if they could be centered inside of their grid?

    I tried using setAlignmentX and that didn't work. I don't really know how to go about it. I'll post a snippet of my code that is important.

    //middle panel setup
            JPanel middle = new JPanel();
            middle.setLayout(new GridLayout(1, 3));
     
            higher = new JButton("Higher");
            higher.setAlignmentX(CENTER_ALIGNMENT);
            middle.add(higher);
     
            or = new JLabel("OR");
            or.setAlignmentX(CENTER_ALIGNMENT);
            middle.add(or);
     
            lower = new JButton("Lower");
            lower.setAlignmentX(CENTER_ALIGNMENT);
            middle.add(lower);
            pane.add(middle);

    EDIT: I drew what I want it to be like in paint...

    Last edited by robertbob; May 24th, 2010 at 08:26 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do you layout these components centered

    You could combine it with BoxLayout and utilize horizontal and vertical glues for the alignment. In your case, you could use this layout inside the GridLayout, perhaps create a new type of JPanel
    public class HorizontallyCenteredJPanel extends JPanel{
        public HorizontallyCenteredJPanel(Component c){
            super();
            add(Box.createHorizontalGlue());
            add(c);
            add(Box.createHorizontalGlue());
        }
    }
    Or something like this....then just add your components to this, and in turn add it to your GridLayout JPanel.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How do you layout these components centered

    Another way is to add each component to a JPanel with a GridBagLayout WITHOUT using a GridBagConstraints, which will center the component in the JPanel at its preferredSize, and then add these JPanels to your original GridLayout.

    db

    edit But a more suitable approach might be to just use a GridBagLayout with a GridBagConstraints set to fill=NONE and anchor=CENTER. Of course, if the individual components don't have the same preferred size, this could lead to an uneven grid spacing.

Similar Threads

  1. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM
  2. Components that interact with eachother
    By Flamespewer in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2010, 09:27 PM
  3. Re-Initialization of FrameView app components??
    By DarkJoy in forum Java Networking
    Replies: 2
    Last Post: November 13th, 2009, 01:19 AM
  4. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM
  5. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM