How to Make a Clear Button + Numeric and Missing Input Error Message on a Java GUI – Java #60

*Watch copying and pasting the quotation marks, you may have to retype them to avoid an error

*Error Message code is in the dagbaGUI class

//—————Main Function Screenshot

//—————-Main Function Code

Programmer: Akinwale Owi
Company:Dagba Computers
Program Description:Show how to make a numeric and missing input error message on a GUI
package dagba.computers;

public class DagbaComputers {

public static void main(String[] args)
//Creating a dagbaGUI OBJECT
dagbaGUI sampleGUI = new dagbaGUI();

}//end of main

}//end of class

//———–End of Main Function Code

//————————-Calculations class screenshot

//——————-Beginning of Calculations Class Code

//Where the calculations will be done

package dagba.computers;

public class Calculations
private double carPrice;//variable that will hold car price


public void setPrice(double sentPrice)
this.carPrice = sentPrice;//setting the value of carPrice for the Entire Class
}///end of set price

public double getNewValue()
double depreciationAmount,depreciatedValue;

depreciationAmount= carPrice*0.19;//calculating the actual amount of depreciation

depreciatedValue = carPrice – depreciationAmount; //value of the car after depreciation is factored in

return depreciatedValue;//sending the answer out

}//end of getNewValue

}//End of Calculations Class

//———————–End of Calculations Class Code

*dagbaGUI Class is too big for a screenshot but here is the code

//———beginning of dagbaGUI class code

package dagba.computers;

import java.awt.GridLayout;
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class dagbaGUI

public dagbaGUI()
//creating the jFrame
JFrame myFrame = new JFrame(“Dagba Computers Sample”);

//Setting close operation

//Size of JFrame
myFrame.setPreferredSize(new Dimension (400, 400));

//The Dimensions of the Grid (rows, columns)
myFrame.setLayout(new GridLayout(4, 2));

//——————-Building the contents of the frame——————–
JLabel typeLabel = new JLabel(“Car Type:”);

JTextField typeField = new JTextField(); //input box for Car Type

JLabel priceLabel = new JLabel(“Car Price : $”);

JTextField priceField = new JTextField(); //input box for Car Price

JButton clearButton = new JButton(“Clear”);
JButton computeButton = new JButton(“Compute”);
//———–End of Buttons——————–

//———–beginning of answer JLabels and JTextFields———
JLabel answerLabel = new JLabel(“Value after 1 Year: $”);
JTextField answerField = new JTextField();
//————-end of answer JLabels and JTextFields————–

//—————–End of Building contents of the JFrame——————-

//—————-adding items to JFrame—————————

//——————–Beginning of ActionListeners——————

//adding the clear function to the clearbutton
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
//End of Clear Button ActionListener

//Compute Button event handler to check for empty fields!
computeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
//beginning of if validation checks
if (e.getSource() == computeButton)
//get input from GUI and pass to object
if (
//List of fields to check for
typeField.getText().isEmpty() ||

)//end of empty fields
//missing input
JOptionPane.showMessageDialog(null, “Please enter all input “);

//creating Calculations object to send over data to Calculations class
Calculations guiCar = new Calculations();

//—————–end of complete input check—————-

//beginning of numeric input & check for Car Price
guiCar.setPrice(Double.parseDouble(priceField.getText()));//price check


catch (NumberFormatException ex)
JOptionPane.showMessageDialog(null,”Car Price must be in numbers only”);

//displaying results on GUI
answerField.setText(String.valueOf(guiCar.getNewValue()));//putting depreciated value in answerField on GUI

}//end of calculateButton if

}//end of calculateButton’s actionperformed

//——————–End of ActionListeners————————

//Pack and Visibility

}//end of public dagbaGUI

}//end of dagbaGUI Class

//———————-End of dagbaGUI Class Code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: