CSC 160 Lab 2-2

GUI programs and While Statements

The While loop is a mechanism for repeatedly executing a statement as long as a boolean condition remains true. The general format of a while statement looks like this

        while (boolean_condition)
        {
            // statement to repeat
        }
    

Example 1. A frame with text fields

This following example is a program that asks a user to enter an integer n and creates a frame that has text fields. The program will make sure that the number entered by the user is greater than 0 but no greater than 10. If a user enters a number outside of this range, the program will terminate.


package lab2prog2;

import java.awt.FlowLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Lab2Prog2
{
    public static void main(String[] args)
    {
       // Create a scanner
       Scanner sc = new Scanner(System.in);
       // Read a number from the keyboard
       System.out.print("Enter a positive integer in range 1..10: ");
       int number = sc.nextInt();       
       // Create frame with FlowLayout
       JFrame frame = new JFrame("Frame With Text Fields");
       frame.setLayout(new FlowLayout());
       // Create the specified number of text fields and add them to the frame
       
       int k = 1; 
       while (k <= number)
       {
           // Create a Text field with width 4 and add it to the frame
           JTextField tf = new JTextField(4);
           frame.add(tf);
           k++;
       }      
       // Set up the frame and show it
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
    }
}

    

The program will use a FlowLayout, which means that the layout of the text fields will change if the frame is resized. Below is a sample input output, followed by the frame displayed by the program when it begins to run, and after it has been resized by the user.


Enter a positive integer in range 1..10: 7
    

Here is the frame as initially displayed:

Here is the frame after being resized:

Exercise 1. Add some color

Modify the program so that the (content pane) of the frame has a yellow background and the text fields have a pink background. You will have to assign each text field to a variable as you create it so that you change its background color.

Exercise 2. GridLayout

Modify the program so that it uses a GridLayout. The program should ask the user for the number of rows and also for the number of columns to use for the grid. Here is a sample interaction, followed by a display of the created frame:


Enter a positive integer in range 1..10: 7
Enter the number of grid rows and  columns:2 4
    

Note that when the rows and columns of the grid are both specified, the number of columns is ignored. This is contrary to what was said in lecture (that it was the number of rows that was ignored).

Exercise 3. Color Alternation

Modify your program to switch back to a FlowLayout manager, and eliminate the part of the program that asks the user to enter the number of rows and columns for the grid layout. Now your program will just ask the user to enter the number of text fields to be created. This time, however, you should make the background color of the text fields alternate between Pink and Green:


Enter a positive integer in range 1..10: 10

Example 2. Displaying a character in a textfield

The JTextField class has a method

        void setText(String text)
    

that can be used to set a string into a text field once the text field is created. This method cannot be used to set a single character into a text field though. Thus the calls

        JTextField tf = new JTextField();
        tf.setText('a');
        char ch = 'G';
        tf.setText(ch);
    

both result in an error. To use setText() we must convert the character into a string. We can do that by using the static method valueOf of the String class:

        JTextField tf = new JTextField();
        tf.setText(String.valueOf('a'));
        char ch = 'G';
        tf.setText(String.valueOf(ch));
    

Here is a full example.


package lab2prog2;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Lab2Prog2
{
    public static void main(String[] args)
    {
       // Create frame with FlowLayout
       JFrame frame = new JFrame("Characters in Text field");
       frame.setLayout(new FlowLayout());
       // Create and add a text field with a single character
       JTextField tf = new JTextField(2);
       tf.setText(String.valueOf('a'));
       frame.add(tf);
       // Create and add another text field with a single character
       char ch = 'G';
       tf = new JTextField(2);
       tf.setText(String.valueOf(ch));
       frame.add(tf);
       // Set up the frame and show it
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
    }
}
    

Exercise 4. One Character per Text Field Assignment

Write a program that asks the user to enter a line of text. The program then reads the line, and displays a frame that shows the entire line entered, with each character being in its own textfield. See the sample user interation below.


Enter your favourite sentence:
I love Computer Science!
    

Due Date: Saturday night at the end of Week 2.

Submit only Exercise 4 for grading.