Results 1 to 5 of 5
Thread: Help with JFrame exception
- 11-12-2010, 01:39 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Help with JFrame exception
I am doing an assignment for my AP Java class. We are required to create this GUI program that will ask the user for an input then will add 5% tax, and then output the new total. I have that part working, but I am trying to add an exception handler in the TaxListener that is thrown if the user enters something other than a number. I have got it to compile and run, however it is thrown into an infinite loop in which it creates an infinite number of JFrames. Can someone help me prevent this? I want it to output a single error message JFrame, then the user can close it, and go back to the GUI to input a new number. Thanks!
Java Code://************ //Adam Brewer* //Lab 11 * //************ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; class TaxGUI extends JFrame { private JLabel billLabel = new JLabel("Bill"); private JLabel totalLabel = new JLabel("Bill + tax"); private JTextField billField = new JTextField("$00.0"); private JTextField totalField = new JTextField("$00.0"); private JButton taxButton = new JButton(">>>>>"); public TaxGUI() { JPanel dataPanel = new JPanel(new GridLayout(2,2,20,10)); dataPanel.add(billLabel); dataPanel.add(billField); dataPanel.add(totalLabel); dataPanel.add(totalField); JPanel buttonPanel = new JPanel(); buttonPanel.add(taxButton); Container container = getContentPane(); container.add(dataPanel, BorderLayout.NORTH); container.add(buttonPanel, BorderLayout.SOUTH); taxButton.addActionListener(new TaxListener()); } private class TaxListener implements ActionListener { public void actionPerformed(ActionEvent e) { double bill = 0; while(true) { try { String input = billField.getText(); bill = Double.parseDouble(input); break; } catch(Exception ex) { JFrame j1 = new JFrame(); JPanel error = new JPanel(); JLabel msg = new JLabel("ERROR!"); error.add(msg); j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j1.setSize(200, 200); j1.setTitle("ERROR!"); Container pane = getContentPane(); pane.add(error); j1.setVisible(true); } } bill = bill + (bill * .05); bill = roundTwoDecimals(bill); totalField.setText("$" + bill); } } private double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); } }
- 11-12-2010, 01:46 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Change the infinite loop into something that makes sense. I suggest changing the declaration of the variable "bill" to this:
Then try implementing a do-while loop where you try to obtain the value for bill while the variable bill is not -1d.Java Code:doube bill = -1d;
- 11-12-2010, 01:46 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Why do you have a while(true) loop in your code? I think you should get rid of that.
Second, there are standard dialogues present in Swing you can use for your error message. No need to construct one yourself.
ErikI'm new to Java but I like to help where ever I can. :)
- 11-12-2010, 02:00 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
StormyWaters, I don't know anything about do-while loops. :(
venerik, I have been searching the API's and cannot find that method. I assumed there was one. What exactly is it?
- 11-12-2010, 02:12 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Similar Threads
-
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
Passing data from one JFrame to another JFrame. - need help.
By Unsub in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:33 AM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks