Java Article 10 - Swing Windowed Applications

Picking up from the last article I am providing more examples of Java windowed applications created with Swing. Also in the last article I introduced if control structures, and today I will show more examples of what you can do with if.

Today we will create an application that will take a numeric input in a JTextField, evaluate it with if statements, then output the results to the text of a JLabel. A button will trigger the evaluation and there will be another button to reset the text field and the label.

The comments in the code below are there to explain a bit about what is going on. As a programmer you should comment your code so you and others can quickly determine what is going on without having to read and think about every line.

Unless you are writing a program that only you will ever see the source code, you are really writing code for someone else. Since you are writing for someone else you should make your code user-friendly, as easy to read as possible, and try to explain what you are doing in every method. Poorly or uncommented code will really ruin someone's day if they have to modify or build upon the work you have done.

Go ahead and put this class into your editor, look it over and compile/run it.

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

public class SwingWithIf extends JFrame implements ActionListener

	 // Here we declare and initialize an int to hold
	 // our input
	int inputNumber = 0;
	here we create our controls
	JButton startButton = new JButton("Evaluate");
	JButton resetButton = new JButton("Reset");
	 // A textfield is a control that allows a user to enter a small
	 // amount of text. The argument 20 specifies the number of 
	 // columns in the textfield(the length).
	JTextField textField = new JTextField(20);
	JLabel answerLabel = new JLabel();
	public SwingWithIf()


		//Remember, the panel is a invisible container for
		//our controls
		JPanel panel = new JPanel();

		//Here we add our controls to the panel
		 // Here we set the layout of the panel to a grid layout.
		 // GridLayout puts our controls into a grid. The arguments
		 // are GridLayout(number of rows, number of columns)
		 // We will go over this more in another article so don't
		 // worry to much about how layouts work.
		panel.setLayout(new GridLayout(4,1));
		 // So there is a two containers here.
		 // Our controls go into a first container, the panel,
		 // the panel's layout is specified,
		 // then the panel as added to the second container,
		 // the window.

	public void actionPerformed(ActionEvent e)
		String cmd = e.getActionCommand();
		if (cmd == "evaluate")
		 // Here we could use "if (cmd == "reset")" However
		 // we can just use an else statement. Java checks 
		 // the first if (above), and if its false then it 
		 // goes to the else.
		 // The else does not get executed if the above if is true.
			 // This clears the text in the answerLabel
			 // and the textfield, resetting our app. The 
			 // program will work without this but it is 
			 // a nice option to add.
	 // Here we write our own custom method
	 // This method evaluates the number in the text 
	 // field with some if statements
	public void evaluateInputNumber()
		inputNumber = Integer.parseInt(textField.getText());
		 // Here are our if statements that evaluate the number
		 // from the textfield.
		if(inputNumber == 5)
			answerLabel.setText("The number is equal to 5");
		if(inputNumber > 5)
			answerLabel.setText("The number is greater than 5");
		if(inputNumber < 5)
			answerLabel.setText("The number is less than 5");

	 // We did all this before in article 9 so if you want explanations
	 // about this check the example program in article 9
	public static void main(String[] args)
		SwingWithIf swingExampleObject = new SwingWithIf();
		swingExampleObject.setSize(200, 200);
		 // This tells Java to exit the app when you click the windows
		 // close button. If you don't include this your window will 
		 // disappear but the program will continue to run invisibly,
		 // even though you closed the window.

We use an if/else statement in actionPerformed(). Here else is a bit of a shortcut so we do not have to enter in the slightly longer if statement. Since there are just two buttons, we check to see if the button sent the ActionCommand "evaluate". If it did we execute the code in the braces. If it did not then we know the other button (resetButton) must have been clicked and we jump to the else statement. Else can be handy for telling Java "If anything else happens do what is in the else block."

Under the if we call a method we wrote with


This tells Java to go to and execute this method then return back here when done. You could have called this method anything you wanted but it is important to try to indicate what the method does in its name. This method evaluates the input number so evaluateInputNumber is a fair name.

evaluateInputNumber takes the number from the textfield which is a string by default and coverts it to an int with:

inputNumber = Integer.parseInt(textField.getText());

Here what you are asking Java to do is to get the text in the textfield with textField.getText(), and pass that as an argument to Integer.parseInt(). parseInt is a method in the class named Integer. parseInt converts a string into an int. We then assign this int to inputNumber.

We have to covert the string to an int to use our comparison operators in evaluateInputNumber. In evaluateInputNumber we use three of the comparison operators we talked about in article 9: ==, > and <. Here we are just checking if the number entered if equal to, less than or greater than 5. Each of the three if statements sets the text of our label to show some text we wrote explaining how the inputNumber relates to 5.

In the next article we will cover the basics of for loops, again using Swing.