/** * Simple demonstration class to show how to display a text * file in a JTextPane in a Swing program using a JFileChooser * * @author David Mitchell, dave@dmdata.com * @version 1.0 * @see http://www.dbmdata.com */ import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.io.*; /** * * * @author David Mitchell, dave@dbmdata.com * @version 1.0 * @see http://www.dbmdata.com */ public class FileReaderWindow extends JFrame { // declare GUI variables public Container c; private BorderLayout m_brd; private JPanel mainPanel; public BorderLayout brd; // for the textpane window public JTextPane textPane; private JScrollPane textScroll; // for the control buttons private JPanel controlPanel; private JButton btnOpen; private JButton btnQuit; /** * Class constructor * Start building some of the Swing GUI elements * * @param * @return * @exception * * @see */ public FileReaderWindow() { super("FileReaderWindow"); c = getContentPane(); // set up the main panel that will contain the entire interface m_brd = new BorderLayout(); mainPanel = new JPanel(); mainPanel.setLayout(m_brd); textPane = new JTextPane(); textScroll = new JScrollPane(textPane); textScroll.setPreferredSize(new Dimension(300, 300)); controlPanel = buildControlPanel(); mainPanel.add(textScroll, m_brd.CENTER); brd = new BorderLayout(); c.setLayout(brd); // start listening for events now that the interface is showing addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); c.add(mainPanel, brd.CENTER); c.add(controlPanel, brd.SOUTH); pack(); show(); } /** * * builds a panel of buttons to add to the main panel * and returns it to the the Constructor * * @param none * @return tempControlPanel * @exception none * * @see */ private JPanel buildControlPanel() { JPanel tempControlPanel = new JPanel(); btnOpen = new JButton("Open File"); btnQuit= new JButton("Quit"); tempControlPanel.add(btnOpen); tempControlPanel.add(btnQuit); /** * Event handler for "Open" button * will call the method to display the JFileChooser */ btnOpen.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { showFileChooser(); } }); /** * Event handler for "quit" button */ btnQuit.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { System.exit(0); } }); return tempControlPanel; } /** * displays JFileFileChooser and grabs the selected file * * @param * @return * @exception * * @see */ public void showFileChooser() { JFileChooser fc = new JFileChooser(); int retr = fc.showOpenDialog(this); File selectedFile = null; if (retr == JFileChooser.APPROVE_OPTION) { selectedFile = fc.getSelectedFile(); } // instantiate the FileRead class which will open the file, read it // and place it's contents into the textPane FileRead fr = new FileRead(selectedFile, this); } /** * Main method - start program * * @param * @return * @exception * * @see */ public static void main (String[] args) { FileReaderWindow remote = new FileReaderWindow(); } }