Source Code Cross Referenced for ButtonGroup.java in  » 6.0-JDK-Core » swing » javax » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025        package javax.swing;
026
027        import java.awt.event.*;
028        import java.util.Vector;
029        import java.util.Enumeration;
030        import java.io.Serializable;
031
032        /**
033         * This class is used to create a multiple-exclusion scope for
034         * a set of buttons. Creating a set of buttons with the
035         * same <code>ButtonGroup</code> object means that
036         * turning "on" one of those buttons 
037         * turns off all other buttons in the group.
038         * <p>
039         * A <code>ButtonGroup</code> can be used with
040         * any set of objects that inherit from <code>AbstractButton</code>.
041         * Typically a button group contains instances of 
042         * <code>JRadioButton</code>,
043         * <code>JRadioButtonMenuItem</code>,
044         * or <code>JToggleButton</code>.
045         * It wouldn't make sense to put an instance of 
046         * <code>JButton</code> or <code>JMenuItem</code>
047         * in a button group
048         * because <code>JButton</code> and <code>JMenuItem</code>
049         * don't implement the selected state.
050         * <p>
051         * Initially, all buttons in the group are unselected. 
052         * <p>
053         * For examples and further information on using button groups see
054         * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
055         * a section in <em>The Java Tutorial</em>.
056         * <p>
057         * <strong>Warning:</strong>
058         * Serialized objects of this class will not be compatible with
059         * future Swing releases. The current serialization support is
060         * appropriate for short term storage or RMI between applications running
061         * the same version of Swing.  As of 1.4, support for long term storage
062         * of all JavaBeans<sup><font size="-2">TM</font></sup>
063         * has been added to the <code>java.beans</code> package.
064         * Please see {@link java.beans.XMLEncoder}.
065         *
066         * @version 1.45 05/05/07
067         * @author Jeff Dinkins
068         */
069        public class ButtonGroup implements  Serializable {
070
071            // the list of buttons participating in this group
072            protected Vector<AbstractButton> buttons = new Vector();
073
074            /**
075             * The current selection.
076             */
077            ButtonModel selection = null;
078
079            /**
080             * Creates a new <code>ButtonGroup</code>.
081             */
082            public ButtonGroup() {
083            }
084
085            /**
086             * Adds the button to the group.
087             * @param b the button to be added
088             */
089            public void add(AbstractButton b) {
090                if (b == null) {
091                    return;
092                }
093                buttons.addElement(b);
094
095                if (b.isSelected()) {
096                    if (selection == null) {
097                        selection = b.getModel();
098                    } else {
099                        b.setSelected(false);
100                    }
101                }
102
103                b.getModel().setGroup(this );
104            }
105
106            /**
107             * Removes the button from the group.
108             * @param b the button to be removed
109             */
110            public void remove(AbstractButton b) {
111                if (b == null) {
112                    return;
113                }
114                buttons.removeElement(b);
115                if (b.getModel() == selection) {
116                    selection = null;
117                }
118                b.getModel().setGroup(null);
119            }
120
121            /**
122             * Clears the selection such that none of the buttons 
123             * in the <code>ButtonGroup</code> are selected.
124             * 
125             * @since 1.6       
126             */
127            public void clearSelection() {
128                if (selection != null) {
129                    ButtonModel oldSelection = selection;
130                    selection = null;
131                    oldSelection.setSelected(false);
132                }
133            }
134
135            /**
136             * Returns all the buttons that are participating in
137             * this group.
138             * @return an <code>Enumeration</code> of the buttons in this group
139             */
140            public Enumeration<AbstractButton> getElements() {
141                return buttons.elements();
142            }
143
144            /**
145             * Returns the model of the selected button.
146             * @return the selected button model
147             */
148            public ButtonModel getSelection() {
149                return selection;
150            }
151
152            /**
153             * Sets the selected value for the <code>ButtonModel</code>.
154             * Only one button in the group may be selected at a time.
155             * @param m the <code>ButtonModel</code>
156             * @param b <code>true</code> if this button is to be
157             *   selected, otherwise <code>false</code>
158             */
159            public void setSelected(ButtonModel m, boolean b) {
160                if (b && m != null && m != selection) {
161                    ButtonModel oldSelection = selection;
162                    selection = m;
163                    if (oldSelection != null) {
164                        oldSelection.setSelected(false);
165                    }
166                    m.setSelected(true);
167                }
168            }
169
170            /**
171             * Returns whether a <code>ButtonModel</code> is selected.
172             * @return <code>true</code> if the button is selected,
173             *   otherwise returns <code>false</code>
174             */
175            public boolean isSelected(ButtonModel m) {
176                return (m == selection);
177            }
178
179            /**
180             * Returns the number of buttons in the group.
181             * @return the button count
182             * @since 1.3
183             */
184            public int getButtonCount() {
185                if (buttons == null) {
186                    return 0;
187                } else {
188                    return buttons.size();
189                }
190            }
191
192        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.