01: package net.matuschek.swing;
02:
03: import java.awt.*;
04: import javax.swing.*;
05:
06: /*********************************************
07: Copyright (c) 2001 by Daniel Matuschek
08: *********************************************/
09:
10: /**
11: * An OptionPanel is a special JPane object that act a little bit like
12: * JOptionPane
13: * After creating the pane you can add lines that consists of a label
14: * at the left side and a JComponent at the ride side.
15: *
16: * This is useful for simple dialogs.
17: *
18: * @author Daniel Matuschek
19: * @version $Revision: 1.5 $
20: */
21: public class OptionPanel extends JPanel {
22:
23: private static final long serialVersionUID = -4539847595846318366L;
24:
25: private int count = 0;
26: private int inset = 2;
27:
28: /**
29: * Initializes a new OptionPanel object and sets the inset value.
30: * The inset is used to have some space around the components
31: */
32: public OptionPanel(int inset) {
33: super ();
34: setLayout(new GridBagLayout());
35: this .inset = inset;
36: }
37:
38: /**
39: * adds a new line to the OptionPane with a label at the left side
40: * and the given Component at the right side
41: * @param labelText the text for the label
42: * @param comp a JComponent object that should be added at the right
43: * side
44: * @param fill can be java.awt.GridBagConstraints.
45: * NONE|HORIZONTAL|VERTICAL|BOTH and describes if the component should
46: * fill the place at the right side or keep its defaults size
47: */
48: public void add(String labelText, JComponent comp, int fill) {
49: JLabel label = new JLabel();
50: label.setText(labelText);
51: GridBagConstraints consLeft = new GridBagConstraints();
52: consLeft.gridx = 0;
53: consLeft.gridy = count;
54: consLeft.insets = new Insets(inset, inset, inset, inset);
55: consLeft.anchor = GridBagConstraints.WEST;
56: this .add(label, consLeft);
57:
58: GridBagConstraints consRight = new GridBagConstraints();
59: consRight.gridx = 1;
60: consRight.gridy = count;
61: consRight.insets = new Insets(inset, inset, inset, inset);
62: consRight.fill = fill;
63: consRight.anchor = GridBagConstraints.WEST;
64:
65: this .add(comp, consRight);
66:
67: count++;
68: }
69:
70: /**
71: * adds a new line to the OptionPane with a label at the left side
72: * and the given Component at the right side
73: * @param labelText the text for the label
74: * @param comp a JComponent object that should be added at the right
75: * side
76: * @see #add(String, JComponent, int) (fill will be set to NONE)
77: */
78: public void add(String labelText, JComponent comp) {
79: add(labelText, comp, GridBagConstraints.NONE);
80: }
81:
82: } // OptionPanel
|