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
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

695 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