java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to implement group of buttons in an application
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4089
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to implement group of buttons in an application E-mail
User Rating: / 7
PoorBest 

This Java tip illustrates a method of creating a group of buttons in an application. A ButtonGroup is usually created when developer wants to provide a list of choices to users and wants him to choose one from them. If user selects second choice his first choice will be deselected.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleButtonGroupExample {
    
    public static void main(String[] args) {
        // Some choices
        JRadioButton choice1, choice2, choice3;
        choice1 = new JRadioButton("Bach: Well Tempered Clavier, Book I");
        choice1.setActionCommand("bach1");
        choice2 = new JRadioButton("Bach: Well Tempered Clavier, Book II");
        choice2.setActionCommand("bach2");
        choice3 = new JRadioButton("Shostakovich: 24 Preludes and Fugues");
        choice3.setActionCommand("shostakovich");
        
        // A group, to ensure that we only vote for one.
        final ButtonGroup group = new ButtonGroup();
        group.add(choice1);
        group.add(choice2);
        group.add(choice3);
        
        // A simple ActionListener, showing each selection 
        // using the ButtonModel
        class VoteActionListener implements ActionListener {
            public void actionPerformed(ActionEvent ev) {
                String choice = group.getSelection().getActionCommand();
                System.out.println("ACTION Choice Selected: " + choice);
            }
        }
        
        // A simple ItemListener, showing each selection and deselection
        class VoteItemListener implements ItemListener {
            public void itemStateChanged(ItemEvent ev) {
                boolean selected = (ev.getStateChange() == 
                        ItemEvent.SELECTED);
                
                AbstractButton button = 
                        (AbstractButton)ev.getItemSelectable();
                
                System.out.println("ITEM Choice Selected: " + selected +
                        ", Selection: " + button.getActionCommand());
            }
        }
        
        // Add listeners to each button
        ActionListener alisten = new VoteActionListener();
        choice1.addActionListener(alisten);
        choice2.addActionListener(alisten);
        choice3.addActionListener(alisten);
        
        ItemListener ilisten = new VoteItemListener();
        choice1.addItemListener(ilisten);
        choice2.addItemListener(ilisten);
        choice3.addItemListener(ilisten);
        
        // Throw everything together
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c = frame.getContentPane();
        c.setLayout(new GridLayout(01));
        c.add(new JLabel("Vote for your favorite prelude & fugue cycle"));
        c.add(choice1);
        c.add(choice2);
        c.add(choice3);
        frame.pack();
        frame.setVisible(true);
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.