/********************************* * WebEdit.java * From http://home.att.net/~gobruen * * Very simple class for editing * files that have already been created * * **********************************/ import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; public class WebEdit extends JFrame implements ActionListener { private JTextArea display = new JTextArea(); private JButton readButton = new JButton("Open and Edit Existing File"), writeButton = new JButton("Save"); private JTextField nameField = new JTextField(20); private JLabel prompt = new JLabel("File Name:", JLabel.RIGHT); private JPanel commands = new JPanel(); public WebEdit() { super("Web Edit"); readButton.addActionListener(this); writeButton.addActionListener(this); commands.setLayout(new GridLayout(2,2,1,1)); readButton.setForeground(Color.blue); writeButton.setForeground(Color.green.darker()); readButton.setFont(new Font("TimesRoman", Font.BOLD,10)); writeButton.setFont(new Font("TimesRoman", Font.BOLD,10)); prompt.setFont(new Font("TimesRoman", Font.BOLD, 10)); nameField.setToolTipText("Type the name of a file to read or write"); nameField.setFont(new Font("TimesRoman", Font.BOLD, 10)); display.setFont(new Font("TimesRoman", Font.BOLD, 10)); commands.add(prompt); commands.add(nameField); commands.add(readButton); commands.add(writeButton); display.setLineWrap(true); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add("North", commands); c.add(new JScrollPane(display)); c.add("Center", display); } //Read text file private void readTextFile(JTextArea display, String fileName) { try { BufferedReader inStream = new BufferedReader(new FileReader(fileName)); String line = inStream.readLine(); while(line != null) { display.append(line+"\n"); line = inStream.readLine(); } inStream.close(); }catch(FileNotFoundException e) { display.setText("IOError: no file: " +fileName+ "\n"); e.printStackTrace(); } catch(IOException e) { display.setText("IOERROR: "+e.getMessage()+"\n"); e.printStackTrace(); } }//end readTextFile //write text file private void writeTextFile(JTextArea display, String fileName){ try { FileWriter outStream = new FileWriter(fileName); outStream.write (display.getText()); outStream.close(); JOptionPane.showMessageDialog(null, "File "+fileName+" was successfully created!"); }catch(IOException e) { display.setText("IOERROR: "+e.getMessage()+"\n"); e.printStackTrace(); } } //action performed public void actionPerformed(ActionEvent evt) { String fileName = nameField.getText(); fileName = "c:\\windows\\desktop\\myWeb\\"+fileName+".html"; if(evt.getSource() == readButton) { display.setText(""); readTextFile(display, fileName); } else { writeTextFile(display, fileName); } } /* public static void main(String args[]) { WebEdit we = new WebEdit(); we.setSize(500, 500); we.setVisible(true); we.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }//end main */ }//end class