3

I want to have a JPanel which uses an image as a background, with this I want to add new panels to this panels so that they sit on top of this background image. I have tried the following:

Image background;
 public Table(){
  super();
   ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTable.png"));
      background = ii.getImage();
      setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
 }
 @Override
 protected void paintComponent(Graphics g)
 {
  super.paintComponent(g); 
  if (background != null){
        g.drawImage(background, 0,0,this.getWidth(),this.getHeight(),this);
  }

      JButton button = new JButton("hello world");

      JPanel OverlayedPanel1 = new JPanel();
      OverlayedPanel1.setMinimumSize(new Dimension(600,50));
      OverlayedPanel1.setMaximumSize(new Dimension(600,50));
      OverlayedPanel1.setPreferredSize(new Dimension(600,50));
         OverlayedPanel1.add(button, BorderLayout.CENTER);
      OverlayedPanel1.setBackground(Color.yellow);

  }

The background image is displayed but the OverlayedPanel1 doesnt show. Any ideas?

Aly
  • 15,865
  • 47
  • 119
  • 191
  • Bad idea to create Swing objects in paint()! You should create them in the Table constructor or some method of your class. – PhiLho Jan 26 '10 at 17:03
  • Yes sorry I just put that there to demonstrate the example – Aly Jan 26 '10 at 17:05
  • you are not painting the panel, you are just creating it. Which kind of object is Table? You should add the panel to the Table object (but not in the paint() method) – Fortega Jan 26 '10 at 17:20

1 Answers1

2

You didn't add OverlayedPanel1 to the panel.

add(OverlayedPanel1);
Samir Talwar
  • 14,220
  • 3
  • 41
  • 65