Home : Course Map : Chapter 6 : Java :
Text Display
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

So far we discussed how to use JLabel, JTextField, JTextArea to display text. The JLabel component provides for the display of brief text (it can also display an icon; see the JLabel specifications) to help explain the workings of a user interface. The JTextField displays a single line while JTextArea provides for a multi-line display.

JTextField and JTextArea also allow users to modify the text, which can then become the input for processing of some kind.

In most of the examples in Chapter 1-5 we used System.out.println() to send string output to the console. It would obviously be much more user friendly to send the program output to a graphical interface.

In the example below we use a JTextArea to display text to take the place of the console. We put the JTextArea component on a JPanel subclass called TextOutputPanel. (We also put it into a JScrollPane to allow for scroll bars when text goes beyond the boundaries.)

We devise an interface called Outputable, which holds two methods: print(String) and println(String):

Outputable.java
public interface Outputable
{
   static final char CR = '\n';

   // A method to print a string
   public void print (String str);

   // A method to print a string with a carriage return
   public void println (String str);
}

 

The TextOutputPanel class below implements Outputable. Its methods print(String) and println(String) methods send text to the JTextArea.

TextOutputPanel.java


public class
TextOutputPanel extends JPanel implements Outputable
{
  // A Swing textarea for display of string info
  JTextArea textArea = null;
  public TextOutputPanel () {
    setLayout (new BorderLayout ());

    // Create an instance of JTextArea
    textArea = new JTextArea ();
    textArea.setEditable (false);

    // Add to a scroll pane so that a long list of
    // computations can be seen.

    JScrollPane areaScrollPane = new JScrollPane (textArea);
    add (areaScrollPane,"Center");
  }

  public void println (String str) {
    textArea.append (str + CR);
  }

  public void print (String str) {
    textArea.append (str);
  }
}

 

The applet below uses the TextOutputPanel component to display text :


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

/** Use a JPanel subclass TextOutputPanel to show text
  * output on the applet area.
 **/

public class
TextOutputApplet extends JApplet
{
  public void init() {   
    Container contentPane = getContentPane ();

    // Create an instance of the panel subclass
    // with JTextArea to show output

    TextOutputPanel output = new TextOutputPanel ();

    // Add the panel to the applet's pane
    contentPane.add (output);

    // Send text to the JTextArea.
    output.println (
      "TextOutputPanel implements the Outputable interface");
    output.println (
      "Outputable methods are print(String) & println(String)");
  }
}

This example nicely illustrates the convenience of interfaces. While only a single class can be extended, there are no limits on the number of interfaces that can be implemented. So we can always modify a class to implement an interface when we want to provide new capabilities.

As exercises, the student should modify some of the Chapter 1-5 example programs such that they implement the Outputable interface in a similar manner and send string output to a text area rather than the console.

 

Latest update: Oct. 25, 2004

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.