Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

LayoutUtils


The LayoutUtils class propose some utilities for Swing Layouts which provide greedy Box fillers to use with the BoxLayout:

Example

The following code will create two rows with a huge space between the rows:
   JFrame f = new JFrame("test");

   Container pane = f.getContentPane();
   pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

   JPanel xpane = new JPanel();
   pane.add(xpane);
   xpane.setLayout(new BoxLayout(xpane, BoxLayout.X_AXIS));
   xpane.add(Box.createHorizontalStrut(5));
   xpane.add(new JFileSelector());
   xpane.add(Box.createHorizontalStrut(5));
   xpane.add(Box.createHorizontalGlue());

   pane.add(Box.createVerticalStrut(5));
   xpane = new JPanel();
   pane.add(xpane);
   xpane.setLayout(new BoxLayout(xpane, BoxLayout.X_AXIS));
   xpane.add(Box.createHorizontalStrut(5));
   xpane.add(new JButton("Click Me!"));
   xpane.add(Box.createHorizontalGlue());
   pane.add(Box.createVerticalGlue());

   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.setSize(400, 300);
   f.setVisible(true);

layoututils
With the following code:
      JFrame f = new JFrame("test");

      Container pane = f.getContentPane();
      pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

      JPanel xpane = new JPanel();
      pane.add(xpane);
      xpane.setLayout(new BoxLayout(xpane, BoxLayout.X_AXIS));
      xpane.add(Box.createHorizontalStrut(5));
      xpane.add(new JFileSelector());
      xpane.add(Box.createHorizontalStrut(5));
      xpane.add(Box.createHorizontalGlue());

      pane.add(Box.createVerticalStrut(5));
      xpane = new JPanel();
      pane.add(xpane);
      xpane.setLayout(new BoxLayout(xpane, BoxLayout.X_AXIS));
      xpane.add(Box.createHorizontalStrut(5));
      xpane.add(new JButton("Click Me!"));
      xpane.add(Box.createHorizontalGlue());
      pane.add(LayoutUtils.createGreedyVerticalGlue());

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(400, 300);
      f.setVisible(true);
We have the following result:
layoututils2

Categories: packages | swing

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences