Solutions to the Try It Out Exercises in Java For Dummies, 8th Edition
by Barry Burd

Chapter 16: Java's Juggling Act

In this chapter:

Mark for Removal

This Is It

The first use of the keyword this forces the expression this.n to refer to the field named n, as in
private int n;
instead of referring to the parameter named n, as in
IntegerHolder(int n)
The second use of the keyword this refers to an instance of the IntegerHolder class (whichever instance is executing its own displayMyN method at the time). This instance gets passed to the Displayer class's display method. In the display method's body, that instance goes by the parameter name holder. The display method calls that holder's getN method. In other words, the display method calls the IntegerHolder instance's getN method.

Copy Cat

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame implements ActionListener {

   JTextField textField = new JTextField(5);
   JButton button = new JButton("Copy");
   JLabel label = new JLabel("(the label)");

   public Copier() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      add(textField);
      add(button);
      add(label);
      button.addActionListener(this);
      pack();
      setVisible(true);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      String textFieldText = textField.getText();
      label.setText(textFieldText);
   }
}


public class ShowCopierFrame {

   public static void main(String[] args) {
       new Copier();
   }
}

Copy When the User Types

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame implements KeyListener {
  private static final long serialVersionUID = 1L;

  JTextField textField = new JTextField(5);
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    textField.addKeyListener(this);
    add(label);
    pack();
    setVisible(true);
  }

  @Override
  public void keyTyped(KeyEvent e) {
  }

  @Override
  public void keyPressed(KeyEvent e) {
  }

  @Override
  public void keyReleased(KeyEvent e) {
    label.setText(textField.getText());
  }

}

Maintain Anonymity

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame {
  JTextField textField = new JTextField(5);
  JButton button = new JButton("Copy");
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    add(button);
    add(label);
    button.addActionListener(new MyActionListener());
    pack();
    setVisible(true);
  }

  class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
      String textFieldText = textField.getText();
      label.setText(textFieldText);
    }
  }
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame {
  private static final long serialVersionUID = 1L;

  JTextField textField = new JTextField(5);
  JButton button = new JButton("Copy");
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    add(button);
    add(label);

    button.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        String textFieldText = textField.getText();
        label.setText(textFieldText);
      }
    });

    pack();
    setVisible(true);
  }
}