0

Okay so, I have added an image to a label using this code.

JLabel image = new JLabel();
image.setIcon(new ImageIcon("path here" ));
//image.setLayout( new BorderLayout() );

And added the label to a panel that I already have

JPanel jp1=new JPanel(new FlowLayout());
jp1.add(image);

Note that jp1 also has alot of other buttons. So I simply want the simplest way to add the label "image" as a background of the JPanel "jp1", because with that code, the panel(jp1) sets the image and the buttons next to each others(according to the flowlayout), but I want the the label to be a background. I tried setBackground, but it only takes a color. How can I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahmed
  • 49
  • 1
  • 7
  • Using a label to show a BG image has drawbacks. Better to custom paint the image as in Rob Camick's [Background Panel](https://tips4java.wordpress.com/2008/10/12/background-panel/), see it also for discussion of the pitfalls of using a standard label. – Andrew Thompson Sep 24 '16 at 11:59
  • Quit posting duplicate questions!!! You were given answer in your last question. For example notice how both the comment above and a link in your other question refer to `BackgroundPanel`??? In addition you have been given multiple example of simple painting of the image. Now again you have 3 valid links. There is no reason you can't solve your problem. – camickr Sep 24 '16 at 13:44

1 Answers1

2

Try any of these:

If label is used only for image, then you could avoid using it altogether.

Community
  • 1
  • 1
  • The label is used only for the image, yes. can I add the image directly as a background for the panel? I've read those 3 links before, they are a little bit confusing and mess up with my code. – Ahmed Sep 24 '16 at 13:21
  • (1+) @Ahmed, `they are a little bit confusing and mess up with my code` - well the problem is with your code, not the code provided in the links. Those links provide examples to the common solutions to this problem. So you need to debug your code to see what you are doing differently. So download the working code examples found in the links and study the code. If you have a question about the code then ask it, but we sure can't guess what your problem is because those are very simple code examples. – camickr Sep 24 '16 at 13:41