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;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024:
025: public class RadioButtonDemo extends JFrame {
026: public static void main(String s[]) {
027: JFrame frame = new RadioButtonDemo();
028: frame.addWindowListener(new WindowAdapter() {
029: public void windowClosing(WindowEvent e) {
030: System.exit(0);
031: }
032: });
033:
034: frame.pack();
035: frame.setVisible(true);
036: }
037:
038: public RadioButtonDemo() {
039: setContentPane(new RadioButtonDemoPanel());
040: }
041:
042: public static class RadioButtonDemoPanel extends JPanel {
043: static String birdString = "Bird";
044: static String catString = "Cat";
045: static String dogString = "Dog";
046: static String rabbitString = "Rabbit";
047: static String pigString = "Pig";
048:
049: JLabel picture;
050:
051: public RadioButtonDemoPanel() {
052: // Create the radio buttons.
053: JRadioButton birdButton = new JRadioButton(birdString);
054: birdButton.setMnemonic(KeyEvent.VK_B);
055: birdButton.setActionCommand(birdString);
056: birdButton.setSelected(true);
057:
058: JRadioButton catButton = new JRadioButton(catString);
059: catButton.setMnemonic(KeyEvent.VK_C);
060: catButton.setActionCommand(catString);
061:
062: JRadioButton dogButton = new JRadioButton(dogString);
063: dogButton.setMnemonic(KeyEvent.VK_D);
064: dogButton.setActionCommand(dogString);
065:
066: JRadioButton rabbitButton = new JRadioButton(rabbitString);
067: rabbitButton.setMnemonic(KeyEvent.VK_R);
068: rabbitButton.setActionCommand(rabbitString);
069:
070: JRadioButton pigButton = new JRadioButton(pigString);
071: pigButton.setMnemonic(KeyEvent.VK_P);
072: pigButton.setActionCommand(pigString);
073:
074: // Group the radio buttons.
075: ButtonGroup group = new ButtonGroup();
076: group.add(birdButton);
077: group.add(catButton);
078: group.add(dogButton);
079: group.add(rabbitButton);
080: group.add(pigButton);
081:
082: // Register a listener for the radio buttons.
083: RadioListener myListener = new RadioListener();
084: birdButton.addActionListener(myListener);
085: catButton.addActionListener(myListener);
086: dogButton.addActionListener(myListener);
087: rabbitButton.addActionListener(myListener);
088: pigButton.addActionListener(myListener);
089:
090: // Set up the picture label
091: picture = new JLabel(new ImageIcon("resources/images/"
092: + birdString + ".gif"));
093:
094: // The preferred size is hard-coded to be the width of the
095: // widest image and the height of the tallest image.
096: // A real program would compute this.
097: picture.setPreferredSize(new Dimension(177, 122));
098:
099: // Put the radio buttons in a column in a panel
100: JPanel radioPanel = new JPanel();
101: radioPanel.setLayout(new GridLayout(0, 1));
102: radioPanel.add(birdButton);
103: radioPanel.add(catButton);
104: radioPanel.add(dogButton);
105: radioPanel.add(rabbitButton);
106: radioPanel.add(pigButton);
107:
108: setLayout(new BorderLayout());
109: add(radioPanel, BorderLayout.WEST);
110: add(picture, BorderLayout.CENTER);
111: setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
112: }
113:
114: /** Listens to the radio buttons. */
115: class RadioListener implements ActionListener {
116: public void actionPerformed(ActionEvent e) {
117: picture.setIcon(new ImageIcon("resources/images/"
118: + e.getActionCommand() + ".gif"));
119: }
120: }
121:
122: }
123: }
|