Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Getting a calculate button to work, and clear button to reset everything.

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Getting a calculate button to work, and clear button to reset everything.

    I'm creating a Pizza Ordering GUI, and I am confused on how & where to add an Action Listener for the calculate button to recognize the jradio button
    and jcheckboxes. Can anyone help me?


    import java.text.NumberFormat;
    import java.util.Locale;
    import java.awt.event.ActionEvent;   ///might not need
     
     
    public class PizzaGUI extends javax.swing.JFrame {
     
     
        /**
         * Creates new form PizzaGUI
         */
        public PizzaGUI() {
            initComponents();
     
     
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            buttonGroup1 = new javax.swing.ButtonGroup();
            jLabel1 = new javax.swing.JLabel();
            CalculateButton = new javax.swing.JButton();
            ClearButton = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
            TotalPrice = new javax.swing.JTextField();
            jPanel1 = new javax.swing.JPanel();
            smallRadio = new javax.swing.JRadioButton();
            medRadio = new javax.swing.JRadioButton();
            largeRadio = new javax.swing.JRadioButton();
            jPanel2 = new javax.swing.JPanel();
            SausageTopping = new javax.swing.JCheckBox();
            PepperoniTopping = new javax.swing.JCheckBox();
            SalamiTopping = new javax.swing.JCheckBox();
            OlivesTopping = new javax.swing.JCheckBox();
            MushroomsTopping = new javax.swing.JCheckBox();
            AnchoviesTopping = new javax.swing.JCheckBox();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Giovanni's Pizza");
     
            jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            jLabel1.setText("Giovanni's Online Pizza Order");
     
            CalculateButton.setText("Calculate");
            CalculateButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CalculateButtonActionPerformed(evt);
                }
            });
     
            ClearButton.setText("Clear");
            ClearButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ClearButtonActionPerformed(evt);
                }
            });
     
            jLabel4.setText("Price:");
     
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Size"));
            jPanel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
     
            buttonGroup1.add(smallRadio);
            smallRadio.setText("Small");
            smallRadio.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    smallRadioActionPerformed(evt);
                }
            });
     
            buttonGroup1.add(medRadio);
            medRadio.setText("Medium");
     
            buttonGroup1.add(largeRadio);
            largeRadio.setText("Large");
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(smallRadio)
                    .addGap(26, 26, 26)
                    .addComponent(medRadio)
                    .addGap(18, 18, 18)
                    .addComponent(largeRadio)
                    .addContainerGap(35, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(smallRadio)
                        .addComponent(medRadio)
                        .addComponent(largeRadio))
                    .addGap(72, 72, 72))
            );
     
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Toppings", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
            jPanel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
     
            SausageTopping.setText("Sausage");
            SausageTopping.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    SausageToppingActionPerformed(evt);
                }
            });
     
            PepperoniTopping.setText("Pepperoni");
     
            SalamiTopping.setText("Salami");
     
            OlivesTopping.setText("Olives");
     
            MushroomsTopping.setText("Mushrooms");
     
            AnchoviesTopping.setText("Anchovies");
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(SausageTopping)
                        .addComponent(PepperoniTopping)
                        .addComponent(SalamiTopping))
                    .addGap(29, 29, 29)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(AnchoviesTopping)
                        .addComponent(MushroomsTopping)
                        .addComponent(OlivesTopping))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(SausageTopping)
                        .addComponent(OlivesTopping))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(PepperoniTopping)
                        .addComponent(MushroomsTopping))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(SalamiTopping)
                        .addComponent(AnchoviesTopping))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(64, 64, 64)
                            .addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(CalculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(TotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap(21, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(CalculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(TotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(24, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void SausageToppingActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
     
        private void smallRadioActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
     
        private void CalculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            // declaring variables and create constants
            NumberFormat currency = NumberFormat.getCurrencyInstance();        //formats
            double SmallPizza, MediumPizza, LargePizza;
            double Sausage, Salami, Mushrooms, Pepperoni, Olives, Anchovies;
            SmallPizza = 6.99;
            MediumPizza = 8.99;
            LargePizza = 10.99;
            Sausage = 1.49;
            Salami = 1.49;
            Pepperoni = 1.49;
            Olives = 0.99;
            Mushrooms = 0.99;
            Anchovies = 0.99;
     
     
     
     
     
        }                                               
     
        private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // clears everything. If radiobutton topping/ if price is selected, reset it.
            smallRadio.setSelected(false);
            medRadio.setSelected(false);
            largeRadio.setSelected(false);
            SausageTopping.setSelected(false);
            SalamiTopping.setSelected(false);
            PepperoniTopping.setSelected(false);
            OlivesTopping.setSelected(false);
            MushroomsTopping.setSelected(false);
            AnchoviesTopping.setSelected(false);
            TotalPrice.setText(null);
     
     
        }                                           
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url]http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new PizzaGUI().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JCheckBox AnchoviesTopping;
        private javax.swing.JButton CalculateButton;
        private javax.swing.JButton ClearButton;
        private javax.swing.JCheckBox MushroomsTopping;
        private javax.swing.JCheckBox OlivesTopping;
        private javax.swing.JCheckBox PepperoniTopping;
        private javax.swing.JCheckBox SalamiTopping;
        private javax.swing.JCheckBox SausageTopping;
        private javax.swing.JTextField TotalPrice;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JRadioButton largeRadio;
        private javax.swing.JRadioButton medRadio;
        private javax.swing.JRadioButton smallRadio;
        // End of variables declaration                   
    }
    Last edited by Ancharius; April 3rd, 2014 at 03:59 PM. Reason: Adding code


  2. #2
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting a calculate button to work, and clear button to reset everything.

    Any help would be appreciated.
    What I have tried is adding action listeners for the jradiobutton and checkboxes but I wasn't sure where to place them.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    smallRadio.addActionListener();
    medRadio.addActionListener();
    largeRadio.addActionListener()

    small, med, large are the variable names of my sizes in the picture.
    And I was going to do the same for the toppings.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Getting a calculate button to work, and clear button to reset everything.

    You are asking for help with the graphical GUI Builder rather than help with Java. Most here would be able to help you code the action listeners "by hand," but adding them to the graphical builder's code is a different process, one less familiar to most here. You may get help if you're patient. Then again, you may not.

    Good luck!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting a calculate button to work, and clear button to reset everything.

    Suggested reading:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    GUI Builders Pitfalls

  5. The Following User Says Thank You to copeg For This Useful Post:

    Ancharius (April 3rd, 2014)

Similar Threads

  1. Replies: 1
    Last Post: March 30th, 2014, 03:56 AM
  2. Code help with reset button error
    By Razorfc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2012, 02:20 PM
  3. Calculate Button Not Working
    By czerenborg in forum AWT / Java Swing
    Replies: 8
    Last Post: September 8th, 2011, 09:25 PM
  4. Reset button.
    By ish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 07:57 AM
  5. My Clear Info button is not working with Screenshot
    By drkossa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 15th, 2010, 08:06 AM

Tags for this Thread