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.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
/** CombineName.java Example:  Generates "Last, First" from separate parts.
    @author Fred Swartz
    @version 2004-04-14
*/

import javax.swing.*;

//////////////////////////////////////////////////////////////// CombineName
public class CombineName {
    //================================================================= main
    public static void main(String[] args) {
        JFrame window = new CombineNameGUI();
        window.setTitle("CombineName Example");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.show();  // Same as window.setVisible(true)
    }//end main  
}//end class CombineName

  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 
// CombineNameGUI.java - Generates "Last, First" from separate parts.
// Fred Swartz, 2004-04-14

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

///////////////////////////////////////////////////////////// CombineNameGUI
public class CombineNameGUI extends JFrame {
    //=================================================== instance variables
    private JTextField _fName  = new JTextField(12);
    private JTextField _lName  = new JTextField(12);
    private JTextField _combinedName = new JTextField(20);

    //========================================================== constructor
    public CombineNameGUI() {
        //-- 1. Create or set attributes of components.
        _combinedName.setEditable(false);  // Don't let user change output.
        JButton btn_combine = new JButton("Combine");
        
        //--- 2. Add listener(s).
        btn_combine.addActionListener(new CombineAction());
      
        //--- 3. Create a panel and add components to it
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout());
        p.add(new JLabel("First"));
        p.add(_fName);
        p.add(new JLabel("Last"));
        p.add(_lName);
        p.add(btn_combine);
        p.add(new JLabel("Combined Name"));
        p.add(_combinedName);
        
        //-- 4. Set the content panel of window and perform layout.
        this.setContentPane(p);
        this.pack();                // Do layout.
    }//end constructor
    
    ///////////////////////////////////// inner listenre class CombineAction
    class CombineAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String first = _fName.getText();
            String last = _lName.getText();
            _combinedName.setText(last + ", " + first);
        }//end actionPerformed
    }//end inner class
}//end class CombineNameGUI

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!
  3. 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.