Results 1 to 4 of 4
  1. #1
    Wolfie is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Erasing Memory after user selections

    Hi folks,

    I am on my last few projects in my Java programming class and have a question about erasing memory after a user makes his/her selection on a GUI (check boxes and radio buttons) and an output is provided on a message box, and the message box is closed.

    The problem that I am having is that the total values remain in memory after the message box is closed and if new selections are made, they add to what was stored in memory. What I want to do is when the message box is closed and new selections are made, it starts out fresh.

    My program meets the minimum requirements for the class, but came across this issue and would like to learn how I would correct this. If you need anymore sections let me know but I think this woud be where I would place the necessary modification.

    Java Code:
     public double getSubTotal()
          {
                
             subTotal = subTotal + bread.breadPrice() + filler.fillerPrice();
             
             return subTotal;
          }
          
          public double getTax()
          {
             tax = subTotal * TAX;
             
             return tax;
          }
       	
          public double getTotal()
          {
             double total = subTotal + tax;
          	
             return total;
          }
          
          private void createCalculateButton()
          {
             calculateButton = new JButton("Calculate");
                         
             class CalculateButtonListener implements ActionListener
             {
                        	
                public void actionPerformed(ActionEvent event)
                {
                   JOptionPane.showMessageDialog(null, "Subtotal: " + getSubTotal() + "\n" +
                      	"Tax: " + getTax() + "\n" + "Total: " + getTotal());
                }
                
             }
             
             ActionListener listener = new CalculateButtonListener();
             calculateButton.addActionListener(listener);
          }
       	
          private void createExitButton()
          {
             exitButton = new JButton("Exit");
          	
             class ExitButtonListener implements ActionListener
             {
                        	
                public void actionPerformed(ActionEvent event)
                {
                   System.exit(0);
                }
                
             }
             
             ActionListener listener = new ExitButtonListener();
             exitButton.addActionListener(listener);
          }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Perhaps the class needs a method to make subTotal equal to zero. (and perhaps do other things to put in instance into an "initial" state like clear the check boxes etc) This method could be called whenever the gui is made visible.

  3. #3
    Wolfie is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Would it be legal to call the method to set subtotal to zero at the end of the calculateButton's action event?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think so. But the real test is to try it...

    By the way I think that method could be written:

    Java Code:
    private void createCalculateButton()
          {
             calculateButton = new JButton("Calculate");
             calculateButton.addActionListener(new ActionListener() 
                 {
                     public void actionPerformed(ActionEvent event)
                     {
                         JOptionPane.showMessageDialog(null, "Subtotal: " + getSubTotal() + "\n" +
                      	"Tax: " + getTax() + "\n" + "Total: " + getTotal());
                     }
                 });
          }

Similar Threads

  1. RadioButton selections in an Exam application
    By riddhikarnik in forum AWT / Swing
    Replies: 6
    Last Post: 06-29-2009, 04:41 AM
  2. How to open a file without erasing its contents?
    By spmchugh82 in forum New To Java
    Replies: 2
    Last Post: 12-17-2008, 01:52 AM
  3. Writing to excel file erasing existing formatting
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:58 PM
  4. Replies: 2
    Last Post: 08-21-2008, 07:33 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
  •