Java Reference
In-Depth Information
} catch (IOException exception) {
System.err.println("Error closing writer");
exception.printStackTrace();
}
}
}
}
public static void doLoadCommand(JTextComponent textComponent,
String filename) {
FileReader reader = null;
try {
reader = new FileReader(filename);
textComponent.read(reader, filename);
} catch (IOException exception) {
System.err.println("Load oops");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}
}
}
}
■
Note
By default, file reading and writing deals only with plain text. If the contents of a text component are
styled, the styled attributes aren't saved. The
EditorKit
class can customize this loading and saving behavior.
You'll explore that class in Chapter 16.
Accessing the Clipboard
To use the system clipboard for cut, copy, and paste operations, you do not need to manually
concoct a
Transferable
clipboard object. Instead, you just call one of these three methods of
the
JTextComponent
class:
public void cut()
,
public void copy()
, or
public void paste()
.
You could call these methods directly from
ActionListener
implementations associated
with buttons or menu items, as in the following: