newbie
newbie

Reputation: 14960

JPanel on top of JLabel

Good day!

Is it possible to add a JPanel on top of a JLabel?

I would like my JFrame to have a background image and in order to this, i used this code (based from past stackoverflow answers):

 setLocation(150,50);
 setSize(700,650);
 setVisible(true);
 JLabel contentPane = new JLabel();
 contentPane.setIcon(new ImageIcon("pics/b1.jpg"));
 contentPane.setLayout( new BorderLayout());
 setContentPane( contentPane );

Now my problem is, I cannot put a panel on my JFrame because of the JLabel background. Please help.

Thanks.

Upvotes: 1

Views: 2794

Answers (4)

camickr
camickr

Reputation: 324207

I cannot put a panel on my JFrame because of the JLabel background

Thats because a panel is opague so it paints over top of the label. You need to use:

panel.setOpaque( false );

Upvotes: 2

Suraj
Suraj

Reputation: 96

sure..you can....Use NetBEans IDE to simplify tedious tasks like this by drag and drop and write the actual programming..

Upvotes: 0

user489041
user489041

Reputation: 28312

You could use a JLayeredPane. This lets you add components to different layers and have them ontop of one another.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

To create a background image for a JFrame, I recommend that you draw the image in the paintComponent method of a JPanel, and then add this JPanel to the contentPane BorderLayout.CENTER which has it fill the contentPane. You may even want to set the JPanel's preferredSize to be that of the Image. Then you can add any components you'd like to the image panel, and don't have to worry about trying to add comopnents to a JLabel which seems bass ackwards to me.

For example here's a program that does this but slightly different. It creates an ImagePanel object, a JPanel that draws an image and sizes itself to the image and then places it in a JScrollPane which is then added to the contentPane, but you can just get rid of the JScrollPane and put your image JPanel directly in the contentPane instead:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class BigDukeImage {
   public static final String IMAGE_PATH = "http://" + "duke.kenai.com/nyanya/NyaNya.jpg";
   private static final Dimension SCROLLPANE_SIZE = new Dimension(900, 700);

   private static void createAndShowUI() {
      Image image = null;
      try {
         URL url = new URL(IMAGE_PATH);
         image = ImageIO.read(url);
         // JLabel label = new JLabel(new ImageIcon(image));
         ImagePanelA imagePanel = new ImagePanelA(image);

         JScrollPane scrollpane = new JScrollPane();
         // scrollpane.getViewport().add(label);
         scrollpane.getViewport().add(imagePanel);
         scrollpane.setPreferredSize(SCROLLPANE_SIZE);

         JFrame frame = new JFrame("Big Duke Image");
         frame.getContentPane().add(scrollpane);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);

         JScrollBar vertSBar = scrollpane.getVerticalScrollBar();
         JScrollBar horzSBar = scrollpane.getHorizontalScrollBar();

         vertSBar.setValue((vertSBar.getMaximum() - vertSBar.getVisibleAmount()) / 2);
         horzSBar.setValue((horzSBar.getMaximum() - horzSBar.getVisibleAmount()) / 2);

      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

@SuppressWarnings("serial")
class ImagePanelA extends JPanel {

   private Image image;

   public ImagePanelA(Image image) {
      this.image = image;
      setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
   }
}

Upvotes: 3

Related Questions