Search code examples
javaswingjdialogjwindow

JPanel on JWindow, adding components


I have a JFrame, inside the JFrame code I create a JWindow and on window I have created a JPanel. On JPanel is inserted a background image.

JButton btnImage= new JButton("My Button");

 Image splashImg = Toolkit.getDefaultToolkit().getImage("images/image1.jpeg");
    
JPanel pnlSplashWindow= new JPanel(){ 

        public void paint(Graphics g){
          g.drawImage(splashImg,0,0,splashImg.getWidth(this),splashImg.getHeight(this),this);
    }
    };
    
    pnlSplashWindow.setLayout(new BorderLayout());
    pnlSplashWindow.add(BorderLayout.SOUTH,btnImage);
    JWindo window= new JWindow(this); // this refers to my class which has extended JFrame

    window.setContentPane(pnlSplashWindow);
    window.setSize(688, 344);
    btnImg.setVisible(true);
    window.setLocationRelativeTo(this);

I am new to JWindow and have the following questions:

  1. How to add elements like buttons and labels on JWindow (or JPanel which is on the JWindow)?
  2. How to set my JFrame as the parent of this JWindow? I mean while JWindow is active, the JFrame should not be clickable.

An example of the desired end effect

Example of splash


Solution

  • To add components you should use:

    pnlSplashWindow.add(btnImage, BorderLayout.SOUTH);
    

    instead. And if you don't want your JFrame to be clickable, you should use a modal JDialog , by extending JDialog instead of JWindow.

    But if you want to create a Splash Screen, you should read How to create a Splash Screen.