Implement the person comparator :
[Persons_Comparator.git] / src / MainWindowsView.java
... / ...
CommitLineData
1import javax.swing.*;
2import javax.swing.text.DefaultEditorKit;
3import java.awt.*;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.awt.event.KeyEvent;
7import java.util.ArrayList;
8
9public class MainWindowsView extends JFrame {
10
11 MainWindowsView(String title, JPanel panel) {
12 //Create and set up the window.
13 setTitle(title);
14 setSize(panel.getPreferredSize());
15 setLocationRelativeTo(null);
16 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17
18 //Create menu
19 JMenuBar menuBar = createMenuBar();
20 setJMenuBar(menuBar);
21
22 //Get all Swing/AWT JPanel in the views and add them to the main panel.
23 ArrayList<Component> components = new ArrayList<>();
24 for (int i = 0; i < panel.getComponentCount(); i++) {
25 components.add(panel.getComponent(i));
26 }
27 for (Component component : components) {
28 panel.add(component);
29 }
30
31 setContentPane(panel);
32 }
33
34 /**
35 * Create menu that support cut/copy/paste.
36 */
37 private JMenuBar createMenuBar() {
38 JMenuItem menuItem;
39 JMenuBar menuBar = new JMenuBar();
40
41 JMenu fileMenu = new JMenu("File");
42
43 menuItem = new JMenuItem("Exit");
44 menuItem.addActionListener(new ActionListener() {
45 public void actionPerformed(ActionEvent e) {
46 System.exit(0);
47 }
48 });
49 fileMenu.add(menuItem);
50
51 JMenu editMenu = new JMenu("Edit");
52 editMenu.setMnemonic(KeyEvent.VK_E);
53
54 menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
55 menuItem.setText("Cut");
56 menuItem.setMnemonic(KeyEvent.VK_T);
57 editMenu.add(menuItem);
58
59 menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
60 menuItem.setText("Copy");
61 menuItem.setMnemonic(KeyEvent.VK_C);
62 editMenu.add(menuItem);
63
64 menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
65 menuItem.setText("Paste");
66 menuItem.setMnemonic(KeyEvent.VK_P);
67 editMenu.add(menuItem);
68
69 menuBar.add(fileMenu);
70 menuBar.add(editMenu);
71 return menuBar;
72 }
73
74 /**
75 * Show the GUI. For thread safety,
76 * this method should be invoked from the
77 * event-dispatching thread.
78 */
79 public void showGUI() {
80
81 //Display the window.
82 //this.pack();
83 this.setVisible(true);
84 }
85}