Solved

how to determine what radio button is selected and perform the appropriate action when the jbutton is pressed

Posted on 2007-04-07
4
241 Views
Last Modified: 2010-03-31
Im trying to setup some code where you select a radio button and depending on what you select a different value is added..
I have the code below and when the radio button for quarter,dime or nickel is selected I want to add the corresponding value to total when the deposit coin button is presseed... What I don't know how to do is do a caseon whether or not the button is selected, any help?

package project4;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

public class SodaMachine {

      private ButtonGroup buttonGroup = new ButtonGroup();
      private JTextField totalTextField;
      private JComboBox billComboBox;
      private JFrame frame;
      private float total=0;

      /**
       * Launch the application
       * @param args
       */
      public static void main(String args[]) {
            try {
                  SodaMachine window = new SodaMachine();
                  window.frame.setVisible(true);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }

      /**
       * Create the application
       */
      public SodaMachine() {
            initialize();
      }

      /**
       * Initialize the contents of the frame
       */
      private void initialize() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JRadioButton nickelRadioButton;
            nickelRadioButton = new JRadioButton();
            buttonGroup.add(nickelRadioButton);
            nickelRadioButton.setText("Nickel");

            JRadioButton dimeRadioButton;
            dimeRadioButton = new JRadioButton();
            buttonGroup.add(dimeRadioButton);
            dimeRadioButton.setText("Dime");

            JRadioButton quarterRadioButton;
            quarterRadioButton = new JRadioButton();
            quarterRadioButton.setSelected(true);
            buttonGroup.add(quarterRadioButton);
            quarterRadioButton.setText("Quarter");

            JButton depositCoinButton;
            depositCoinButton = new JButton();
            depositCoinButton.setText("Deposit Coin");

            billComboBox = new JComboBox();
            billComboBox.setToolTipText("Please select your bill to enter then press deposit bill");
            billComboBox.setModel(new DefaultComboBoxModel(new String[] {"1 Dollar", "5 Dollars"}));

            JButton depositBillButton;
            depositBillButton = new JButton();
            depositBillButton.setText("Deposit Bill");

            JButton buyCanButton;
            buyCanButton = new JButton();
            buyCanButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent arg0) {
                  }
            });
            buyCanButton.setText("Buy Can");

            JButton buyBottleButton;
            buyBottleButton = new JButton();
            buyBottleButton.setText("Buy Bottle");

            JPanel graphicsPanel;
            graphicsPanel = new JPanel();

            totalTextField = new JTextField();

            JLabel messageLabel;
            messageLabel = new JLabel();
            final GroupLayout groupLayout = new GroupLayout((JComponent) frame.getContentPane());
            groupLayout.setHorizontalGroup(
                  groupLayout.createParallelGroup(GroupLayout.LEADING)
                        .add(groupLayout.createSequentialGroup()
                              .addContainerGap()
                              .add(groupLayout.createParallelGroup(GroupLayout.TRAILING)
                                    .add(GroupLayout.LEADING, buyBottleButton, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .add(GroupLayout.LEADING, buyCanButton, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .add(GroupLayout.LEADING, groupLayout.createSequentialGroup()
                                          .add(groupLayout.createParallelGroup(GroupLayout.TRAILING)
                                                .add(groupLayout.createSequentialGroup()
                                                      .add(nickelRadioButton)
                                                      .addPreferredGap(LayoutStyle.RELATED)
                                                      .add(dimeRadioButton))
                                                .add(billComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                          .addPreferredGap(LayoutStyle.RELATED)
                                          .add(quarterRadioButton)
                                          .addPreferredGap(LayoutStyle.RELATED)
                                          .add(groupLayout.createParallelGroup(GroupLayout.LEADING, false)
                                                .add(depositBillButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(depositCoinButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .add(messageLabel, GroupLayout.PREFERRED_SIZE, 262, GroupLayout.PREFERRED_SIZE)
                                    .add(totalTextField, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
                              .addPreferredGap(LayoutStyle.RELATED)
                              .add(graphicsPanel, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                              .addContainerGap())
            );
            groupLayout.setVerticalGroup(
                  groupLayout.createParallelGroup(GroupLayout.TRAILING)
                        .add(groupLayout.createSequentialGroup()
                              .addContainerGap(25, Short.MAX_VALUE)
                              .add(groupLayout.createParallelGroup(GroupLayout.LEADING)
                                    .add(groupLayout.createSequentialGroup()
                                          .add(groupLayout.createParallelGroup(GroupLayout.BASELINE)
                                                .add(billComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .add(depositBillButton))
                                          .addPreferredGap(LayoutStyle.RELATED)
                                          .add(totalTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                          .add(12, 12, 12)
                                          .add(groupLayout.createParallelGroup(GroupLayout.BASELINE)
                                                .add(nickelRadioButton, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                                .add(dimeRadioButton)
                                                .add(quarterRadioButton)
                                                .add(depositCoinButton))
                                          .addPreferredGap(LayoutStyle.RELATED)
                                          .add(buyCanButton)
                                          .addPreferredGap(LayoutStyle.RELATED)
                                          .add(buyBottleButton)
                                          .add(59, 59, 59)
                                          .add(messageLabel, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                                          .add(75, 75, 75))
                                    .add(groupLayout.createSequentialGroup()
                                          .add(graphicsPanel, GroupLayout.PREFERRED_SIZE, 303, GroupLayout.PREFERRED_SIZE)
                                          .addContainerGap())))
            );
            frame.getContentPane().setLayout(groupLayout);
            frame.pack();
      }

}
0
Comment
Question by:urobins
  • 3
4 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 18871639
buttonGroup.getSelection() returns the selected button model...

or you may want to check each radio button if it is selected,

for e.g.,

if( nickelRadioButton.isSelected() ){

}else if( dimeRadioButton.isSelected() ){

}....
0
 

Author Comment

by:urobins
ID: 18872184
okay i'll give this a shot, thanks.  Is there a preferred method for doing this?
0
 

Author Comment

by:urobins
ID: 18872262
When I try this I get cannot refer to a non-final variable... I tried putting it in the depostCoin button action event, is this the wrong place for it?
0
 

Author Comment

by:urobins
ID: 18872390
Actually I got it working by making my var declerations final... Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now