Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Is it possible to put Panel on the Image

I have a doubt. Can i place a panel on top of the image in the frame?

I have a JFrame where i want the image to be displayed... and i have a JPanel where i want it to be displayed but on top of the image. I am talking about the skin of the GUI...   In VC++, you can do that.. is there a way to do in java??

Thanks in advance
Ram
0
rampriya_sb
Asked:
rampriya_sb
  • 7
  • 5
  • 5
  • +1
3 Solutions
 
CEHJCommented:
If you want to float components over the image just override paintComponent, being careful to call super.paintComponent
0
 
rampriya_sbAuthor Commented:
can u give me some more clue? some small code snippet wud be appreciated
0
 
CEHJCommented:
public void paintComponent(Graphics g) {
      super.paintComponent(g);
      // call drawImage here
}

You'll still be able to add components leaving the image there
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
(oh that's overriding the paintComponent of JPanel btw)
0
 
rampriya_sbAuthor Commented:
is it JPanel u r talking about or the JFrame?
0
 
CEHJCommented:
JPanel
0
 
Tommy BraasCommented:
If you want the image drawn behind the panel, you need to override paintComponent() (see CEHJ's comment) in your JFrame instance.
0
 
CEHJCommented:
You can't do that actually - JFrame has no paintComponent. Easiest to add another JPanel to the frame and then place the final one on that
0
 
Tommy BraasCommented:
Oh, my bad.

rampriya_sb, recap of information so far:
Set BorderLayout as your layout manager for the JFrame's content pane
Override paintComponent in a subclass of JPanel, in order to paint the image, as described above
Add an instance of the JPanel described above to the BorderLayout.CENTER of the JFrame
0
 
CEHJCommented:
>>Set BorderLayout as your layout manager for the JFrame's content pane...

Well of course, it's still not clear what the intention is, so this is probably a little bit too assumptive ;-)
0
 
Tommy BraasCommented:
>> probably a little bit too assumptive
possibly, but not if he wants the image to cover the complete area of the JFrame
0
 
rampriya_sbAuthor Commented:
I want the image to cover the entire area and place a Panel on the image...
0
 
CEHJCommented:
Should be OK then
0
 
Tommy BraasCommented:
yep, I think that's it! ;-)
0
 
rampriya_sbAuthor Commented:
As suggested, i overrode paintComponent method and put the image

class TestPanel extends JPanel
{

JPanel pnlAnotherPanel;
public TestPanel()
{
    pnlAnotherPanel = new JPanel();
    pnlAnotherPanel.setBackground(Color.white);
    pnlAnotherPanel.setBounds(new Rectangle(30,30,60,80));
    this.add(pnlAnotherPanel,null);
}

public void paintComponent(Graphics g)
{
  super.paintComponent(g);
  Image img=Toolkit.getDefaultToolkit().getImage("testimg.jpg");
  g.drawImage(img,0,0,this);
}
}

When i try to put this JPanel class to the Frame and try to runthe program, only the image is displayed and not the panel(pnlAnotherPanel)..... so what shud i do for that? i tried setting the panel to opaque, but didnt help...
 

Then i tried putting
0
 
rampriya_sbAuthor Commented:
i m not finding any luck still.... shud i put the panel in the paintComponent method??
0
 
objectsCommented:
Theres no need to override paintComponent, just add your panel to whatever component is displaying your image.

eg.

JLabel back = new JLabel(image);
JPanel panel = new JPanel();
back.add(panel);
0
 
objectsCommented:
If you instead want the panel to float above your existing component hierarchy then add the panel to the glass pane or a layered pane.
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
0
 
Tommy BraasCommented:
Three way point split
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now