import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.beans.*; public class WOR_FilePreviewFrame extends JInternalFrame implements ActionListener, PropertyChangeListener { JFileChooser chooser; JTextArea textArea; JLabel commentArea; String fileName = null; String selectedFile = null; protected static final int LINES = 20; protected static final int COLUMNS = 40; static int openFrameCount = 0; static final int offset = 10; private String if_wor = "not-null"; public WOR_FilePreviewFrame() { super("", true, true, true, true); setTitle("File Previewer"); String dir = "./wor"; File dirFile = dir == null ? null : new File(dir); chooser = new JFileChooser(dirFile); chooser.setMultiSelectionEnabled(false); // Create a text area to preview the file textArea = new JTextArea(LINES, COLUMNS); textArea.setEditable(false); textArea.setFont(new Font("Dialog", Font.BOLD, 10)); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new BorderLayout()); controlPanel.add(chooser, "Center"); JPanel commentPanel = new JPanel(); commentPanel.setLayout(new BorderLayout()); commentArea = new JLabel("Select a file...", SwingConstants.LEFT); commentPanel.add(new JSeparator(), "North"); commentPanel.add(commentArea, "South"); controlPanel.add(commentPanel, "South"); JPanel textPanel = new JPanel(); textPanel.setLayout(new BorderLayout()); textPanel.add(new JScrollPane(textArea), "Center"); textPanel.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Preview", TitledBorder.CENTER, TitledBorder.TOP)); setContentPane(controlPanel); pack(); setLocation( offset * openFrameCount, offset * openFrameCount); // Install the text previewer as the JFileChooser accessory chooser.setAccessory(textPanel); // Register to receive file selection events from the chooser chooser.addActionListener(this); // Register to receive property changes from the chooser chooser.addPropertyChangeListener(this); //return f; } // Get the name of the selected file, or null if none selected public String getFileName() { System.out.println( "Filename 3: " + fileName + "selectedFile: " + selectedFile ); return fileName; } // Handle file selected event from the JFileChooser public void actionPerformed(ActionEvent evt) { File selectedFile = chooser.getSelectedFile(); System.out.println( "selectedFile 4: " + selectedFile ); if (selectedFile == null) { return; } else { fileName = selectedFile.getPath(); System.out.println( "Filename 4: " + fileName ); //return selectedFile; } //JInternalFrame doc = new FormFrame16( fileName ); //setContentPane(doc); // Dispose the dialog //setVisible(false); //return fileName; } // Handle property changes from the JFileChooser public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { File selectedFile = (File)evt.getNewValue(); // Single click on something if (selectedFile.isFile() && selectedFile.canRead()) { // It's a file: show the preview previewFile(selectedFile); commentArea.setText("Double-click to select this file."); } else { // Clear text area if not a readable file textArea.setText(""); if (selectedFile.isFile() == false) { commentArea.setText("Not a file."); } else { commentArea.setText("File is not readable."); } } } } protected void previewFile(File f) { textArea.setText(""); try { BufferedReader br = new BufferedReader(new FileReader(f)); for (int i = 0; i < LINES; i++) { String s = br.readLine(); if (s == null) { break; } textArea.append(s); textArea.append("\n"); } br.close(); } catch (Throwable t) { textArea.setText(""); commentArea.setText("Failed to read file contents."); } } }