001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples.withtemplate;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024:
025: import net.ar.webonswing.*;
026: import net.ar.webonswing.swing.layouts.*;
027:
028: public class TemplateRadioButtonDemo extends JFrame {
029:
030: public static void main(String[] args) {
031: new TemplateRadioButtonDemo().setVisible(true);
032: }
033:
034: public TemplateRadioButtonDemo() {
035: setContentPane(new RadioButtonDemoPanel());
036: }
037:
038: public JPanel getRadioButtonPanel() {
039: return new RadioButtonDemoPanel();
040: }
041:
042: public static class RadioButtonDemoPanel extends JPanel {
043:
044: String birdString = "Bird";
045: String catString = "Cat";
046: String dogString = "Dog";
047: String rabbitString = "Rabbit";
048: String pigString = "Pig";
049:
050: JLabel picture;
051:
052: public RadioButtonDemoPanel() {
053: // Create the radio buttons.
054: final JRadioButton birdButton = new JRadioButton(birdString);
055: birdButton.setMnemonic(KeyEvent.VK_B);
056: birdButton.setActionCommand(birdString);
057: birdButton.setSelected(true);
058: birdButton
059: .setLayout(new TemplateLayout(
060: WosFramework
061: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel.radio1")));
062:
063: final JRadioButton catButton = new JRadioButton(catString);
064: catButton.setMnemonic(KeyEvent.VK_C);
065: catButton.setActionCommand(catString);
066: catButton
067: .setLayout(new TemplateLayout(
068: WosFramework
069: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel.radio1")));
070:
071: final JRadioButton dogButton = new JRadioButton(dogString);
072: dogButton.setMnemonic(KeyEvent.VK_D);
073: dogButton.setActionCommand(dogString);
074: dogButton
075: .setLayout(new TemplateLayout(
076: WosFramework
077: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel.radio1")));
078: dogButton.putClientProperty("theUserUniqueName",
079: "DogButton");
080:
081: final JRadioButton rabbitButton = new JRadioButton(
082: rabbitString);
083: rabbitButton.setMnemonic(KeyEvent.VK_R);
084: rabbitButton.setActionCommand(rabbitString);
085: rabbitButton
086: .setLayout(new TemplateLayout(
087: WosFramework
088: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel.radio1")));
089:
090: final JRadioButton pigButton = new JRadioButton(pigString);
091: pigButton.setMnemonic(KeyEvent.VK_P);
092: pigButton.setActionCommand(pigString);
093: pigButton
094: .setLayout(new TemplateLayout(
095: WosFramework
096: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel.radio1")));
097:
098: // Group the radio buttons.
099: ButtonGroup group = new ButtonGroup();
100: group.add(birdButton);
101: group.add(catButton);
102: group.add(dogButton);
103: group.add(rabbitButton);
104: group.add(pigButton);
105:
106: picture = new JLabel();
107: picture.setIcon(new ImageIcon("resources/images/"
108: + birdString + ".gif"));
109:
110: class RadioListener implements ActionListener {
111: public void actionPerformed(ActionEvent e) {
112: picture.setIcon(new ImageIcon("resources/images/"
113: + e.getActionCommand() + ".gif"));
114: }
115:
116: public String getName() {
117: return "ChangePicture";
118: }
119:
120: public Object[] getParameters() {
121: return new Object[] { picture };
122: }
123: }
124:
125: RadioListener myListener = new RadioListener();
126: birdButton.addActionListener(myListener);
127: catButton.addActionListener(myListener);
128: dogButton.addActionListener(myListener);
129: rabbitButton.addActionListener(myListener);
130: pigButton.addActionListener(myListener);
131:
132: // Set up the picture label
133:
134: // The preferred size is hard-coded to be the width of the
135: // widest image and the height of the tallest image.
136: // A real program would compute this.
137: picture.setPreferredSize(new Dimension(177, 122));
138: picture
139: .setLayout(new TemplateLayout(
140: WosFramework
141: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.thePicture")));
142:
143: // Put the radio buttons in a column in a panel
144: JPanel radioPanel = new JPanel();
145: radioPanel
146: .setLayout(new TemplateLayout(
147: WosFramework
148: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements.radioButtonPanel")));
149:
150: radioPanel.add(birdButton, "radio1");
151: radioPanel.add(catButton, "radio2");
152: radioPanel.add(dogButton, "radio3");
153: radioPanel.add(rabbitButton, "radio4");
154: radioPanel.add(pigButton, "radio5");
155:
156: setLayout(new TemplateLayout(
157: WosFramework
158: .getKeyPositionTemplateForName("RadioButtonDemo.twoElements")));
159: add(radioPanel, "radioButtonPanel");
160: add(picture, "thePicture");
161:
162: setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
163: }
164: }
165:
166: }
|