KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > TwoColumnLayoutTest


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2006 Amit Bhayani / JBoss
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack;
23
24 /**
25  * TwoColumnLayoutTest.java is a 1.4 application that
26  * demonstrates the use of JButton, JTextField and
27  * JLabel. It requires no other files.
28  * @author abhayani Amit Bhayani
29  */

30
31 import com.izforge.izpack.gui.TwoColumnConstraints;
32 import com.izforge.izpack.gui.TwoColumnLayout;
33
34 import javax.swing.*;
35 import java.awt.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38
39 public class TwoColumnLayoutTest implements ActionListener JavaDoc
40 {
41     JFrame converterFrame;
42     JPanel converterPanel;
43     JTextField tempText;
44     JLabel label;
45     JButton addRow;
46     JButton removeRow;
47
48     boolean removed = false;
49
50     public TwoColumnLayoutTest()
51     {
52         //Create and set up the window.
53
converterFrame = new JFrame("TwoColumnLayoutTest");
54         converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55         converterFrame.setSize(new Dimension(240, 80));
56
57         TwoColumnLayout layout = new TwoColumnLayout(10, 5, 30, 25, TwoColumnLayout.LEFT);
58
59         //Create and set up the panel.
60
converterPanel = new JPanel();
61         converterPanel.setLayout(layout);
62
63         //Add the widgets.
64
addWidgets();
65
66         //Set the default button.
67
converterFrame.getRootPane().setDefaultButton(addRow);
68
69         //Add the panel to the window.
70
converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER);
71
72         //Display the window.
73
converterFrame.pack();
74         converterFrame.setVisible(true);
75     }
76
77     /**
78      * Create and add the widgets.
79      */

80     private void addWidgets()
81     {
82         //Create widgets.
83
tempText = new JTextField("10", 30);
84         TwoColumnConstraints constraints = new TwoColumnConstraints();
85         constraints.position = TwoColumnConstraints.EAST;
86
87         label = new JLabel("Label : ");
88         TwoColumnConstraints constraints1 = new TwoColumnConstraints();
89         constraints1.position = TwoColumnConstraints.WEST;
90
91         addRow = new JButton("Add Row");
92         TwoColumnConstraints constraints2 = new TwoColumnConstraints();
93         constraints2.position = TwoColumnConstraints.BOTH;
94
95         //Listen to events from the Convert button.
96
addRow.addActionListener(this);
97
98         //Add the widgets to the container.
99
converterPanel.add(tempText, constraints);
100         converterPanel.add(label, constraints1);
101         converterPanel.add(addRow, constraints2);
102
103
104         label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
105
106     }
107
108     public void actionPerformed(ActionEvent JavaDoc event)
109     {
110
111         if (!removed)
112         {
113
114             converterPanel.remove(tempText);
115             converterPanel.remove(label);
116             removed = true;
117
118         }
119         else
120         {
121             TwoColumnConstraints constraints = new TwoColumnConstraints();
122             constraints.position = TwoColumnConstraints.EAST;
123             converterPanel.add(tempText, constraints);
124
125             TwoColumnConstraints constraints1 = new TwoColumnConstraints();
126             constraints1.position = TwoColumnConstraints.WEST;
127             converterPanel.add(label, constraints1);
128             removed = false;
129
130         }
131         converterPanel.repaint();
132
133     }
134
135     /**
136      * Create the GUI and show it. For thread safety,
137      * this method should be invoked from the
138      * event-dispatching thread.
139      */

140     private static void createAndShowGUI()
141     {
142         //Make sure we have nice window decorations.
143
JFrame.setDefaultLookAndFeelDecorated(true);
144
145         TwoColumnLayoutTest converter = new TwoColumnLayoutTest();
146     }
147
148     public static void main(String JavaDoc[] args)
149     {
150         //Schedule a job for the event-dispatching thread:
151
//creating and showing this application's GUI.
152
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc()
153         {
154             public void run()
155             {
156                 createAndShowGUI();
157             }
158         });
159     }
160 }
161
Popular Tags