Java Reference
In-Depth Information
Figure 15-6 shows a sample program that uses the loading and saving capabilities, with
these options implemented through buttons (although Load and Save options are more
commonly seen under a File menu). The Clear button clears the contents of the text field.
Figure 15-6. Loading and saving a text component
The source in Listing 15-3 puts all the pieces together in a sample program to demonstrate
loading and saving streams.
Listing 15-3. Loading and Saving Streams with a JTextComponent
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
public class LoadSave {
public static void main (String args[]) {
Runnable runner = new Runnable() {
public void run() {
final String filename = "text.out";
JFrame frame = new JFrame("Loading/Saving Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField textField = new JTextField();
frame.add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel();
// Setup actions
Action loadAction = new AbstractAction() {
{
putValue(Action.NAME, "Load");
}
public void actionPerformed(ActionEvent e) {
doLoadCommand(textField, filename);
}
};
 
Search WWH ::




Custom Search