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

    Default Help with JOptionPane

    I am writing a GUI program to calculate sales tax. I am trying to make it more robust by making it catch an exception of the user enters somthing other than a double. I try to compile this code, but it tells me that on the lines where "JOptionPane" appears, that the compiler cannot find the symbol. Help? Thanks.

    Java Code:
    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)
                    {
                        JOptionPane.showMessageDialog("You need to enter a number!", "Error", JOptionPane.ERROR_MESSAGE);
                        JOptionPane.setVisisble(true);
                        String input = billField.getText();
                        bill = Double.parseDouble(input);
                    }
                }
    
                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
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Have a close look at the signature of the three overloaded showMessageDialog methods of JOptionPane at JOptionPane. Yours does not match.
    I'm new to Java but I like to help where ever I can. :)

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

    Default

    Thanks! I got it!

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

    Default

    You're welcome.
    I'm new to Java but I like to help where ever I can. :)

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

    Default

    Another question though, is there a way to make a JFrame visible in a location other than the top left corner of the screen?

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

    Default

    Nope, all frames must stick to to the top left corner... ;)

    No, seriously, it is possible of course. Have a look here: How to Make Frames
    I'm new to Java but I like to help where ever I can. :)

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

Similar Threads

  1. JOptionPane
    By hfbroady in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2010, 12:07 PM
  2. JOptionPane
    By izzy in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 05:55 PM
  3. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 11:20 AM
  4. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 10:40 PM
  5. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 03:17 AM

Posting Permissions

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