Add an ArrayList of persons to the Person class.
[Persons_Comparator.git] / src / MainWindowsView.java
CommitLineData
60971873 1import javax.swing.*;
089fcbfc 2import java.awt.*;
416e35f7
JB
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
089fcbfc 5import java.util.ArrayList;
60971873
JB
6
7public class MainWindowsView extends JFrame {
f1e9d6d2 8
b974e749 9 MainWindowsView(String title, JPanel panel) {
60971873
JB
10 //Create and set up the window.
11 setTitle(title);
089fcbfc
JB
12 setSize(300, 300);
13 setLocationRelativeTo(null);
60971873
JB
14 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15
416e35f7
JB
16 //Create menu
17 JMenuBar menuBar = new JMenuBar();
18 setJMenuBar(menuBar);
19 JMenu fileMenu = new JMenu("File");
20 menuBar.add(fileMenu);
21 JMenuItem exit = new JMenuItem("Exit");
22 exit.addActionListener(new ActionListener() {
23 public void actionPerformed(ActionEvent e) {
24 System.exit(0);
25 }
26 });
27 fileMenu.add(exit);
28
089fcbfc
JB
29 //Get all Swing/AWT primitive components in the views and add them to the panel.
30 ArrayList<Component> components = new ArrayList<>();
31 for (int i = 0; i < panel.getComponentCount(); i++) {
32 if ((panel.getComponent(i) instanceof Container)) {
33 Container subContainer = (Container) panel.getComponent(i);
34 for (int j = 0; j < subContainer.getComponentCount(); j++) {
35 components.add(subContainer.getComponent(j));
36 }
37 }
38 }
39 for (Component component : components) {
40 panel.add(component);
41 }
60971873 42
089fcbfc 43 setContentPane(panel);
60971873
JB
44 }
45
46 /**
47 * Show the GUI. For thread safety,
48 * this method should be invoked from the
49 * event-dispatching thread.
50 */
51 public void showGUI() {
52
53 //Display the window.
089fcbfc 54 //this.pack();
60971873
JB
55 this.setVisible(true);
56 }
416e35f7 57}