/** Creates the EditorLite object and displays the GUI with an empty editor pane. */ public EditorLite() { /* Scrollable Text Panes */ // create some text areas // these are dimensionless to let the layout handle it editor = new JTextArea(); editor.setLineWrap(true); editor.setWrapStyleWord(true); editor.addKeyListener(this); side = new JTextArea(); side.setLineWrap(true); side.setEditable(false); // create the scroll panes (to house the text areas) editorPane = new JScrollPane(editor); sidePane = new JScrollPane(side); centerPanel = new JPanel(new GridLayout(1, 2)); centerPanel.add(editorPane); centerPanel.add(sidePane); /* Button Panel */ // create the buttonPanel buttonPanel = new JPanel(new FlowLayout()); // create the buttons fileChooserButton = new JButton("Open File"); sideButton = new JButton("Copy to Side Pane"); // add action listeners to the buttons fileChooserButton.addActionListener(this); sideButton.addActionListener(this); // add the buttons to the buttonPanel buttonPanel.add(fileChooserButton); buttonPanel.add(sideButton); /* Menu Bar */ // create a menu bar menuBar = new JMenuBar(); // create menus to add to the menu bar // also setup mnemonics for keyboard access fileMenu = new JMenu("File"); editMenu = new JMenu("Edit"); formatMenu = new JMenu("Format"); viewMenu = new JMenu("View"); helpMenu = new JMenu("Help"); fileMenu.setMnemonic(KeyEvent.VK_F); editMenu.setMnemonic(KeyEvent.VK_E); formatMenu.setMnemonic(KeyEvent.VK_O); viewMenu.setMnemonic(KeyEvent.VK_V); helpMenu.setMnemonic(KeyEvent.VK_H); fileMenu.setDisplayedMnemonicIndex(0); editMenu.setDisplayedMnemonicIndex(0); formatMenu.setDisplayedMnemonicIndex(1); viewMenu.setDisplayedMnemonicIndex(0); helpMenu.setDisplayedMnemonicIndex(0); // create menu items to add to the menus // also setup mnemonics for keyboard access // note: \u2026 is a unicode ellipsis (ie, '...') newFile = new JMenuItem("New", KeyEvent.VK_N); open = new JMenuItem("Open\u2026", KeyEvent.VK_O); save = new JMenuItem("Save", KeyEvent.VK_S); saveAs = new JMenuItem("Save As\u2026", KeyEvent.VK_A); close = new JMenuItem("Exit", KeyEvent.VK_X); newFile.setDisplayedMnemonicIndex(0); open.setDisplayedMnemonicIndex(0); save.setDisplayedMnemonicIndex(0); saveAs.setDisplayedMnemonicIndex(5); close.setDisplayedMnemonicIndex(1); newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); // disable the save JMenuItem by default save.setEnabled(false); cut = new JMenuItem("Cut", KeyEvent.VK_T); copy = new JMenuItem("Copy", KeyEvent.VK_C); paste = new JMenuItem("Paste", KeyEvent.VK_P); deleteSelected = new JMenuItem("Delete", KeyEvent.VK_L); cut.setDisplayedMnemonicIndex(2); copy.setDisplayedMnemonicIndex(0); paste.setDisplayedMnemonicIndex(0); deleteSelected.setDisplayedMnemonicIndex(2); cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); deleteSelected.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); wordWrap = new JCheckBoxMenuItem("Word Wrap", true); wordWrap.setMnemonic(KeyEvent.VK_W); wordWrap.setDisplayedMnemonicIndex(7); toggleSide = new JCheckBoxMenuItem("Side Pane", true); toggleSide.setMnemonic(KeyEvent.VK_S); toggleSide.setDisplayedMnemonicIndex(7); about = new JMenuItem("About " + programName, KeyEvent.VK_A); about.setDisplayedMnemonicIndex(0); // add action listeners to the menu items newFile.addActionListener(this); open.addActionListener(this); save.addActionListener(this); saveAs.addActionListener(this); close.addActionListener(this); cut.addActionListener(this); copy.addActionListener(this); paste.addActionListener(this); deleteSelected.addActionListener(this); wordWrap.addItemListener(this); toggleSide.addItemListener(this); about.addActionListener(this); // add the menu items to the menus fileMenu.add(newFile); fileMenu.add(open); fileMenu.add(save); fileMenu.add(saveAs); fileMenu.add(close); editMenu.add(cut); editMenu.add(copy); editMenu.add(paste); editMenu.add(deleteSelected); formatMenu.add(wordWrap); viewMenu.add(toggleSide); helpMenu.add(about); // add the menus to the menu bar menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(formatMenu); menuBar.add(viewMenu); menuBar.add(helpMenu); /* Main Frame */ // initialize mainFrame = new JFrame("EditorLite"); // add the listener for windowClosing event mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); mainFrame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent winEvt) { if (isUnsavedFile()) { int choice = displayUnsavedWarning(); if (choice == JOptionPane.YES_OPTION) { System.exit(0); } } else { System.exit(0); } } }); // set up the layout mainFrame.setJMenuBar(menuBar); mainFrame.add(centerPanel, BorderLayout.CENTER); mainFrame.add(buttonPanel, BorderLayout.SOUTH); // pack and set a reasonable start size mainFrame.pack(); mainFrame.setSize(600, 400); // center the window on the screen // basically, it gets the width of the screen, subtracts the width of the frame, // and then divides by 2 for the x coordinate int xCoord = (Toolkit.getDefaultToolkit().getScreenSize().width - mainFrame.getSize().width) / 2; // similarly, it gets the height of the screen, subtracts the height of the frame, // and then divides by 2 for the y coordinate int yCoord = (Toolkit.getDefaultToolkit().getScreenSize().height - mainFrame.getSize().height) / 2; mainFrame.setLocation(xCoord, yCoord); // finally, make it visible mainFrame.setVisible(true); }