Results 1 to 5 of 5

Thread: How to use BoxLayout in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    221

    How to use BoxLayout in Java?

    Hi folks,
    You guys are really cool in explaining the things. So I thought that instead of doing the searching on web and wasting the time, it would be better if I post here. I have just completed about the layout manager in Java. Now I am told to use the BoxLayout, which I don't know how to do..!! So please help me by telling how to use BoxLayout in Java.?? Hope that you will have me sooner.!! Thanks in Advance.
    AMD Sempron 2800+ @ 2Ghz
    Asus A7V8X-LA
    120Gb Seagate barracuda 7200Rpm Ultra-ATA 100
    Elixir 512mb DDR Pc3200 (Soon 1Gb)
    Club 3D Radeon 9600 256Mb
    Lite-On Cd & Dvd writer combo
    IDE-Dvd drive
    400w Psu
    Windows Xp Pro Sp2
    Advent Wireless Mouse & Keyboard.

  2. #2
    Join Date
    Jul 2006
    Posts
    442

    Re: How to use BoxLayout in Java?

    A layout manager that allows multiple components to be laid out either vertically or horizontally. The Swing packages include a general purpose layout manager named BoxLayout. According to your choice, a BoxLayout either stacks its components on top of each other or places them in a row. The components will not wrap. The BoxLayout manager is constructed with an axis parameter that specifies the type of layout that will be done. There are four types from which you can choose one :
    1. X_AXIS - In this type, the components are laid out horizontally from left to right.
    2. Y_AXIS - If you choose this choice, then the arrangement of the components would be vertically from top to bottom.
    3. LINE_AXIS - In this type components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property.
    "When they give you ruled paper, write the other way..." J.R.J.

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: How to use BoxLayout in Java?

    I have provided you the full coding of the BoxLayout, which is described as follows :
    Code:
    import java.awt.Component;
    import java.awt.Container;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    
    public class BoxLayoutTrial {
        public static void addComponentsToPane(Container pane) {
            pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    
            addAButton("Button 1", pane);
            addAButton("Button 2", pane);
            addAButton("Button 3", pane);
            addAButton("Long-Named Button 4", pane);
            addAButton("Button 5", pane);
        }
    
        private static void addAButton(String text, Container container) {
            JButton button = new JButton(text);
            button.setAlignmentX(Component.CENTER_ALIGNMENT);
            container.add(button);
        }
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("BoxLayoutTrial");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            addComponentsToPane(frame.getContentPane());
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: How to use BoxLayout in Java?

    I have given you the coding which lays out the GUI. This code is in the constructor for the dialog, which is implemented as a JDialog subclass. So have a look at the sample of code :
    Code:
    JScrollPane listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(320, 110));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);
    ...
    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
    JLabel label = new JLabel(labelText);
    ...
    listPane.add(label);
    listPane.add(Box.createRigidArea(new Dimension(0,7)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(cancelButton);
    buttonPane.add(Box.createRigidArea(new Dimension(20, 0)));
    buttonPane.add(setButton);
    
    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

  5. #5
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use BoxLayout in Java?

    For Creating BoxLayout Objects, you will need some methods. The following are the methods that are used to do that :
    • BoxLayout(Container, int) - This method creates a BoxLayout instance that controls the specified Container.
    • Box(int) - This method creates a Box which is just like a container that uses a BoxLayout with the specified axis.
    • static Box createHorizontalBox() - This method creates a Box that lays out its components from left to right.
    • static Box createVerticalBox() - This method creates a Box that lays out its components from top to bottom.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Setting Of Java to The Point At Manual Java
    By winni in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 10:05 PM
  3. Java Programming using Adventnet SNMP Java API
    By ROCKING_Suhas in forum Software Development
    Replies: 5
    Last Post: 17-07-2010, 06:52 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,419,728.49689 seconds with 17 queries