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

Thread: Problem validating textfield

  1. #1
    jrandaz is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Problem validating textfield

    I am working on a project where it uses Pythagorean theorem to find the third side(c). I am required to validate the two text fields. Rules include;
    - Textfields for side A and B are required
    - Textfields for side A and B must have a double that is greater than zero.

    I have the first rule figured out, but cant seem to figure out how to parse to find the double in a text field and to make it greater than zero.

    Guidance would be appreciated!
    Thanks

    Java Code:
    // import libraries
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.lang.Math;
    
    
    // main method declaration
    public class RightTriangleApp
    {
        public static void main(String[] args)
        {
    		// display frame
            JFrame frame = new TriangleFrame();
            frame.setVisible(true);
        }
    } // end main method
    
    
    // class to define frame
    class TriangleFrame extends JFrame
    {
        // define frame
        public TriangleFrame()
        {
            // add code here to definte the frame
    		setTitle("Right Triangles");
    		setSize(210, 150);
    		centerWindow(this);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new TrianglePanel();
    		this.add(panel);
    
    
    
        } // end define frame
    
    	// method to center frame
        private void centerWindow(Window w)
        {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = tk.getScreenSize();
            setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    
        } // end centerWindow method
    
    } 
    // class to define panel
    class TrianglePanel extends JPanel implements ActionListener
    {
    	double a = 0;
    	double b = 0;
    	double c = 0;
    
    
        // decare JTextFields, JLabels, JButtons
    	private JTextField sideATextField,
    					   sideBTextField,
    					   sideCTextField;
    
    
    	private JLabel 	   sideALabel,
    					   sideBLabel,
    					   sideCLabel;
    
    
    	private JButton	   calculateButton,
    					   resetButton;
    
    
    
        // define panel
        public TrianglePanel()
        {
            // set layout
    		JPanel displayPanel = new JPanel();
    		displayPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
    
    
    
    		sideALabel = new JLabel("Side A:");
    		displayPanel.add(sideALabel);
    
         
    		sideATextField = new JTextField(10);
    		sideATextField.addActionListener(this);
    		displayPanel.add(sideATextField);
    
    
       
    		sideBLabel = new JLabel("Side B:");
    		displayPanel.add(sideBLabel);
    
         
    		sideBTextField = new JTextField(10);
    		displayPanel.add(sideBTextField);
    
            
    		sideCLabel = new JLabel("Side C:");
    		displayPanel.add(sideCLabel);
    
           
    		sideCTextField = new JTextField(10);
    		sideCTextField.setEditable(false);
    		sideCTextField.setFocusable(false);
    		displayPanel.add(sideCTextField);
    
    		// create button panel and set layout
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    		// enter button
    		calculateButton = new JButton("Calculate");
    		calculateButton.addActionListener(this);
    		buttonPanel.add(calculateButton);
    
    
            // reset button
    		resetButton = new JButton("Reset");
    		resetButton.addActionListener(this);
    		buttonPanel.add(resetButton);
    
    		// add panels to main panel
    		this.setLayout(new BorderLayout());
    		this.add(displayPanel, BorderLayout.CENTER);
    		this.add(buttonPanel, BorderLayout.SOUTH);
    
    
        } // end define panel
    
        // action event
        public void actionPerformed(ActionEvent e)
        {
    
    		Object source = e.getSource();
    
    		if(source == calculateButton)
    		{
    
    			if(isValidData())
    			{
    				a = Double.parseDouble(sideATextField.getText());
    				b = Double.parseDouble(sideBTextField.getText());
    
    				c = Math.pow(a,2) + Math.pow(b,2);
    
    				sideCTextField.setText("" + c);
    			}
    
    		}
    
    		// clear data
    		else if(source == resetButton)
    		{
    			clearFields();
    		}
    
    	}
    	// end actionPerformed
    
    	private boolean isValidData()
    	{
    
    		boolean isValidData = false;
    
    
    		if (sideATextField.getText().length() == 0)
    		{
    			String message = "Side A is a required field.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideATextField.requestFocusInWindow();
    			isValidData = false;
    		}
    		else if (sideBTextField.getText().length() == 0)
    		{
    			String message = "Side B is a required field.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideBTextField.requestFocusInWindow();
    			isValidData = false;
    		}
    		else if(Double.parseDouble(sideATextField.getText()))
    		{
    			String message = "Side A must be a valid number.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideATextField.requestFocusInWindow();
    			isValidData = false;
    		}
    		else if(Double.parseDouble(sideBTextField.getText()))
    		{
    			String message = "Side B must be a valid number.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideBTextField.requestFocusInWindow();
    			isValidData = false;
    		}
    		else if(Double.parseDouble(sideATextField.getText() > 0))
    		{
    			String message = "Side A must be greater than zero.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideBTextField.requestFocusInWindow();
    			isValidData = false;
    
    		}
    		else if(Double.parseDouble(sideBTextField.getText() > 0))
    		{
    			String message = "Side B must be greater than zero.\n" + "Please re-enter.";
    			JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    			sideBTextField.requestFocusInWindow();
    			isValidData = false;
    
    		}
    		else
    		{
    			isValidData = true;
    		}
    		return isValidData;
    
    
    	}
    
    
    	// method to clear data
    	private void clearFields()
    	{
    		sideATextField.setText("");
    		sideBTextField.setText("");
    		sideCTextField.setText("");
    	}
    
    
    }

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

    Default Re: Problem validating textfield

    I've usually used a try/catch block and catch a NumberFormatException instance if testing if a String parses to a valid double or not. I also usually trim the String first.
    jrandaz likes this.

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

    Default Re: Problem validating textfield

    Hey, you never replied to the helpful answer in your previous thread. Don't you want to do that, so that the volunteers who try to help you know that you read their posts and appreciate their efforts?

Similar Threads

  1. Validating log in using database
    By mDennis10 in forum New To Java
    Replies: 6
    Last Post: 03-11-2012, 01:17 AM
  2. TextField problem.
    By miller.bartek in forum AWT / Swing
    Replies: 3
    Last Post: 01-11-2011, 07:06 PM
  3. Validating Jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 03:54 PM
  4. validating account
    By ServletEst in forum Java Servlet
    Replies: 3
    Last Post: 08-24-2009, 02:11 PM
  5. Textfield border problem
    By jurka in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2008, 10:15 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
  •