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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
Java and GPO 11 47
sites similar to codingbat to improve coding hanson skills 3 25
mockito example issue 8 38
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

746 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

11 Experts available now in Live!

Get 1:1 Help Now