Results 1 to 4 of 4
- 04-15-2011, 09:52 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
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); }
- 04-15-2011, 10:03 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-15-2011, 11:04 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Would it be legal to call the method to set subtotal to zero at the end of the calculateButton's action event?
- 04-15-2011, 11:21 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
RadioButton selections in an Exam application
By riddhikarnik in forum AWT / SwingReplies: 6Last Post: 06-29-2009, 04:41 AM -
How to open a file without erasing its contents?
By spmchugh82 in forum New To JavaReplies: 2Last Post: 12-17-2008, 01:52 AM -
Writing to excel file erasing existing formatting
By jmHoekst in forum New To JavaReplies: 1Last Post: 09-16-2008, 05:58 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks