KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > swing > VerticalAlignPanel


1 package net.matuschek.swing;
2
3 import java.awt.*;
4 import javax.swing.*;
5 /*********************************************
6     Copyright (c) 2001 by Daniel Matuschek
7  *********************************************/

8
9
10 /**
11  * This is a JPanel object that allows "stacking" of components without
12  * explicit use of the GridBagLayout. Simply create that panel and add
13  * components to it.
14  *
15  * @author Daniel Matuschek
16  * @version $Revision: 1.4 $
17  */

18 public class VerticalAlignPanel extends JPanel {
19
20     private static final long serialVersionUID = -5964950356897442955L;
21
22     private int count=0;
23     private int inset=2;
24
25     /**
26      * Initializes a new VerticalAlignPanel object.
27      */

28     public VerticalAlignPanel() {
29         super();
30         setLayout(new GridBagLayout());
31     }
32
33     /**
34      * Initializes a new VerticalAlignPanel object and sets the inset value.
35      * The inset is used to have some space around the components
36      */

37     public VerticalAlignPanel(int inset) {
38         this();
39         this.inset = inset;
40     }
41
42     /**
43      * adds a new line to the VerticalAlignPanel
44      * with the given Component
45      * @param comp a JComponent object that should be added in the next line
46      * @param fill can be java.awt.GridBagConstraints.
47      * NONE|HORIZONTAL|VERTICAL|BOTH and describes if the component should
48      * fill the place at the right side or keep its defaults size
49      */

50     public void add(JComponent comp, int fill) {
51         GridBagConstraints consRight = new GridBagConstraints();
52         consRight.gridx = 0;
53         consRight.gridy = count;
54         consRight.insets = new Insets(inset,inset,inset,inset);
55         consRight.fill = fill;
56         consRight.anchor = GridBagConstraints.CENTER;
57
58         this.add(comp, consRight);
59
60         count++;
61     }
62
63     /**
64      * adds a new line to the VerticalAlignPanel
65      * with the given Component
66      * @param comp a JComponent object that should be added in the next line
67      * @see #add(JComponent, int) (fill will be set to NONE)
68      */

69     public void add(JComponent comp) {
70         add(comp,GridBagConstraints.NONE);
71     }
72
73
74 } // VerticalAlignPanel
75
Popular Tags