Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By doWhile

Thread: Using the Box class

  1. #1
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Using the Box class

    I'm using the Box class for one of my applets to organize my buttons.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class conceptstemplate extends JApplet
    {
        Box sidebar = new Box (BoxLayout.LINE_AXIS);
        
        JButton next = new JButton ("Next >");
        JButton previous = new JButton ("< Previous");
        JButton mmenu = new JButton ("Main Menu");
        
        public void init ()
        {
            setSize (1000, 600);
            setBackground (Color.lightGray);
            
            
            //sidebar.add (Box.createRigidArea (new Dimension (50, 450)));
            //sidebar.add (Box.createVerticalStrut (450));
            //sidebar.setBounds (0, 450, getWidth (), 150);
            //sidebar.add (Box.createHorizontalGlue ());
            
            sidebar.add (Box.createHorizontalStrut (50));
            sidebar.add (mmenu);
            sidebar.add (Box.createHorizontalStrut (600));
            sidebar.add (previous);
            sidebar.add (Box.createHorizontalStrut (50));
            sidebar.add (next);
            
            add (sidebar);
        }
    }
    The problem I'm having is getting the buttons to shift downward to the bottom of the window. The code with // behind it shows the methods I've tried using and that haven't worked. Is anyone able to help me?
    Last edited by Neoco; 05-20-2012 at 12:46 AM.

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

    Default Re: Using the Box class

    To put things on the bottom of a container, I usually use the BorderLayout, and place my bottom-most component BorderLayout.PAGE_END. If I have a bunch of buttons to put at the bottom, I put them in a JPanel, say called buttonPanel, and then place that JPanel on the bottom of the container.

    Please edit your post above, and change your [quote] [/quote] tags to [code] [/code] tags.

  3. #3
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Using the Box class

    If there is a way to shift the box or the buttons downward, that would be fine.

    I would use BorderLayout, except that I am using the Box class and the Box class can only use the BoxLayout. If I were to use another container, I'd be severely compromising my summative

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

    Default Re: Using the Box class

    Quote Originally Posted by Neoco View Post
    If there is a way to shift the box or the buttons downward, that would be fine.
    OK, one way is as described above.


    I would use BorderLayout, except that I am using the Box class and the Box class can only use the BoxLayout. If I were to use another container, I'd be severely compromising my summative
    You are using Box class as a tool to get the gui design you desire, but you shouldn't be wed to the tool. Please show an image of what gui you're trying to achieve, and then an image of the undesirable gui that your current code creates right now.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Using the Box class

    I would use BorderLayout, except that I am using the Box class and the Box class can only use the BoxLayout. If I were to use another container, I'd be severely compromising my summative
    Based upon the above it seems you aren't familiar with nesting layout managers - placing the JPanel with the BoxLayout (your button panel) inside a JPanel with a BorderLayout as Fubarable described. Alternatively, you can create a Box or JPanel with a BoxLayout and set it to vertical - adding a vertical glue before adding the button parent Component
    Fubarable and Neoco like this.

  6. #6
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Using the Box class

    Hey I managed to find the solution on my own actually

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class conceptstemplate extends JApplet
    {
    	Box sidebar = new Box (BoxLayout.LINE_AXIS);
    	
    	JButton next = new JButton ("Next >");
    	JButton previous = new JButton ("< Previous");
    	JButton mmenu = new JButton ("Main Menu");
    	
    	public void init ()
    	{
    		setSize (1000, 600);
    		setBackground (Color.lightGray);
    		
    		mmenu.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		previous.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		next.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		
    		sidebar.add (Box.createHorizontalStrut (50));
    		sidebar.add (mmenu);
    		sidebar.add (Box.createHorizontalGlue ());
    		sidebar.add (previous);
    		sidebar.add (Box.createHorizontalStrut (50));
    		sidebar.add (next);
    		sidebar.add (Box.createHorizontalStrut (50));
    		
    		add (sidebar);
    	}
    }
    All I had to do was set the proper alignment for each button.

    Quote Originally Posted by doWhile View Post
    Alternatively, you can create a Box or JPanel with a BoxLayout and set it to vertical - adding a vertical glue before adding the button parent Component
    I might end up doing this to make it look better. Thanks for the help though!. I'll post here again if I have anymore problems

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  3. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  4. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 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
  •