Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: JOptionPanel

  1. #1
    yankeetooter7 is offline student
    Join Date
    Jun 2012
    Location
    MD
    Posts
    8
    Rep Power
    0

    Default JOptionPanel

    Hi all...

    I'm new here, and hoping I can get some help. I am writing a GUI java program, which requires that an internal dialog box pops up when an invalid value is entered in one of the main panel JTextFields. I have everything set up, and the program runs, with one glitch. When an invalid value is entered, the internal dialog box pops up twice before returning to the original panel. Please help...

    Thanks.

    (Below is my code if you want to wade through it.)

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.NumberFormat;
    
    public class CalculatorPanel extends JPanel
    {
    	private JButton calculate;
    	private JLabel loanAmount, interestRate, numberOfPayments, monthlyPayment,
    		resultLabel;
    	private JTextField amount, rate, payments;
    	double amt, intRate, numPayments, monthly, powerBase, power, percentage;
    	
    	NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		
    	//--------------------------------------------------------------------
    	//	Constructor sets up the main GUI components
    	//--------------------------------------------------------------------
    	public CalculatorPanel()
    	{
    		calculate = new JButton("Calculate");
    		calculate.addActionListener(new ButtonListener());	
    		
    		ButtonListener listener = new ButtonListener();
    		calculate.addActionListener(listener);
    		
    		loanAmount = new JLabel("Loan amount:");
    		interestRate = new JLabel("Interest rate:");
    		numberOfPayments = new JLabel("Number of payments:");
    		monthlyPayment = new JLabel("Monthly payments: ");
    		resultLabel = new JLabel("$0.00");
    					
    		amount = new JTextField(15);
    		rate = new JTextField(15);
    		payments = new JTextField(10);		
    						
    		add(loanAmount);
    		add(amount);
    		add(interestRate);
    		add(rate);
    		add(numberOfPayments);
    		add(payments);
    		add(monthlyPayment);
    		add(resultLabel);
    		add(calculate);
    				
    		setPreferredSize(new Dimension(275, 225));
    		setBackground(Color.yellow);
    	}
    	
    	//-----------------------------------------------------------------
    	//	Represents an action listener for the amount input field.
    	//-----------------------------------------------------------------
    	private class ButtonListener implements ActionListener
    	{
    		//--------------------------------------------------------------
    		//	Performs the calculation after information for each field
    		//	is entered.
    		//--------------------------------------------------------------
    		public void actionPerformed(ActionEvent event)
    		{
    			String text1 = amount.getText();
    			String text2 = rate.getText();
    			String text3 = payments.getText();
    			
    			amt = Double.parseDouble(text1);	
    			intRate = Double.parseDouble(text2);			
    			numPayments = Double.parseDouble(text3);
    									
    			do
    			{
    				String error1 = "Loan amount must be greater than 0.";
    				JOptionPane.showMessageDialog(null, error1);
    				amt = 1.0;
    			}
    			while (amt <= 0.0);
    			
    				if (intRate <= 0.0 )
    				{
    					String error2 = "Interest rate must be greater than 0.";
    					JOptionPane.showMessageDialog(null, error2);
    				}
    				else
    					if (numPayments < 1)
    					{
    						String error3 = "Number of payments entered must be at least 1.";
    						JOptionPane.showMessageDialog(null, error3);
    					}				
    			
    			intRate = intRate / 100;
    			powerBase = 1.0 + (intRate / 12.0);
    			power = Math.pow(powerBase, numPayments);
    			percentage = intRate / 12.0;
    				
    			monthly = amt * (percentage * power) / (power - 1.0);
    				
    			resultLabel.setText(fmt.format(monthly));			
    		}
    	}
    }
    Last edited by JosAH; 06-11-2012 at 10:22 AM. Reason: added [code] ... [/code] tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JOptionPanel

    A do-while loop will *always* run, at least once. Are you sure that you want to do this? Are you sure that you want to set the amt to 1.0 regardless? You may want to re-think your logic here.
    yankeetooter7 likes this.

  3. #3
    yankeetooter7 is offline student
    Join Date
    Jun 2012
    Location
    MD
    Posts
    8
    Rep Power
    0

    Default Re: JOptionPanel

    Yeah, actually I changed that back, but had already copied and pasted the incorrect one. The code now includes three if statements, but is still creating the same issue.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JOptionPanel

    Then you'll want to show the *latest* code with [code] [/code] tags, with error messages (if any) and descriptions of misbehavior (if any).

  5. #5
    yankeetooter7 is offline student
    Join Date
    Jun 2012
    Location
    MD
    Posts
    8
    Rep Power
    0

    Default Re: JOptionPanel

    No errors showing, just the internal dialog box popping up twice...

    Here's the new code...

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.NumberFormat;

    public class CalculatorPanel extends JPanel
    {
    private JButton calculate;
    private JLabel loanAmount, interestRate, numberOfPayments, monthlyPayment,
    resultLabel;
    private JTextField amount, rate, payments;
    double amt, intRate, numPayments, monthly, powerBase, power, percentage;

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    //--------------------------------------------------------------------
    // Constructor sets up the main GUI components
    //--------------------------------------------------------------------
    public CalculatorPanel()
    {
    calculate = new JButton("Calculate");
    calculate.addActionListener(new ButtonListener());

    ButtonListener listener = new ButtonListener();
    calculate.addActionListener(listener);

    loanAmount = new JLabel("Loan amount:");
    interestRate = new JLabel("Interest rate:");
    numberOfPayments = new JLabel("Number of payments:");
    monthlyPayment = new JLabel("Monthly payments: ");
    resultLabel = new JLabel("$0.00");

    amount = new JTextField(15);
    rate = new JTextField(15);
    payments = new JTextField(10);

    add(loanAmount);
    add(amount);
    add(interestRate);
    add(rate);
    add(numberOfPayments);
    add(payments);
    add(monthlyPayment);
    add(resultLabel);
    add(calculate);

    setPreferredSize(new Dimension(275, 225));
    setBackground(Color.yellow);
    }

    //-----------------------------------------------------------------
    // Represents an action listener for the amount input field.
    //-----------------------------------------------------------------
    private class ButtonListener implements ActionListener
    {
    //--------------------------------------------------------------
    // Performs the calculation after information for each field
    // is entered.
    //--------------------------------------------------------------
    public void actionPerformed(ActionEvent event)
    {
    String text1 = amount.getText();
    String text2 = rate.getText();
    String text3 = payments.getText();

    amt = Double.parseDouble(text1);
    intRate = Double.parseDouble(text2);
    numPayments = Double.parseDouble(text3);

    if (amt <= 0)
    {
    String error1 = "Loan amount must be greater than 0. Please correct your information.";
    JOptionPane.showMessageDialog(null, error1);
    resultLabel.setEnabled(false);
    }
    else
    if (intRate <= 0.0 )
    {
    String error2 = "Interest rate must be greater than 0. Please correct your information.";
    JOptionPane.showMessageDialog(null, error2);
    resultLabel.setEnabled(false);
    }
    else
    if (numPayments < 1)
    {
    String error3 = "Number of payments entered must be at least 1. Please correct your information.";
    JOptionPane.showMessageDialog(null, error3);
    resultLabel.setEnabled(false);
    }

    intRate = intRate / 100;
    powerBase = 1.0 + (intRate / 12.0);
    power = Math.pow(powerBase, numPayments);
    percentage = intRate / 12.0;

    monthly = amt * (percentage * power) / (power - 1.0);

    resultLabel.setText(fmt.format(monthly));
    }
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    yankeetooter7 is offline student
    Join Date
    Jun 2012
    Location
    MD
    Posts
    8
    Rep Power
    0

    Default Re: JOptionPanel

    Quote Originally Posted by Fubarable View Post
    Which dialog is showing? And where are your code tags as suggested previously?

    I managed to figure it out, but I appreciate your help.

    Thanks,
    yankeetooter7

Posting Permissions

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