Java: Example - CombineName

The following program has two text fields, one for a first name and one for a last name. When the button is pressed, it formats them in the result field in the standard last name comma first name style. Exercises follow the source listing.

What this does is, of course, completely trivial. The point is to use a GUI structure like this as the basis for making some changes.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
// File   : gui/components/calculators/CombineName.java
// Description: Takes a first and last name to produce "Last, First"
// Purpose: This program does almost nothing but show GUI construction.
//          The "model/logic" of the program is intentionally trivial.
//          It illustrates: subclassing and constructing a JFrame,
//          the use of JTextFields, JButton, inner action listener.
// Author : Fred Swartz - 2006-12-29 - Placed in public domain.

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

//////////////////////////////////////////////////////////////// CombineName
public class CombineName extends JFrame {
    //=================================================== instance variables
    private JTextField _fNameTf        = new JTextField(8);
    private JTextField _lNameTf        = new JTextField(8);
    private JTextField _combinedNameTf = new JTextField(14);
    
    //================================================================= main
    public static void main(String[] args) {
        JFrame window = new CombineName();   // Create window.
    }
    
    //========================================================== constructor
    public CombineName() {
        //... 1. Create or set attributes of components.
        _combinedNameTf.setEditable(false);  // Don't let user change output.
        JButton combineBtn = new JButton("Combine");
        
        //... 2. Add listener(s).
        combineBtn.addActionListener(new CombineAction());
        
        //... 3. Create a panel, set layout, and add components to it.
        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("First"));
        content.add(_fNameTf);
        content.add(new JLabel("Last"));
        content.add(_lNameTf);
        content.add(combineBtn);
        content.add(new JLabel("Combined Name"));
        content.add(_combinedNameTf);
        
        //... 4. Set the content panel of window and perform layout.
        this.setContentPane(content);
        this.setTitle("CombineName Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();                       // Do layout.
        this.setLocationRelativeTo(null);  // Center window.
        this.setVisible(true);
    }
    
    ///////////////////////////////////// inner listener class CombineAction
    class CombineAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //... Get text from the text fields, combine, set text.
            //    Please make this program do something interesting.
            String first = _fNameTf.getText();
            String last  = _lNameTf.getText();
            String combined = last + ", " + first;  // Trivial logic!!!
            _combinedNameTf.setText(combined);
        }
    }
}

Exercises

  1. Draw a diagram of what the window looks like.
  2. Add a listener so that, if the user types Enter in the last name field, the same thing is done as when the button is clicked. Hint, if two (or more) components do the same thing, they can use the same listener! The right way to do this is to only create one listener object with new and assign it to a variable, then add that same listener to all components that would use it (eg, the button and the last name text field).
  3. To get you used to working more with components, enhance the program to handle a middle name. Produce a combined name of the form last name, comma, space, first name, comma, space, middle initial, period. For example, Michael Mortimer Mouse would become "Maus, Michael M.". This is Happy Trails programming, so don't worry about possible errors (eg, no middle name entered). Indicate your solution by indicating modifications to the original listing, line numbers, etc
  4. Add another button to go the other way, from combined name to the separate parts.