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

Calculations(){};//Constructor

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
//Build

package dagba.computers;

//Imports
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
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//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

//——————Buttons——————–
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—————————
myFrame.add(typeLabel);
myFrame.add(typeField);
myFrame.add(priceLabel);
myFrame.add(priceField);
myFrame.add(clearButton);
myFrame.add(computeButton);
myFrame.add(answerLabel);
myFrame.add(answerField);

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

//adding the clear function to the clearbutton
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
typeField.setText(“”);
priceField.setText(“”);
answerField.setText(“”);
}
});
//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() ||
priceField.getText().isEmpty()

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

//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
try
{
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
myFrame.pack();
myFrame.setVisible(true);

}//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:

WordPress.com Logo

You are commenting using your WordPress.com 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 WordPress.com.

Up ↑

%d bloggers like this: