Results 1 to 5 of 5
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default 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));
        }
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Change the infinite loop into something that makes sense. I suggest changing the declaration of the variable "bill" to this:
    Java Code:
    doube bill = -1d;
    Then try implementing a do-while loop where you try to obtain the value for bill while the variable bill is not -1d.

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    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?

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    JOptionPane.showMessageDialog is the one I meant.
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  3. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Tags for this Thread

Posting Permissions

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