0

I've tried to set a background image to my JPanel, that holds some other components like Jlabel,JCheckBox,JTextField.I did add the background by creating another class and extending JPanel, and so overriding paintComponent().the image is been set perfectly but those labels and ... , won't get displayed.only buttons get displayed and that when I bring the mouse over them(checkboxes too, but, when they get displayed they contain their own background, which interferes with the image).

My code:

public class ImagePanel extends JPanel {

private Image image;

ImagePanel(String path){
    
    this(new ImageIcon(path).getImage());
    
}

ImagePanel(Image image){
    
    this.image=image;
    
    Dimension size=new Dimension(796, 481);
    setSize(size);
    setLayout(null);
    
}
@Override
public void paintComponent(Graphics g) {
    //numbers are the size of the main JPanel
    g.drawImage(image,0,0,796,481, this);
    
}

}

and then added its object to my main JPanel, which holds components.for those components, i have only set the size and location nothing else. please help.

thanks in advance.

The answer is found thank you all for your participation.the answer is in the comments.

Navid Nasro
  • 19
  • 1
  • 6
  • 1
    `public void paintComponent(Graphics g) { ..` should be `public void paintComponent(Graphics g) { super.paintComponent(g); ..` – Andrew Thompson Jun 03 '21 at 09:37
  • 2
    Further 1) `g.drawImage(image,0,0,796,481, null);` should be `g.drawImage(image,0,0,796,481, this);` (every `JComponent` is an `ImageObserver`) 2) The `@Override` notation should be added to any overridden method. 3) `Dimension size=new Dimension(796, 481); setSize(size);` will achieve nothing. For a specific size, override the `getPreferredSize()` method. Most layout managers will honor that, and `pack()` the frame around it for a neat fit. 4) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem that should be fixed. – Andrew Thompson Jun 03 '21 at 09:41
  • 1
    5) `new ImageIcon(path).getImage()` is a very fragile way to load an image. Use `ImageIO.read(URL)` for helpful output when the image is not found. Also note that a background image for a panel is obviously an application resource, so will need to be loaded by URL rather than file. .. Really, this code should be tossed out, and started over again. There's more wrong with it, than right. – Andrew Thompson Jun 03 '21 at 09:46
  • @AndrewThompson What differences that would make?could you please explain?(for adding super.paintComponent(g)) – Navid Nasro Jun 03 '21 at 09:47
  • *"could you please explain?"* Have you tried it? – Andrew Thompson Jun 03 '21 at 09:50
  • @AndrewThompson yeah i know that (for number 5) but here I'm just trying to find the solution as easy as possible.when the solution is found and the code worked well, I'll make those changes for efficiency. – Navid Nasro Jun 03 '21 at 09:52
  • @AndrewThompson "Have you tried it?" yeah but that won't solve my problem.are you suggesting it for efficiency or has some other effects? – Navid Nasro Jun 03 '21 at 09:56
  • @AndrewThompson and by the way,the bracket problem that you mentioned.there is no such problem in my code.the last closing bracket has been dropped out of the code here I don't know why – Navid Nasro Jun 03 '21 at 10:03
  • I have better things to do than play 20 questions, so I'll leave you with these last two tips and .. go do them. 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jun 03 '21 at 10:05
  • 1
    You have to add your Swing components to the same JPanel you've incorrectly drawn your image on. Since you want explanations for everything, go through the Oracle tutorial, [Creating a GUI With JFC/Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Netbeans section, Study every other section. Finally, start by creating simple Swing GUIs that work, then worry about making them "pretty" with background images. – Gilbert Le Blanc Jun 03 '21 at 10:05
  • @GilbertLeBlanc you're saying that i must put the components on the JPanel that I painted here?then the components will get shown. right? – Navid Nasro Jun 03 '21 at 10:11
  • @GilbertLeBlanc thank you, the problem was what you mentioned.i had to do all these things where the components are.now that I found the solution the question seems silly to myself lol.thank you for your help and for controlling your emotions not to punch me in the face. (just joking) :D – Navid Nasro Jun 03 '21 at 10:56

0 Answers0