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
246 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
tomcat administrtor 12 61
jboss 7.1 start up error 1 40
Html Table Looping (part 2) 5 27
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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