Results 1 to 2 of 2
  1. #1
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default Box panel won't go vertical

    Hi,
    This is on a JLayeredPane with many other components on it, but out in clear space on the right. I try making a JPanel with a BoxLayout and some buttons in it that will lay out vertically. So far, I haven't gotten it to happen. I have it trimmed down to the following code sample. The buttons and panel are being added to 'pane', which is a JLayeredPane. The buttons come out in a horizontal string (no matter what I have tried). I have tried
    buttonPanel.validate(); and buttonPanel.setAlignmentX(Component.CENTER_ALIGNME NT);
    and setSize() and setAlignmentX() on each of the buttons, to no avail. I would love to get these buttons to display vertically!
    I have also tried PAGE_AXIS.

    Cheers - Mark

    Java Code:
    		JPanel buttonPanel = new JPanel();
    		BoxLayout boxLayout = new BoxLayout(buttonPanel, BoxLayout.Y_AXIS);
    		JButton hi = new JButton("Hi");
    		JButton hello = new JButton("Hello");
    		JButton howdy = new JButton("Howdy");
    		buttonPanel.add(hi);
    		buttonPanel.add(hello);
    		buttonPanel.add(howdy);
    		buttonPanel.setLocation(boardWidth+20, margin+200);
    		buttonPanel.setSize(buttonPanel.getPreferredSize());
    		pane.add(buttonPanel);
    		return;
    Last edited by Markgm; 01-02-2011 at 07:42 PM.

  2. #2
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    I got it working due to the necessary line with the setLayout() call. Gee wiz.

    Java Code:
    		JPanel buttonPanel = new JPanel();
    		BoxLayout boxLayout = new BoxLayout(buttonPanel, BoxLayout.Y_AXIS);
    		buttonPanel.setLayout(boxLayout);
    		JButton hi = new JButton("Hi");
    		JButton hello = new JButton("Hello");
    		JButton howdy = new JButton("Howdy");
    		hi.setAlignmentX(Component.CENTER_ALIGNMENT);
    		hello.setAlignmentX(Component.CENTER_ALIGNMENT);
    		howdy.setAlignmentX(Component.CENTER_ALIGNMENT);
    		buttonPanel.add(hi);
    		buttonPanel.add(hello);
    		buttonPanel.add(howdy);
    		buttonPanel.setLocation(boardWidth+20, margin+200);
    		buttonPanel.setSize(buttonPanel.getPreferredSize());
    		pane.add(buttonPanel);
    		return;

Similar Threads

  1. Vertical menu
    By Kligham in forum New To Java
    Replies: 13
    Last Post: Yesterday, 02:07 PM
  2. How to make buttons vertical
    By ninjalord918 in forum AWT / Swing
    Replies: 2
    Last Post: 07-29-2010, 09:10 AM
  3. vertical sync
    By afraidofdark in forum Java 2D
    Replies: 0
    Last Post: 03-25-2010, 08:54 PM
  4. Collapsible Vertical Menu
    By ccvid in forum Java Applets
    Replies: 4
    Last Post: 01-27-2009, 05:40 AM
  5. Replies: 0
    Last Post: 07-02-2008, 08:06 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
  •