Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboPopup;

public class Main extends JPanel {

    private JComboBox comboBox;

    public Main() {
        comboBox = new JComboBox(new String[] { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" });
        add(comboBox, BorderLayout.PAGE_START);
        JFrame frame = new JFrame("Main");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);

        comboBox.showPopup();
        Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
        BasicComboPopup popup = (BasicComboPopup) child;
        popup.setName("BasicComboPopup");
        JList list = popup.getList();
        Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
        JScrollPane scrollPane = (JScrollPane) c;

        Window mainFrame = SwingUtilities.windowForComponent(comboBox);

        System.out.println(mainFrame.getName());
        Window popupWindow = SwingUtilities.windowForComponent(popup);
        System.out.println(popupWindow);
        Window popupWindowa = SwingUtilities.windowForComponent(c);
        System.out.println(popupWindowa);

        Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox);

        System.out.println(mainFrame1);
        Window popupWindow1 = SwingUtilities.getWindowAncestor(popup);
        System.out.println(popupWindow1);

        Component mainFrame2 = SwingUtilities.getRoot(comboBox);

        System.out.println(mainFrame2.getName());
        Component popupWindow2 = SwingUtilities.getRoot(popup);
        System.out.println(popupWindow2);

        if (popupWindow != mainFrame) {
            popupWindow.pack();
        }
    }

    public static void main(String[] args) {
        Main Main = new Main();
    }
}