Code import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class Ed extends JFrame { protected static String[] fontNames; protected int selectedFontIndex; protected int fontSize = 14; private JTextArea editbox; private JMenuItem[] mnuFonts; private JCheckBoxMenuItem mnuBold, mnuItalic; private JFileChooser chooser; private File file; private boolean dirty = false; public Ed() { SwingUtilities.invokeLater(new Runnable() { public void run() { initGUI(); } }); } private void initGUI() { // create window for editor JFrame fra = new JFrame("Ed - Text Editor"); fra.setSize(500, 500); // add document area to editor editbox = new JTextArea(); JScrollPane sp = new JScrollPane(editbox); Container c = fra.getContentPane(); c.setLayout(new BorderLayout()); c.add(sp, BorderLayout.CENTER); // get all fonts on system fontNames = GraphicsEnvironment .getLocalGraphicsEnvironment() .getAvailableFontFamilyNames(); // add menu bar to editor fra.setJMenuBar(createMenuBar()); chooser = new JFileChooser(); try { chooser.setCurrentDirectory((new File(".")).getCanonicalFile()); } catch (IOException e2) {} selectedFontIndex = 0; refresh(); startNewDoc(); fra.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (!shouldSave()) return; System.exit(0); } }); fra.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); fra.setVisible(true); } private JMenuBar createMenuBar() { // needs to be final for event handlers final JMenuBar menuBar = new JMenuBar(); // create File menu JMenu mnuFile = new JMenu("File"); mnuFile.setMnemonic('f'); // create File New menu item JMenuItem mnuItem = new JMenuItem(new AbstractAction("New") { public void actionPerformed(ActionEvent e) { if (!shouldSave()) return; startNewDoc(); } }); mnuItem.setMnemonic('n'); mnuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_N, InputEvent.CTRL_MASK)); mnuFile.add(mnuItem); // create File Open... menu item mnuItem = new JMenuItem(new AbstractAction("Open...") { public void actionPerformed(ActionEvent e) { if (!shouldSave()) return; openDoc(); } }); mnuItem.setMnemonic('o'); mnuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_O, InputEvent.CTRL_MASK)); mnuFile.add(mnuItem); // create File Save menu item mnuItem = new JMenuItem(new AbstractAction("Save") { public void actionPerformed(ActionEvent e) { if (!dirty) return; saveFile(false); } }); mnuItem.setMnemonic('s'); mnuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_MASK)); mnuFile.add(mnuItem); // create File Save As... menu item mnuItem = new JMenuItem(new AbstractAction("Save As...") { public void actionPerformed(ActionEvent e) { saveFile(true); } }); mnuItem.setMnemonic('a'); mnuFile.add(mnuItem); mnuFile.addSeparator(); // create File Exit menu item mnuItem = new JMenuItem(new AbstractAction("Exit") { public void actionPerformed(ActionEvent e) { if (!shouldSave()) return; System.exit(0); } }); mnuItem.setMnemonic('x'); mnuFile.add(mnuItem); menuBar.add(mnuFile); ActionListener fontChangeListener = new ActionListener() { public void actionPerformed(ActionEvent e) { refresh(); } }; JMenu mnuFont = new JMenu("Font"); mnuFont.setMnemonic('o'); mnuBold = new JCheckBoxMenuItem("Bold"); mnuBold.setMnemonic('b'); mnuBold.setSelected(false); mnuBold.addActionListener(fontChangeListener); mnuFont.add(mnuBold); mnuItalic = new JCheckBoxMenuItem("Italic"); mnuItalic.setMnemonic('i'); mnuItalic.setSelected(false); mnuItalic.addActionListener(fontChangeListener); mnuFont.add(mnuItalic); mnuFont.addSeparator(); FontMenu fmenu = new FontMenu("Fonts"); mnuFont.add(fmenu); menuBar.add(mnuFont); return menuBar; } private String getDocName() { return file==null ? "Untitled" : file.getName(); } protected void startNewDoc() { editbox.setText(""); file = null; setTitle("Ed - [" + getDocName() + "]"); dirty = false; editbox.getDocument().addDocumentListener(new DocListener()); } protected void openDoc() { if (chooser.showOpenDialog(Ed.this) != JFileChooser.APPROVE_OPTION) return; File f = chooser.getSelectedFile(); if (f == null || !f.isFile()) return; file = f; try { FileReader in = new FileReader(file); editbox.read(in, null); in.close(); setTitle("Ed" + " [" + getDocName() + "]"); } catch (IOException e2) { error(e2, "Error reading file "+file); } dirty = false; editbox.getDocument().addDocumentListener(new DocListener()); } protected boolean saveFile(boolean saveAs) { if (saveAs || file == null) { if (chooser.showSaveDialog(Ed.this) != JFileChooser.APPROVE_OPTION) return false; File f = chooser.getSelectedFile(); if (f == null) return false; file = f; setTitle("Ed" + " [" + getDocName() + "]"); } try { FileWriter out = new FileWriter(file); editbox.write(out); out.close(); } catch (IOException e2) { error(e2, "Error saving file "+file); return false; } dirty = false; return true; } protected boolean shouldSave() { if (!dirty) return true; int ans = JOptionPane.showConfirmDialog(this, "Save changes to " + getDocName() + "?", "Ed", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); if (ans == JOptionPane.YES_OPTION) { if (!saveFile(false)) return false; else return true; } return ans==JOptionPane.NO_OPTION; } protected void refresh() { int fontStyle = Font.PLAIN; if (mnuBold.isSelected()) fontStyle |= Font.BOLD; if (mnuItalic.isSelected()) fontStyle |= Font.ITALIC; Font font = new Font(fontNames[selectedFontIndex], fontStyle, fontSize); editbox.setFont(font); editbox.repaint(); } public void error(Exception e, String message) { e.printStackTrace(); JOptionPane.showMessageDialog(this, message, "Ed", JOptionPane.WARNING_MESSAGE); } public static void main(String argv[]) { Ed app = new Ed(); } // set inside Ed to get access to variable named: dirty class DocListener implements DocumentListener { public void insertUpdate(DocumentEvent e) { dirty = true; } public void removeUpdate(DocumentEvent e) { dirty = true; } public void changedUpdate(DocumentEvent e) { dirty = true; } } class FontMenu extends JMenu { public FontMenu(String name) { super(name); JPanel p = new JPanel(); p.setLayout(new BorderLayout()); final JList fontlist = new JList(fontNames); fontlist.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int ndx = fontlist.locationToIndex(e.getPoint()); selectedFontIndex = ndx; refresh(); } }); JScrollPane scrollList = new JScrollPane(fontlist); p.add(scrollList, BorderLayout.CENTER); add(p); } } }