setting a jpanel as a background image

i have managed to load the image into a jpanel but now need to know how to set the jpanel as a background image, to allow my jbuttons and textfields to be seen over the top
[179 byte] By [jimminybba] at [2007-11-26 17:16:11]
# 1
so i assume no one can help?possibly its because trying to make an image inside a jpanel is a stupid way of going about making a background?
jimminybba at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 2
possibly i'm invisible
jimminybba at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 3
possibly...just possibly...i'm going to sit here in vain, hoping to get an answer to this simple question.ori'm going to realize my folly of even thinking that this is the best place to get help.who knows
jimminybba at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 4
See http://faq.javaranch.com/view?BackgroundImageOnJPanelCheers
duckbilla at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 5
Hey, it's a Sunday, so all good Java coders are at church and all bad Java coders are hungover. And don't forget about the Superbowl!Anyway, check out the code I gave in reply #2: http://forum.java.sun.com/thread.jspa?threadID=5132180
DrLaszloJamfa at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 6
> See> http://faq.javaranch.com/view?BackgroundImageOnJPanel> > CheersThe trouble with subclassing like that is what if elsewhere you want a bakground image on your JTable's JViewport? On you JDesktopPane? Etc?
DrLaszloJamfa at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 7

that statement is unbeleivably true.

right, heres my two classes, the first class is where i created the image

/****************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.* ;

import java.io.*;

import javax.imageio.*;

public class EmailBackgroundImage extends JComponent

{

public void paint (Graphics g)

{

File bkgdimage = new File ("background.jpg");

Image imagebg = createImage ( 250,300 );

try {

imagebg = ImageIO.read(bkgdimage);

}

catch (IOException e)

{}

g.drawImage(imagebg, 0, 0,null,null);

}

}

/**************************************************************************/

this next class is where i'm trying to make the background

youll notice remnants of my current attempt to create the background image.

in its current state it does compile

/*********************************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.* ;

import java.sql.*;

import java.util.*;

import java.awt.color.*;

public class emailFrontpage extends JFrame implements ActionListener

{

JLabel FirstNameLabel= new JLabel("First Name");

JLabel LastNameLabel = new JLabel("Last Name");

JLabel EmailLabel= new JLabel("Email Address");

JTextField FirstNameText = new JTextField( 10 );

JTextField LastNameText = new JTextField( 10 );

JTextField EmailAddressText= new JTextField( 20 );

JPanel panel1= new JPanel();

JPanel panel2= new JPanel();

JPanel panel3= new JPanel();

JPanel panel4= new JPanel();

JPanel panel5= new JPanel();

JPanel image= new JPanel();

JButton Enterbutton = new JButton("Enter Details");

JButton Logon= new JButton("Log on");

EmailBackgroundImage myimage;

EmailEdit editEmp;

emailFrontpage()

{

getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ));

myimage = new EmailBackgroundImage();

image.add(myimage);

myimage.setSize(250,300);

image.setOpaque( false );

FirstNameLabel.setForeground(Color.WHITE);

LastNameLabel.setForeground(Color.WHITE);

EmailLabel.setForeground(Color.WHITE);

panel1.add(Logon);

panel2.add(FirstNameLabel); panel2.add(FirstNameText);

panel3.add(LastNameLabel); panel3.add(LastNameText);

panel4.add(EmailLabel);panel4.add(EmailAddressText);

panel5.add(Enterbutton);

getContentPane().add(image);

getContentPane().add(panel1);

getContentPane().add(panel2);

getContentPane().add(panel3);

getContentPane().add(panel4);

getContentPane().add(panel5);

// .setBackground( Color.black )

Enterbutton.addActionListener( this );

Logon.addActionListener( this );

Enterbutton.setActionCommand( "ENTERBUTTONPRESSED" );

Logon.setActionCommand( "LOGONBUTTONPRESSED" );

setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );

editEmp = new EmailEdit();

}

public void actionPerformed( ActionEvent evt)

{

if ( evt.getActionCommand().equals("LOGONBUTTONPRESSED") )

{

emailLogonpage emaillogon = new emailLogonpage();

emaillogon.setSize( 250, 300 );

emaillogon.setVisible( true );

emaillogon.setResizable(false);

setVisible(false);

}

else

{

String newFN = FirstNameText.getText();

String newLN = LastNameText.getText();

String newEA = EmailAddressText.getText();

editEmp.addNewEmail(newFN, newLN, newEA);

setVisible(false);

emailFrontpage emailfront = new emailFrontpage();

emailfront.setSize( 250, 300 );

emailfront.setVisible( true );

emailfront.setResizable(false);

}

}

public static void main ( String[] args )

{

emailFrontpage emailfront = new emailFrontpage();

emailfront.setSize( 250, 300 );

emailfront.setVisible( true );

emailfront.setResizable(false);

}

}

jimminybba at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 8

> The trouble with subclassing like that is what if

> elsewhere you want a bakground image on your JTable's

> JViewport? On you JDesktopPane? Etc?

The op wanted a JPanel that has a background image. Hence the subclass solution. I didn't have a good look at the link you posted, but if it provides a better/more general solution, then I suggest the op to use that instead and award the good Dr with the dukes he deserves. =)

Cheers,

Jukka

duckbilla at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 9

First you post the question in the wrong forum. (Swing related questions should be posted in the Swing forum)

Then you start complaining every 10, 10, 5, minutes that no one is answering your question.

Well, if you even bothered to do a little searching of the forum you would have found your answer because this question is asked all the time.

Learn how to use the forum.

camickra at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...
# 10
> possibly i'm invisible1. Wait 30 seconds 2. Post another message3. Assume that all forum members get a call from FBI and Secret Service that there's a new posting in the forum4. Go to 1
kirillga at 2007-7-8 23:44:14 > top of Java-index,Java Essentials,Java Programming...