Java GUI’s Using the Swing API 3

Layout Manager:-

  • A layout manager does determine the size and position of the components within a container.
  • The alternative to using layout managers is absolute positioning by pixel coordinates.
  • Absolute positioning is achieved through setting a containers layout property to null. Absolute positioning is not platform portable.
  • Issues such as a size of fonts and screens ensure that a layout that is correct based on coordinates can potentially be unusable on other platforms.

Unlike absolute positioning, layout managers have mechanisms to cope with the following situations:

  1. The resizing of the GUI by the user
  2. Different fonts and font sizes used by different operating systems or by user customization
  3. The text layout requirements of different international locales (left-right, right-left, vertical)
  • To cope with these situations, layout managers lay out components according to a predetermined policy.
  • For example, a policy of the GridLayout is to position child components in equal sized cells, starting at the top left and working left to right, top to bottom until the grid is full.
  • The following sections describe some of the layout managers that you can use. Each section highlights the policy used by the layout manager under discussion.

The BorderLayout Layout Manager:-

  • BorderLayout arranges the components in five different regions: CENTER, NORTH, SOUTH, EAST, and WEST.
  • The border layout manager limits the number of components added to each region to one.
  • The position of the component should be specified. If no position is specified, by default the component is added to the CENTER. All the extra space left is used by the component in the CENTER.
  • BorderLayout is the default layout for JFrame, and JApplet. The code below shows the BorderLayout example. It adds five buttons to the JFrame.

  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class BorderExample
  4. {
  5. private JFrame f;
  6. private JButton bn, bs, bw, be, bc;
  7. public BorderExample()
  8. {
  9. f = new JFrame(“Border Layout”);
  10. bn = new JButton(“Button 1”);
  11. bc = new JButton(“Button 2”);
  12. bw = new JButton(“Button 3”);
  13. bs = new JButton(“Button 4”);
  14. be = new JButton(“Button 5”);
  15. }
  16. public void launchFrame()
  17. {
  18. f.add(bn, BorderLayout.NORTH);
  19. f.add(bs, BorderLayout.SOUTH);
  20. f.add(bw, BorderLayout.WEST);
  21. f.add(be, BorderLayout.EAST);
  22. f.add(bc, BorderLayout.CENTER);
  23. f.setSize(400,200);
  24. f.setVisible(true);
  25. }
  26. public static void main(String[] args)
  27. {
  28. BorderExample guiWindow2 = new BorderExample();
  29. guiWindow2.launchFrame();
  30. }
  31. }

The FlowLayout Layout Manager:-

  • FlowLayout arranges the components in a row. By default, it arranges the components from LEFT_TO_RIGHT.
  • This orientation can be changed using the ComponentOrientation property RIGHT_TO_LEFT.
  • The vertical and horizontal spacing between the components can be specified. If not, the default vertical and horizontal gap of five units is used.
  • The code below shows the FlowLayout example. It adds five buttons to the JFrame.


Java Object Serialization

  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class FlowExample
  4. {
  5. private JFrame f;
  6. private JButton b1;
  7. private JButton b2;
  8. private JButton b3;
  9. private JButton b4;
  10. private JButton b5;
  11. public FlowExample()
  12. {
  13. f  = new JFrame(“GUI Example”);
  14. b1 = new JButton(“Button 1”);
  15. b2 = new JButton(“Button 2”);
  16. b3 = new JButton(“Button 3”);
  17. b4 = new JButton(“Button 4”);
  18. b5 = new JButton(“Button 5”);
  19. }
  20. public void launchFrame()
  21. {
  22. f.setLayout(new FlowLayout());
  23. f.add(b1);
  24. f.add(b2);
  25. f.add(b3);
  26. f.add(b4);
  27. f.add(b5);
  28. f.pack();
  29. f.setVisible(true);
  30. }
  31. public static void main(String[] args)
  32. {
  33. flowExample guiWindow = new FlowExample();
  34. guiWindow.launchFrame();
  35. }
  36. } // end of FlowExample class

The BoxLayout Layout Manager:-

  • BoxLayout aarranges the components either vertically or horizontally.
  • The BoxLayout constructor takes a parameter called axis, where the direction to align the components should be specified.
  • This parameter can take any of following values:
  1. X_AXIS – Components are arranged horizontally from left to right.
  2. Y_AXIS – Components are arranged vertically from top to bottom.
  3. LINE_AXIS – Components are laid in the same direction as words in a line.
  4. PAGE_AXIS – Components are arranged in the same direction as the lines in a page.

The CardLayout Layout Manager:-

  • CardLayout arranges the components as a stack of cards. Each card accepts a single component for display.
  • By making this single component a container, you can display multiple components in a card.
  • Only one card is visible at a time.

The GridLayout Layout Manager:-

  • GridLayout arranges the components in row and columns. Each component occupies the same amount of space in the container.
  • When creating the grid layout, the number of rows and columns should be specified. If not specified, by default, the layout manager creates one row and one column.
  • The vertical gap and the horizontal gap between the component can also be specified.
  • The code below shows the GridLayout example. It adds five buttons to the JFrame.

  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class GridExample
  4. {
  5. private JFrame f;
  6. private JButton b1, b2, b3, b4, b5;
  7. public GridExample()
  8. {
  9. f = new JFrame(“Grid Example”);
  10. b1 = new JButton(“Button 1”);
  11. b2 = new JButton(“Button 2”);
  12. b3 = new JButton(“Button 3”);
  13. b4 = new JButton(“Button 4”);
  14. b5 = new JButton(“Button 5”);
  15. }
  16. public void launchFrame()
  17. {
  18. f.setLayout(new GridLayout(3,2));
  19. f.add(b1);
  20. f.add(b2);
  21. f.add(b3);
  22. f.add(b4);
  23. f.add(b5);
  24. f.pack();
  25. f.setVisible(true);
  26. }
  27. public static void main(String[] args)
  28. {
  29. GridExample guiWindow = new GridExample();
  30. guiWindow.launchFrame();
  31. }
  32. }

The GridBagLayout Layout Manager:-

  • GridBagLayout arranges the components in rows and columns, similar to grid layout, but provides a wide variety of flexibility option for resizing and positioning the components.
  • This layout is used to design complex GUIs. The constraints on the components are specified using the GridBagConstraintss class.
  • Some of the constants in this class are gridwidth, gridtheight, gridy, weightx, and weighty.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply