Results 1 to 13 of 13
  1. #1
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Help A Program to Restart

    If the user says Yes to find another commission amount, how do I get the whole program to restart?

    public class CommissionCalculatorGUI extends javax.swing.JFrame {

    /**
    * Creates new form CommissionCalculatorGUI
    */
    public CommissionCalculatorGUI() {
    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")

    private void salesAmountTextActionPerformed(java.awt.event.Acti onEvent evt) {
    }

    private void calculateCommissionButtonActionPerformed(java.awt. event.ActionEvent evt) {


    double salesAmount = 0; //sets value to 0
    double answer = salesAmount; //commission amount
    int repeat; // Confirm dialog button clicked

    //tests to make sure a number was inputted
    try {
    salesAmount = Double.parseDouble(
    this.salesAmountText.getText());
    }
    catch (Exception e) {
    JOptionPane.showMessageDialog(this, "Please input a number amount",
    "Error", JOptionPane.ERROR_MESSAGE);
    return;
    }

    //action coding to take the sales amount entered and multiply it times
    //the selected commission rate
    if(commissionRate1.isSelected()) {
    answer = (double) (0.10*salesAmount);
    }

    else if(commissionRate2.isSelected()) {
    answer = (double) (0.15*salesAmount);
    }

    else if(commissionRate3.isSelected()) {
    answer = (double) (0.20*salesAmount);
    }

    else {
    JOptionPane.showMessageDialog(this, "Please input a commission rate!",
    "Error", JOptionPane.ERROR_MESSAGE);
    return;
    }

    this.commissionAnswer.setText("Your commission is: $" +((String.format("%.02f", +answer))));

    // Does the user want to find another commission amount?
    repeat = JOptionPane.showConfirmDialog(null,
    "Would you like to find another commission amount?", "Please Confirm.",
    JOptionPane.YES_NO_OPTION);

    if(repeat == JOptionPane.NO_OPTION) {
    System.exit(0);
    }
    else if(repeat == JOptionPane.YES_OPTION) {
    remove(repeat);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help A Program to Restart

    Put the code that you want to be repeated in a loop. Ask the user at the end of the loop, exit the loop if the user says to quit.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    I tried a do while loop but it errored out when I hit yes in the GUI. I am very new and probably doing something dumb.

    Java Code:
        private void calculateCommissionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                          
           
       
            double salesAmount = 0; //sets value to 0
            double answer = salesAmount; //commission amount
            int repeat; // Confirm dialog button clicked 
            
            do {
            //tests to make sure a number was inputted
                try {
                    salesAmount = Double.parseDouble(
                        this.salesAmountText.getText());
                }   
                catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Please input a number amount",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            
            //action coding to take the sales amount entered and multiply it times
            //the selected commission rate
                if(commissionRate1.isSelected()) {
                    answer = (double) (0.10*salesAmount);
                }
            
                else if(commissionRate2.isSelected()) {
                    answer = (double) (0.15*salesAmount);
                }
            
                else if(commissionRate3.isSelected()) {
                    answer = (double) (0.20*salesAmount);
                }
            
                else {
                    JOptionPane.showMessageDialog(this, "Please input a commission rate!",
                            "Error", JOptionPane.ERROR_MESSAGE);
                return;
                }
                
                this.commissionAnswer.setText("Your commission is: $" +((String.format("%.02f", +answer))));
     
            // Does the user want to find another commission amount?
               repeat = JOptionPane.showConfirmDialog(null,
                           "Would you like to find another commission amount?", "Please Confirm.",
                           JOptionPane.YES_NO_OPTION);
               
                    if(repeat == JOptionPane.NO_OPTION) {
                        System.exit(0);
                    }
                    else if(repeat == JOptionPane.YES_OPTION) {
                        remove(repeat);
        }                                                         
        
            }while (repeat == JOptionPane.YES_OPTION);
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help A Program to Restart

    it errored out
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.awt.Container.remove(Container.java:1195)
    at Week2.CommissionCalculatorGUI.calculateCommissionB uttonActionPerformed(CommissionCalculatorGUI.java: 205)
    at Week2.CommissionCalculatorGUI.access$100(Commissio nCalculatorGUI.java:15)
    at Week2.CommissionCalculatorGUI$2.actionPerformed(Co mmissionCalculatorGUI.java:59)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
    BUILD SUCCESSFUL (total time: 20 seconds)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help A Program to Restart

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.awt.Container.remove(Container.java:1195)
    at Week2.CommissionCalculatorGUI.calculateCommissionB uttonActionPerformed(CommissionCalculatorGUI.java: 205)
    The call to remove() at line 205 has an invalid value. What is that code supposed to do with the value returned by the JOptionPane method?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    remove(repeat);

    I put this in to make the dialog box close when the user choose 'Yes'.

    When I remove that line, the box stays on the screen when the user chooses 'Yes' but does not clear out the data on the calculator for new input.

    When I have that line in, if I hit 'Yes' twice, the box disappears but the calculator does not not reset nor can you do anything further with the calculator except look at it.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help A Program to Restart

    The remove() method is in the Container class. Did you read the API doc to see what that method does?
    I don't understand why you are using it with the value returned by the JOptionPane class's method.

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    Like this?

    Sorry for all the questions ... been struggling with this all day
    Attached Files Attached Files

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help A Program to Restart

    Make a small complete program and post it here on the forum.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Re: Help A Program to Restart

    notify them to close and reopen the program

  12. #12
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    Quote Originally Posted by Norm View Post
    Make a small complete program and post it here on the forum.
    Java Code:
    import javax.swing.JOptionPane;
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Melissa
     */
    public class TestGui extends javax.swing.JFrame {
    
        /**
         * Creates new form TestGui
         */
        public TestGui() {
            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();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jTextField1 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            buttonGroup1.add(jRadioButton1);
            jRadioButton1.setText("jRadioButton1");
    
            buttonGroup1.add(jRadioButton2);
            jRadioButton2.setText("jRadioButton2");
    
            jTextField1.setText("jTextField1");
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            jButton1.setText("Ok!");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(149, 149, 149)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(158, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(51, 51, 51)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton2)
                    .addGap(42, 42, 42)
                    .addComponent(jButton1)
                    .addContainerGap(82, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            double salesAmount = 0; //sets value to 0
            double answer = salesAmount; //commission amount
            int repeat; // Confirm dialog button clicked 
            
            do {
            //tests to make sure a number was inputted
                try {
                    salesAmount = Double.parseDouble(
                        this.jTextField1.getText());
                }   
                catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Please input a number amount",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                repeat = JOptionPane.showConfirmDialog(null,
                           "Would you like to try again?", "Please Confirm.",
                           JOptionPane.YES_NO_OPTION);
               
                    if(repeat == JOptionPane.NO_OPTION) {
                        System.exit(0);
                    }
                    else if(repeat == JOptionPane.YES_OPTION) {
                    }                                                         
        
            }while (JOptionPane.YES_OPTION == repeat);
        }                                        
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(TestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(TestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(TestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(TestGui.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 TestGui().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JButton jButton1;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

  13. #13
    Icyis is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help A Program to Restart

    Thank you everyone, a friend helped me fix it and it is working as intended.

    Thanks for your patience with a newbie!

Similar Threads

  1. Replies: 6
    Last Post: 06-19-2013, 06:36 PM
  2. How can I restart my jvm and program programmatically?
    By guest_user in forum Advanced Java
    Replies: 7
    Last Post: 08-30-2011, 03:45 PM
  3. Restart gif animation possible?
    By Addez in forum New To Java
    Replies: 12
    Last Post: 01-25-2011, 10:29 PM
  4. Restart main
    By Syahrul in forum New To Java
    Replies: 3
    Last Post: 11-22-2009, 09:34 AM
  5. Replies: 6
    Last Post: 11-04-2009, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •