Results 1 to 5 of 5
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default buttons won't center

    Please have a look at my Quit JFrame:



    Notice that the buttons are pushed to the left. Why is this?

    The QuitFrame extends my TwoButtonFrame class.

    Here's QuitFrame's constructor:

    Java Code:
        public QuitFrame() {
    
    	super("Quit?", "Are you sure you want to quit?", "Yes", "No");
    	setLocation(QUIT_X, QUIT_Y);
        }
    And TwoButtonFrame:

    Java Code:
        public TwoButtonFrame(String titleString, String labelString,
    		          String leftButtonString, String rightButtonString) {
    
    	setTitle(titleString);
    
    	// init left button
    	leftButton = new JButton(leftButtonString);
    	leftButton.addActionListener(this);
    
    	// init right button
    	rightButton = new JButton(rightButtonString);
    	rightButton.addActionListener(this);
    	
    	// add frame components using a GridBagLayout with constraints
    	Container container = getContentPane();
    	container.setLayout(new GridBagLayout());
    
    	GridBagConstraints constraints = new GridBagConstraints();
    	constraints.insets = new Insets(5, 5, 5, 5);
    
    	constraints.gridx = 0;
    	constraints.gridy = 0;
    	constraints.gridwidth = 2;
    	container.add(new JLabel(labelString), constraints);
    
    	constraints.gridwidth = 1;
    	constraints.gridy = 1;
    	constraints.anchor = GridBagConstraints.CENTER; // trying to center buttons here
    	container.add(leftButton, constraints);
    
    	constraints.gridx = 1;
    	container.add(rightButton, constraints);
    
    	// add itself as its own WindowListener
    	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    	addWindowListener(this);
    
    	setResizable(false);
    	setVisible(false);
    	pack();
        }
    I added the line constraints.anchor = GridBagConstraints.CENTER; to try and enforce centering but it doesn't seem to have an effect (and it's supposed to be CENTER by default anyway).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Get rid of the GridBagLayout and instead nest layouts (as has been mentioned before). One possible consideration is to have the whole JPanel use BorderLayout, and then add the JLabel BorderLayout.NORTH. Then use another JPanel that uses GridLayout(1, 0, x, 0) where x is whatever number works best and add the JButtons to that. Then add the GridLayout using JPanel to the main BorderLayout-using JPanel BorderLayout.CENTER. You may wish to also use an EmptyBorder to provide gaps for the border of the gui.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I'm guessing there may be a genuine reason behind doing what you have, but given the functionality coded, why have you not used a confirmation dialog instead?

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks Fubarable, your suggestions worked perfectly (as always).

    Quote Originally Posted by Ronin
    I'm guessing there may be a genuine reason behind doing what you have, but given the functionality coded, why have you not used a confirmation dialog instead?
    Uh... because I didn't know about a confirmation dialog. :o

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gib65 View Post
    Thanks Fubarable, your suggestions worked perfectly (as always).
    You're welcome.

    Uh... because I didn't know about a confirmation dialog. :o
    I think he's talking about using one of the several available JOptionPanes, and on re-thinking your problem, I think that he's absolutely right. Your dialog would probably be best done using one of these guys.

Similar Threads

  1. Media Center
    By udalcse in forum Jobs Offered
    Replies: 0
    Last Post: 10-27-2009, 01:35 PM
  2. jsf center tag needed
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 5
    Last Post: 07-27-2009, 11:49 AM
  3. Center background image
    By Floetic in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:50 PM
  4. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:47 PM
  5. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 03:43 PM

Posting Permissions

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