Properly add all view JComponents to the JPanel in the main JFrame.
[Persons_Comparator.git] / src / MainWindowsView.java
index f4f837f6bbea7f512696dc7f5e1df9daa356caee..065c90d8050f0deb9203ce7733bb26b49b64c029 100644 (file)
@@ -1,18 +1,32 @@
 import javax.swing.*;
+import java.awt.*;
+import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
     MainWindowsView(String title) {
         //Create and set up the window.
         setTitle(title);
+        setSize(300, 300);
+        setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         JPanel panel = new PersonView();
 
-        //TODO: Add content to the panel
-        //JLabel label = new JLabel("Hello World");
-        //panel.add(label);
+        //Get all Swing/AWT primitive components in the views and add them to the panel.
+        ArrayList<Component> components = new ArrayList<>();
+        for (int i = 0; i < panel.getComponentCount(); i++) {
+            if ((panel.getComponent(i) instanceof Container)) {
+                Container subContainer = (Container) panel.getComponent(i);
+                for (int j = 0; j < subContainer.getComponentCount(); j++) {
+                    components.add(subContainer.getComponent(j));
+                }
+            }
+        }
+        for (Component component : components) {
+            panel.add(component);
+        }
 
-        getContentPane().add(panel);
+        setContentPane(panel);
     }
 
     /**
@@ -23,7 +37,7 @@ public class MainWindowsView extends JFrame {
     public void showGUI() {
 
         //Display the window.
-        this.pack();
+        //this.pack();
         this.setVisible(true);
     }