Results 1 to 3 of 3
  1. #1
    AlwaysJava is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default [SOLVED] Two centered buttons with right justified button

    Hi, I'm new to the forum.

    I've done some work with layout managers, but I've run into a situation that I can't figure out. I have a dialog that has an OK and Cancel button centered at the bottom. Now, in certain cases, I want to add a third button that is on the same row as the other buttons but is right justified. Everything that I've tried throws off the alignment of the two buttons. I'm not sure what combination of layout managers to use.

    (It wouldn't surprise me if this question has been asked before, but I'm not sure what to search for to find an answer.)

    Thank you in advance for your help.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Which Layout do you use?Can you show the code please?

  3. #3
    AlwaysJava is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    It figures that as soon as I stepped away from it, I would figure it out. I was making it more complicated in my head than it needed to be. I'm using a BoxLayout for the row of buttons. I needed to create a rigid area the size of the 3rd button to compensate for the space being used on the right. Then I created horizontal glue between the rigid area and the OK button and between the Cancel button and the 3rd button. There's a quick example below. Please feel free to tell me if I'm not doing it the correct way. Thank you for responding so quickly.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class TestButtons extends JFrame{
    
    	public TestButtons(){
    	
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		JPanel contentPane = (JPanel) getContentPane();
    		
    		//Add a text area to fill up the center of the frame.
    		JTextArea myArea = new JTextArea();
    		contentPane.add(myArea, BorderLayout.CENTER);
    		
    		//Create a panel for the buttons.
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
    		contentPane.add(buttonPanel, BorderLayout.PAGE_END);
    		
    		//Create the About button.
    		JButton buttonAbout = new JButton("About...");
    		
    		//Create a rigid area the size of the About button.
    		buttonPanel.add(Box.createRigidArea(buttonAbout.getPreferredSize()));
    		
    		//Add glue between the rigid area and the OK button.
    		buttonPanel.add(Box.createHorizontalGlue());
    		
    		//Add the OK button.
    		JButton buttonOK = new JButton("OK");
    		buttonPanel.add(buttonOK);
    		
    		//Add a spacer between the OK and Cancel buttons.
    		buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));
    		
    		//Add the Cancel button.
    		JButton buttonCancel = new JButton("Cancel");
    		buttonPanel.add(buttonCancel);
    		
    		//Add glue between the Cancel button and the About button.
    		buttonPanel.add(Box.createHorizontalGlue());
    		
    		//Add the About button.
    		buttonPanel.add(buttonAbout);
    		
    		setSize(400, 400);
    		validate();
    		setVisible(true);
    		
    	}
    
    	public static void main(String[] args){
    		new TestButtons();
    	}
    
    }

Similar Threads

  1. GUI buttons question...
    By glenrowan in forum New To Java
    Replies: 3
    Last Post: 01-18-2011, 05:23 PM
  2. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 09:15 PM
  3. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  4. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 01:23 AM
  5. Next, Finish Buttons !!!
    By pele in forum SWT / JFace
    Replies: 1
    Last Post: 07-14-2007, 05:22 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
  •