From c281942a8705cc295af41cbeb55b864563892a94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 11 Jan 2019 16:52:20 +0100 Subject: [PATCH] Add cut/copy/paste to the main windows menu. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/MainWindowsView.java | 53 ++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index 0b89d5d..3fab8e0 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -1,7 +1,9 @@ import javax.swing.*; +import javax.swing.text.DefaultEditorKit; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.util.ArrayList; public class MainWindowsView extends JFrame { @@ -14,17 +16,8 @@ public class MainWindowsView extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create menu - JMenuBar menuBar = new JMenuBar(); + JMenuBar menuBar = createMenuBar(); setJMenuBar(menuBar); - JMenu fileMenu = new JMenu("File"); - menuBar.add(fileMenu); - JMenuItem exit = new JMenuItem("Exit"); - exit.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - System.exit(0); - } - }); - fileMenu.add(exit); //Get all Swing/AWT JPanel in the views and add them to the main panel. ArrayList components = new ArrayList<>(); @@ -38,6 +31,46 @@ public class MainWindowsView extends JFrame { setContentPane(panel); } + /** + * Create menu that support cut/copy/paste. + */ + public JMenuBar createMenuBar() { + JMenuItem menuItem; + JMenuBar menuBar = new JMenuBar(); + + JMenu fileMenu = new JMenu("File"); + + menuItem = new JMenuItem("Exit"); + menuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + fileMenu.add(menuItem); + + JMenu editMenu = new JMenu("Edit"); + editMenu.setMnemonic(KeyEvent.VK_E); + + menuItem = new JMenuItem(new DefaultEditorKit.CutAction()); + menuItem.setText("Cut"); + menuItem.setMnemonic(KeyEvent.VK_T); + editMenu.add(menuItem); + + menuItem = new JMenuItem(new DefaultEditorKit.CopyAction()); + menuItem.setText("Copy"); + menuItem.setMnemonic(KeyEvent.VK_C); + editMenu.add(menuItem); + + menuItem = new JMenuItem(new DefaultEditorKit.PasteAction()); + menuItem.setText("Paste"); + menuItem.setMnemonic(KeyEvent.VK_P); + editMenu.add(menuItem); + + menuBar.add(fileMenu); + menuBar.add(editMenu); + return menuBar; + } + /** * Show the GUI. For thread safety, * this method should be invoked from the -- 2.34.1