javaswingappletappletviewer

show warning if users enter letter instead number in java applet


I am writing an tip calculator app in java applet with GUI, my question is how I make sure the error message will pop up if users enter letter instead of number

it is my first time asking question, please be easy on me! Thanks!!!

import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Typing in the text field and hitting return adds text to text area.
// Clicking on button erases the text area.
public class TextApplet extends Controller implements ActionListener 
{
    private static final int ROWS = 1; // rows in TextArea
    private static final int COLS = 10; // cols in text field & area

    private String amount;
    private float number;
    private JTextField inField, output;         // Input field

    private JButton clear, calc;     
         // button to clear output


    public void begin() 
    {

        Container contentPane = getContentPane();
        JPanel topPanel = new JPanel();       // prepare text field & label
        JLabel inLabel = new JLabel("Bill Cost: ");
        inField = new JTextField(COLS);

        inField.addActionListener(this);
        JLabel topTitle = new JLabel("Tip Calculator", JLabel.CENTER);
        JPanel combinePanel = new JPanel();

        combinePanel.add ( inLabel );
        combinePanel.add ( inField );
        JPanel combinePanel1 = new JPanel();
        combinePanel1.add ( topTitle );

        topPanel.add ( combinePanel1 );
        topPanel.add ( combinePanel );


        topPanel.setLayout ( new GridLayout ( 3,1) );
        contentPane.add(topPanel,BorderLayout.NORTH);

        JPanel centerPanel = new JPanel();     // prepare text area & label
        JLabel outLabel = new JLabel("Bill + Tip:");
        output = new JTextField(COLS);
        output.setEditable(false);        // Prevent user from wrting in output
        centerPanel.add(outLabel);
        centerPanel.add(output);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel(); 

        // create button
        clear = new JButton("  Clear  ");
        calc = new JButton("Calculate");
        calc.addActionListener(this);
        clear.addActionListener(this);
        bottomPanel.add(calc);
        bottomPanel.add(clear);

        contentPane.add(bottomPanel,BorderLayout.SOUTH);
        validate();
    }

    // add text to area if user hits return, else erase text area
    public void actionPerformed(ActionEvent evt) 
    {
        if (evt.getSource() == calc )
        { 
            amount = inField.getText();
            number = ( Float.parseFloat( amount ) );
            number = (15*number/100);

            output.setText ( Float.toString ( number ) + '$' );
        }

        else if (evt.getSource() == clear )
        {
            output.setText("$");
            inField.setText("");
        }
     }
 }

Solution

  • Hello Friend I will give a suggestion

    please add validation when call actionPerformed method

     public void actionPerformed(ActionEvent evt) 
    {
        if (evt.getSource() == calc )
        { 
           if(validate()){
            amount = inField.getText();
            number = ( Float.parseFloat( amount ) );
            number = (15*number/100);
    
            output.setText ( Float.toString ( number ) + '$' );
    
            }
         else{
       // show message for inter valid number or any other
           }
         }
    
        else if (evt.getSource() == clear )
        {
            output.setText("$");
            inField.setText("");
        }
     }
      boolean validate(){  
    
       try{
        amount = inField.getText();
            number = ( Float.parseFloat( amount ) );
          return true;
    
      }
       catch(Exception e){
       return false;
    
     }
    
    
    
    
     }