Search code examples
javaswinguser-interfacefullscreenjinternalframe

JInternalFrame in full-screen mode


I intend to use a JInternalFrame as a modal JDialog in full-screen mode, however, it is not currently being shown when it gets called. Do I need to add it to some container? I tried adding it to a JOptionPane.showInternalMessage(...), but since I want to make the dialog to go away automatically after 3 seconds, this would not work, as the the JOptionPane dialog would just stay there until someone clicks OK.

Any idea? Many thanks.


Solution

  • Yes you have to add it to a container in which you want it to be 'internal'.

    Below you have an example which shows you this behaviour. You can notice here that the internal frame (which will appear/hide on mouse press inside the frame) is not painted over the EAST panel, as it is clipped when it goes outside the borders of its parent.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class InternalFrameTest extends JPanel
    {
        private static final long serialVersionUID = 1L;
        private JInternalFrame internalFrame;
        public InternalFrameTest()
        {
            this.internalFrame = new JInternalFrame("Internal frame");
            internalFrame.setLayout(new FlowLayout());
            internalFrame.add(new JLabel("I am label"));
            internalFrame.add(new JButton("Oi button"));    
            internalFrame.pack();
            add(internalFrame);
        }
    
        public void showHideInternalFrame()
        {
            internalFrame.setVisible(!internalFrame.isVisible());
        }
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {               
                    final InternalFrameTest ift = new InternalFrameTest();
                    ift.setBackground(Color.GREEN);
                    JFrame f = new JFrame();
                    f.addMouseListener(new MouseAdapter() 
                    {
                        @Override
                        public void mousePressed(MouseEvent e)
                        {
                            super.mousePressed(e);
                            ift.showHideInternalFrame();
                        }                   
                    });
                    JPanel cp = new JPanel(new BorderLayout());
                    cp.add(ift);
                    JPanel eastP = new JPanel();
                    eastP.add(new JLabel("EAST"));
                    eastP.setBackground(Color.YELLOW);
                    cp.add(eastP, BorderLayout.EAST);
                    f.setContentPane(cp);
                    f.setSize(400, 300);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }