'JInternalFrame won't appear on my screen

I am trying to get a JInternalFrame to appear on my screen when a button is pressed, a pop up effect basically. However when the button is pressed the JInternalFrame does not appear on the screen. Also when I resize the screen all the elements expand with it, I am wondering if there is a way to get a pop up window to appear on the screen and keep the layout manager I have now still in place so that when the window is resized the elements are also resized with it

public class testing2 implements ActionListener {

JButton buttonAppear = new JButton();
JLayeredPane LayeredPane = new JLayeredPane();


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

public testing2() {



    LayeredPane = new JLayeredPane();
            BorderLayout borderlayoutpane = new BorderLayout();
            LayeredPane.setLayout(borderlayoutpane);
            JPanel mainPanel = new JPanel();
            BorderLayout borderlayout = new BorderLayout();
            mainPanel.setLayout(borderlayout);
            JButton button = new JButton("Button");
            mainPanel.add(button, "Center");
            buttonAppear = new JButton("Panel Appear");
            buttonAppear.addActionListener(this);
            mainPanel.add(buttonAppear, "South");
            LayeredPane.add(mainPanel, 2);
            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(LayeredPane);
            frame.setSize(400, 400);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }



@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource() == buttonAppear)
    {
           JInternalFrame inFrame = new JInternalFrame("Internal Frame", true, true, true, true);
           inFrame.setBounds(10, 10, 200, 200);              
           inFrame.setVisible(true);
           LayeredPane.add(inFrame, 1);
    }

}

}


Solution 1:[1]

a pop up effect basically.

Then use a JDialog. A JInternalFrame was designed to work with a JDesktopPane.

mainPanel.add(button, "Center");

Don't use hardcode strings for the constraint. Use the field provided by the API:

mainPanel.add(button, BorderLayout.CENTER);

Also, follow Java naming conventions. Variable names should NOT start with an upper case character. Be consistent.

Don't know if it will make a difference but components with a higher layer number are painted on top of components with a lower index. So I would guess the panel (which is opaque) would just paint over top of the internal frame. Read the section from the Swing tutorial on How to Use Layered Panes. Also read the section on How to Use Root Panes to find the special variable for "popups" on a layered pane.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1