Properly add all view JComponents to the JPanel in the main JFrame.
[Persons_Comparator.git] / src / MainWindowsView.java
CommitLineData
60971873 1import javax.swing.*;
089fcbfc
JB
2import java.awt.*;
3import java.util.ArrayList;
60971873
JB
4
5public class MainWindowsView extends JFrame {
6 MainWindowsView(String title) {
7 //Create and set up the window.
8 setTitle(title);
089fcbfc
JB
9 setSize(300, 300);
10 setLocationRelativeTo(null);
60971873
JB
11 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12
13 JPanel panel = new PersonView();
14
089fcbfc
JB
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 }
60971873 28
089fcbfc 29 setContentPane(panel);
60971873
JB
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.
089fcbfc 40 //this.pack();
60971873
JB
41 this.setVisible(true);
42 }
43
44}