Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
252 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

604 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