001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2006 Amit Bhayani / JBoss
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack;
023:
024: /**
025: * TwoColumnLayoutTest.java is a 1.4 application that
026: * demonstrates the use of JButton, JTextField and
027: * JLabel. It requires no other files.
028: * @author abhayani Amit Bhayani
029: */
030:
031: import com.izforge.izpack.gui.TwoColumnConstraints;
032: import com.izforge.izpack.gui.TwoColumnLayout;
033:
034: import javax.swing.*;
035: import java.awt.*;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038:
039: public class TwoColumnLayoutTest implements ActionListener {
040: JFrame converterFrame;
041: JPanel converterPanel;
042: JTextField tempText;
043: JLabel label;
044: JButton addRow;
045: JButton removeRow;
046:
047: boolean removed = false;
048:
049: public TwoColumnLayoutTest() {
050: //Create and set up the window.
051: converterFrame = new JFrame("TwoColumnLayoutTest");
052: converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
053: converterFrame.setSize(new Dimension(240, 80));
054:
055: TwoColumnLayout layout = new TwoColumnLayout(10, 5, 30, 25,
056: TwoColumnLayout.LEFT);
057:
058: //Create and set up the panel.
059: converterPanel = new JPanel();
060: converterPanel.setLayout(layout);
061:
062: //Add the widgets.
063: addWidgets();
064:
065: //Set the default button.
066: converterFrame.getRootPane().setDefaultButton(addRow);
067:
068: //Add the panel to the window.
069: converterFrame.getContentPane().add(converterPanel,
070: BorderLayout.CENTER);
071:
072: //Display the window.
073: converterFrame.pack();
074: converterFrame.setVisible(true);
075: }
076:
077: /**
078: * Create and add the widgets.
079: */
080: private void addWidgets() {
081: //Create widgets.
082: tempText = new JTextField("10", 30);
083: TwoColumnConstraints constraints = new TwoColumnConstraints();
084: constraints.position = TwoColumnConstraints.EAST;
085:
086: label = new JLabel("Label : ");
087: TwoColumnConstraints constraints1 = new TwoColumnConstraints();
088: constraints1.position = TwoColumnConstraints.WEST;
089:
090: addRow = new JButton("Add Row");
091: TwoColumnConstraints constraints2 = new TwoColumnConstraints();
092: constraints2.position = TwoColumnConstraints.BOTH;
093:
094: //Listen to events from the Convert button.
095: addRow.addActionListener(this );
096:
097: //Add the widgets to the container.
098: converterPanel.add(tempText, constraints);
099: converterPanel.add(label, constraints1);
100: converterPanel.add(addRow, constraints2);
101:
102: label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
103:
104: }
105:
106: public void actionPerformed(ActionEvent event) {
107:
108: if (!removed) {
109:
110: converterPanel.remove(tempText);
111: converterPanel.remove(label);
112: removed = true;
113:
114: } else {
115: TwoColumnConstraints constraints = new TwoColumnConstraints();
116: constraints.position = TwoColumnConstraints.EAST;
117: converterPanel.add(tempText, constraints);
118:
119: TwoColumnConstraints constraints1 = new TwoColumnConstraints();
120: constraints1.position = TwoColumnConstraints.WEST;
121: converterPanel.add(label, constraints1);
122: removed = false;
123:
124: }
125: converterPanel.repaint();
126:
127: }
128:
129: /**
130: * Create the GUI and show it. For thread safety,
131: * this method should be invoked from the
132: * event-dispatching thread.
133: */
134: private static void createAndShowGUI() {
135: //Make sure we have nice window decorations.
136: JFrame.setDefaultLookAndFeelDecorated(true);
137:
138: TwoColumnLayoutTest converter = new TwoColumnLayoutTest();
139: }
140:
141: public static void main(String[] args) {
142: //Schedule a job for the event-dispatching thread:
143: //creating and showing this application's GUI.
144: javax.swing.SwingUtilities.invokeLater(new Runnable() {
145: public void run() {
146: createAndShowGUI();
147: }
148: });
149: }
150: }
|