Properly add all view JComponents to the JPanel in the main JFrame.
[Persons_Comparator.git] / src / MainWindowsView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.util.ArrayList;
4
5 public class MainWindowsView extends JFrame {
6 MainWindowsView(String title) {
7 //Create and set up the window.
8 setTitle(title);
9 setSize(300, 300);
10 setLocationRelativeTo(null);
11 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12
13 JPanel panel = new PersonView();
14
15 //Get all Swing/AWT primitive components in the views and add them to the panel.
16 ArrayList<Component> components = new ArrayList<>();
17 for (int i = 0; i < panel.getComponentCount(); i++) {
18 if ((panel.getComponent(i) instanceof Container)) {
19 Container subContainer = (Container) panel.getComponent(i);
20 for (int j = 0; j < subContainer.getComponentCount(); j++) {
21 components.add(subContainer.getComponent(j));
22 }
23 }
24 }
25 for (Component component : components) {
26 panel.add(component);
27 }
28
29 setContentPane(panel);
30 }
31
32 /**
33 * Show the GUI. For thread safety,
34 * this method should be invoked from the
35 * event-dispatching thread.
36 */
37 public void showGUI() {
38
39 //Display the window.
40 //this.pack();
41 this.setVisible(true);
42 }
43
44 }