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
|