Java Reference
In-Depth Information
JEditorPane() constructs an editor pane.
read(Reader myReader, Object description) reads the text supplied by
myReader and displays it in the editor area. In the second argument descrip-
tion further information on the type of text can be given; we do not use this
here, i.e. we set description = null .
getText() returns the text currently displayed in the editor area as one string,
including the line-end characters.
The following listing TextDisplayFrame implements a text display. We derive a
TextDisplayFrame from SimpleFrame and glue a JEditorPane into it as a cen-
tral component. The constructor gets a file name as a string. It constructs a file
readfile with this name. As in Section 10.1.3 a FileReader for file readfile
is created. This reader is then passed to the read -method of JEditorPane . The
pane then starts the reader and the text is displayed in the JEditorPane . Class
TextDisplayDriver is just the start class.
File: its/TextDisplay/TextDisplayFrame.java
1. package its.TextDisplay;
2.
3. import its.SimpleFrame.SimpleFrame;
4. import java.io.File;
5. import java.io.FileReader;
6. import java.io.IOException;
7. import javax.swing.JEditorPane;
8. import java.awt.BorderLayout;
9.
10. public class TextDisplayFrame extends SimpleFrame
11. {
12.
private JEditorPane textDisplayPane;
13.
14.
public TextDisplayFrame(String filename)
15.
{
16.
textDisplayPane = new JEditorPane();
17.
this .getContentPane().add(textDisplayPane,BorderLayout.CENTER);
18.
this .setSize(200,160);
19.
20.
File readfile = new File(filename);
21.
22.
try {
23.
FileReader fr = new FileReader(readfile);
24.
textDisplayPane.read(fr, null );
25.
} catch (IOException e){
26.
System.out.println("Problems opening or reading "
+readfile.getPath());
Search WWH ::




Custom Search