KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > ToggleButtonExample


1 /*
2  * $Id: ToggleButtonExample.java,v 1.7 2005/03/10 09:00:50 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
24  * @version $Revision: 1.7 $
25  */

26 public class ToggleButtonExample
27         extends WingSetPane {
28     static final ClassLoader JavaDoc cl = WingSet.class.getClassLoader();
29     static final SIcon icon = new SURLIcon("../icons/ButtonIcon.gif");
30     static final SIcon disabledIcon = new SURLIcon("../icons/ButtonDisabledIcon.gif");
31     static final SIcon pressedIcon = new SURLIcon("../icons/ButtonPressedIcon.gif");
32     static final SIcon rolloverIcon = new SURLIcon("../icons/ButtonRolloverIcon.gif");
33     private ButtonControls controls;
34
35     public SComponent createExample() {
36         controls = new ButtonControls();
37         SContainer p = createButtonExample();
38
39         SForm form = new SForm(new SBorderLayout());
40         form.add(controls, SBorderLayout.NORTH);
41         form.add(p, SBorderLayout.CENTER);
42         return form;
43     }
44
45     SContainer createButtonExample() {
46         SButtonGroup group = new SButtonGroup();
47         SToggleButton[] buttons = new SToggleButton[9];
48
49         for (int i = 0; i < buttons.length; i++) {
50             buttons[i] = new SToggleButton("Text " + (i + 1));
51             buttons[i].setActionCommand(buttons[i].getText());
52             if (i != 4) {
53                 buttons[i].setIcon(icon);
54                 buttons[i].setDisabledIcon(disabledIcon);
55                 buttons[i].setRolloverIcon(rolloverIcon);
56                 buttons[i].setPressedIcon(pressedIcon);
57                 buttons[i].setSelectedIcon(pressedIcon);
58             }
59             buttons[i].setToolTipText("Button " + (i+1));
60             buttons[i].setName("button" + (i+1));
61             buttons[i].setShowAsFormComponent(false);
62             group.add(buttons[i]);
63             controls.addSizable(buttons[i]);
64         }
65
66         buttons[0].setVerticalTextPosition(SConstants.TOP);
67         buttons[0].setHorizontalTextPosition(SConstants.LEFT);
68
69         buttons[1].setVerticalTextPosition(SConstants.TOP);
70         buttons[1].setHorizontalTextPosition(SConstants.CENTER);
71
72         buttons[2].setVerticalTextPosition(SConstants.TOP);
73         buttons[2].setHorizontalTextPosition(SConstants.RIGHT);
74
75         buttons[3].setVerticalTextPosition(SConstants.CENTER);
76         buttons[3].setHorizontalTextPosition(SConstants.LEFT);
77
78         buttons[4].setVerticalTextPosition(SConstants.CENTER);
79         buttons[4].setHorizontalTextPosition(SConstants.CENTER);
80
81         buttons[5].setVerticalTextPosition(SConstants.CENTER);
82         buttons[5].setHorizontalTextPosition(SConstants.RIGHT);
83
84         buttons[6].setVerticalTextPosition(SConstants.BOTTOM);
85         buttons[6].setHorizontalTextPosition(SConstants.LEFT);
86
87         buttons[7].setVerticalTextPosition(SConstants.BOTTOM);
88         buttons[7].setHorizontalTextPosition(SConstants.CENTER);
89
90         buttons[8].setVerticalTextPosition(SConstants.BOTTOM);
91         buttons[8].setHorizontalTextPosition(SConstants.RIGHT);
92
93         SPanel erg = new SPanel();
94
95         SGridLayout grid = new SGridLayout(3, 3);
96         grid.setBorder(1);
97         SPanel b = new SPanel(grid);
98
99         final SLabel pressed = new SLabel("No button pressed");
100
101         ActionListener JavaDoc action = new ActionListener JavaDoc() {
102             public void actionPerformed(ActionEvent JavaDoc e) {
103                 pressed.setText("Button \"" + e.getActionCommand() + "\" pressed");
104             }
105         };
106
107         for (int i = 0; i < buttons.length; i++)
108             buttons[i].addActionListener(action);
109
110         for (int i = 0; i < buttons.length; i++)
111             b.add(buttons[i]);
112
113         erg.add(b);
114         erg.add(new SLabel("<html><br />"));
115         erg.add(pressed);
116
117         return erg;
118     }
119
120     class ButtonControls extends ComponentControls {
121         public ButtonControls() {
122             final SCheckBox showAsFormComponent = new SCheckBox("Show as Form Component");
123             showAsFormComponent.addActionListener(new ActionListener JavaDoc() {
124                 public void actionPerformed(ActionEvent JavaDoc e) {
125                     for (Iterator JavaDoc iterator = components.iterator(); iterator.hasNext();) {
126                         SComponent component = (SComponent) iterator.next();
127                         component.setShowAsFormComponent(showAsFormComponent.isSelected());
128                     }
129                 }
130             });
131             add(showAsFormComponent);
132
133             final SCheckBox useImages = new SCheckBox("Use Icons");
134             useImages.setSelected(true);
135             useImages.addActionListener(new ActionListener JavaDoc() {
136                 public void actionPerformed(ActionEvent JavaDoc e) {
137                     boolean use = useImages.isSelected();
138
139                     for (Iterator JavaDoc iterator = components.iterator(); iterator.hasNext();) {
140                         SAbstractButton component = (SAbstractButton) iterator.next();
141                         component.setIcon(use ? icon : null);
142                         component.setDisabledIcon(use ? disabledIcon : null);
143                         component.setRolloverIcon(use ? rolloverIcon : null);
144                         component.setPressedIcon(use ? pressedIcon : null);
145                     }
146                 }
147             });
148             add(useImages);
149         }
150     }
151 }
152
Popular Tags