// NewTextFileApplication.java (c) 2006 Kari Laitinen // 2006-02-01 File created. // 2006-02-02 Last modification. // This is a Java Application that demonstrates: // - the use of a JTextArea object // - storing text lines to a text file import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import javax.swing.event.* ; import java.io.* ; // Classes for file handling. class NewTextFileFrame extends JFrame implements DocumentListener, ActionListener, WindowListener { public static final int FRAME_WIDTH = 500 ; public static final int FRAME_HEIGHT = 400 ; JTextArea writing_area ; JLabel file_name_label ; JTextField file_name_field ; JButton save_button ; boolean text_has_to_be_saved_to_file = false ; public NewTextFileFrame() { setTitle( "CREATE A NEW TEXT FILE" ) ; setSize( FRAME_WIDTH, FRAME_HEIGHT ) ; setBackground( Color.white ) ; setLayout( new BorderLayout() ) ; writing_area = new JTextArea( 20, 60 ) ; writing_area.getDocument().addDocumentListener( this ) ; add( "Center", writing_area ) ; file_name_label = new JLabel( " File name:" ) ; file_name_field = new JTextField( "tmp.txt", 20 ) ; file_name_field.getDocument().addDocumentListener( this ) ; save_button = new JButton( "Save" ) ; save_button.setEnabled( false ) ; save_button.addActionListener( this ) ; JPanel operations_panel = new JPanel() ; operations_panel.add( file_name_label ) ; operations_panel.add( file_name_field ) ; operations_panel.add( save_button ) ; add( "South", operations_panel ) ; addWindowListener( this ) ; // When the default close operation is DO_NOTHING_ON_CLOSE // the windowClosing() method determines what actually happens // when the window is being closed. setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ) ; } void process_document_event( DocumentEvent event ) { if ( event.getDocument() == file_name_field.getDocument() ) { // The file name field was modified. It is required that // the length of the file name is at least 5 characters, // and the file name extension is .txt if ( file_name_field.getText().length() < 5 || ! file_name_field.getText().endsWith( ".txt" ) ) { file_name_label.setText( "INVALID FILE NAME: " ) ; } else { file_name_label.setText( " File name:" ) ; } } else if ( event.getDocument() == writing_area.getDocument() ) { save_button.setEnabled( true ) ; text_has_to_be_saved_to_file = true ; } } public void insertUpdate( DocumentEvent event ) { process_document_event( event ) ; } public void removeUpdate( DocumentEvent event ) { process_document_event( event ) ; } public void changedUpdate( DocumentEvent event ) { } // This kind of update is not possible in the case of JTextField public void actionPerformed( ActionEvent event ) { if ( event.getSource() == save_button && ! file_name_label.getText().contains( "INVALID" ) ) { String file_name = file_name_field.getText() ; try { PrintWriter new_text_file = new PrintWriter( new FileWriter( file_name ) ) ; writing_area.write( new_text_file ) ; new_text_file.close() ; save_button.setEnabled( false ) ; text_has_to_be_saved_to_file = false ; } catch ( IOException caught_io_exception ) { System.out.print( "\n\n File error. Cannot write to \"" + file_name + "\".\n" ) ; } } } public void windowClosing( WindowEvent event ) { // If the text on the writing area has not been saved after // last modification, window closing operation will not be // permitted. if ( text_has_to_be_saved_to_file == false ) { setVisible( false ) ; System.exit( 0 ) ; } } // Other methods required by the WindowListener interface: public void windowActivated( WindowEvent event ) {} public void windowClosed( WindowEvent event ) {} public void windowDeactivated( WindowEvent event ) {} public void windowDeiconified( WindowEvent event ) {} public void windowIconified( WindowEvent event ) {} public void windowOpened( WindowEvent event ) {} } public class NewTextFileApplication { public static void main( String[] not_in_use ) { NewTextFileFrame new_text_file_frame = new NewTextFileFrame() ; new_text_file_frame.setVisible( true ) ; } } /**** NOTES: One way to store the text from the writing area to a file is to use the following statements, but these statements do not produce correct line termination characters OD and 0A in Windows. PrintWriter output_file = new PrintWriter( new FileWriter( file_name ) ) ; String text_from_writing_area = writing_area.getText() ; output_file.print( text_from_writing_area ) ; output_file.close() ; Another correct way to store the text lines would be to exploit the Scanner class and println() method in the following way: PrintWriter new_text_file = new PrintWriter( new FileWriter( file_name ) ) ; java.util.Scanner text_to_file = new java.util.Scanner( writing_area.getText() ) ; while( text_to_file.hasNextLine() ) { String text_line_to_file = text_to_file.nextLine() ; new_text_file.println( text_line_to_file ) ; } new_text_file.close() ; ****/