import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; /** An example class used to demonstrate the use of * the JTextField and JTextArea components */ public class TextExamples extends JFrame { /** Class constructor method * @param titleText Window's title bar text */ public TextExamples( String titleText ) { super( titleText ); addWindowListener( new WindowCloser() ); final JTextArea upper = new JTextArea(); final JTextArea lower = new JTextArea( upper.getDocument() ); JScrollPane upperScroll = new JScrollPane( upper ); JScrollPane lowerScroll = new JScrollPane( lower ); JSplitPane sp = new JSplitPane( JSplitPane.VERTICAL_SPLIT, upperScroll, lowerScroll ); sp.setOneTouchExpandable( true ); sp.setDividerLocation( 0.5 ); final JTextField fName = new JTextField(); fName.setToolTipText( "Enter a file name" ); fName.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { if ( fName.getText() != null ) try { FileReader fr = new FileReader( fName.getText() ); upper.setText( "" ); char[] buffer = new char[4096]; int count = fr.read( buffer, 0, buffer.length ); while ( count != -1 ) { upper.append( new String( buffer, 0, count ) ); count = fr.read( buffer, 0, buffer.length ); } } catch( IOException ioe ) { System.out.println( ioe ); } } } ); JPanel entry = new JPanel( new BorderLayout() ); entry.add( new JLabel( "File: " ), BorderLayout.WEST ); entry.add( fName, BorderLayout.CENTER ); getContentPane().add( entry, BorderLayout.NORTH ); getContentPane().add( sp, BorderLayout.CENTER ); setSize( 500, 400 ); fName.requestFocus(); setVisible( true ); } /** The test method for the class * @param args not used */ public static void main( String[] args ) { new TextExamples( "Example Text Components" ); } }