Background image on a JPanel

Hi
I wonder if there's possible to set a background image, not just a colour, on a JPanel, and if so, how do I do it?
/Nicklas

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.swing.*;
public class PicPanel extends JPanel
public PicPanel(InputStream theStream)
try
setPicture(theStream);
setMinimumSize(new Dimension(picture.getWidth(), picture.getHeight()));
setMaximumSize(new Dimension(picture.getWidth(), picture.getHeight()));
setPreferredSize(new Dimension(picture.getWidth(), picture.getHeight()));
setLayout(new GridBagLayout());
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
catch(Exception e)
e.printStackTrace();
public void setPicture(InputStream i)
try
decoder = JPEGCodec.createJPEGDecoder(i);
picture = decoder.decodeAsBufferedImage();
catch(Exception e)
e.printStackTrace();
public void paintImmediately()
super.paintImmediately(getBounds());
public void paintComponent(Graphics g)
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(picture, 0, 0, Color.white, null);
paintChildren(g);
JPEGImageDecoder decoder;
BufferedImage picture;

Similar Messages

  • How will I put a background Image in a JPanel?

    Can you help me on how will I put a background image in a JPanel?
    And also in a JDeskTopPane?

    Hint:
    Search the forum using two keywords in your subject. This question had been asked many times.

  • Background Image in JPanel.

    Hi, I am trying to set a background image to my JPanel. So far everything ok. However When I add labels to this panel as well the image is displayed but the label is not! I tried to make a search on google, and I found some links pointing to this forum, however none of them (which) I have seen show how to display a backgroun image and a label on top.
    What I tried to to is override the paint method of the JPanel. Here I did the following code:
    public void paint(Graphics g){
       Image i = this.myImage // Were myImage is of type Image with a loaded image in.
       g = super.getGraphics();
       g.drawImage(i, 0, 0, this);
       super.paint(g);
    }However this code brings the label and the image always blinking. I can imagine the reason for this is that the paint method is called all the time, and thus the image and the label are being painted all the time and so there is the blinking effect.
    Does anyone know how I can set a background image to my JPanel in an efficient way?

    No Gosling? I just retried it and it works for me.
    Maybe you're having trouble with the image URL.
    Try it with a local image.

  • Background image  for JPanel using UI Properties

    Is there any way to add background image for JPanel using UI Properties,
    code is
    if (property.equals("img")) {
    System.out.println("call image file in css"+comp);
    //set the background color for Jpanel
    comp.setBackground(Color.decode("#db7093"));
    here the comp is JPanel and we are setting the background color,
    Is there any way to put the Background image for the JPanel ????

    KrishnaveniB wrote:
    Is there any way to put the Background image for the JPanel ????Override the paintComponent(...) method of JPanel.
    e.g.
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class ImagePanel {
        public void createAndShowUI() {
            try {
                JFrame frame = new JFrame("Background Image Demo");
                final Image image = ImageIO.read(new File("/home/oje/Desktop/icons/yannix.gif"));
                JPanel panel = new JPanel() {
                    protected void paintComponent(Graphics g) {
                        g.drawImage(image, 0, 0, null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(new Dimension(400, 400));
                frame.setContentPane(panel);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            } catch (IOException ex) {
                ex.printStackTrace();
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ImagePanel().createAndShowUI();
    }

  • How to use a backgroud image in a JPanel.....

    hi,
    How can I display a background image for a JPanel.....
    I have searched this forum but all solutions I found are where JPanel at the end has to be displayed on a frame in an application...
    I need to use JPanel on Applet.......
    Actually I am switching JPanels on JApplet as screens for a game....
    I need to display background image for one of the JPanel class that has to be called by an Applet class....
    Thanks a lot for any help.

    Thanks a lot for your help.
    How do we specify URL to use an image from net. Is itt like I open an image file on some website & use its address? Kindly add here some website too so that its more clear for me.
    Thanks a lot for any help & all help your provided previously.
    String imageFilePath = "urlOfMyImageFile"; // I am asking about parameter to be used here
    ImageIcon ii = new ImageIcon(imageFilePath);
    Image image = ii.getImage();

  • Help getting a background image to show through a JScrollpane

    I have a GUI program which has a front menu with buttons on it. I have managed to get the background image displaying as I want it there by overriding the paintComponent method.
    My problem occurs when after clicking the "passport" button, a JScrollPane is added to the JPanel.
    I am having problems making the JScrollPane non-opaque( so i can see the image behind it).
    I have tried setting both scrollPane.setOpaque(false) and scrollPane.getViewport().setOpaque(false) and neither of these have worked.
    If anyone has managed to get a background image working with a JScrollpane then some help would be greatly appreciated.
    FileReader inF = new FileReader("passport.txt");
                    BufferedReader in = new BufferedReader(inF);
                    Passport passport = new Passport(in);
                    panel.removeAll();
                    panel.setVisible(false);
                    JLabel label = new JLabel( "<html><body><font size = \"5\">Number:</body></html>" );
                    JLabel label1 = new JLabel( "<html><body><font size = \"5\">" + passport.getNumber() + "<br><br></body></html>");
                    JLabel label2 = new JLabel( "<html><body><font size = \"5\">Issue Date</body></html>");
                    JLabel label3 = new JLabel( "<html><body><font size = \"5\">" + passport.getIssueDate() + "<br><br></body></html>");
                    JLabel label4 = new JLabel( "<html><body><font size = \"5\">Expiry Date:</body></html>");
                    JLabel label5 = new JLabel( "<html><body><font size = \"5\">" + passport.getExpiryDate() + "<br><br></body></html>");
                    JLabel label6 = new JLabel( "<html><body><font size = \"5\">Type:</body></html>" );
                    JLabel label7 = new JLabel( "<html><body><font size = \"5\">" + passport.getType() + "<br><br></body></html>");
                    JLabel label8 = new JLabel( "<html><body><font size = \"5\">State:</body></html>" );
                    JLabel label9 = new JLabel( "<html><body><font size = \"5\">" + passport.getState() + "<br><br></body></html>");
                    JPanel adder = new JPanel();
                    adder.setOpaque(false);
                    JScrollPane scroll = new JScrollPane(adder);
                    (scroll.getViewport()).setOpaque(false);
                    adder.setLayout( new GridBagLayout() );
                    GridBagConstraints c = new GridBagConstraints();
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 0;
                    c.weighty = 1.0;
                    adder.add( label, c );
                    c.gridy = 1;
                    adder.add( label1, c );
                    c.gridy = 2;
                    adder.add( label2, c );
                    c.gridy = 3;
                    adder.add( label3, c );
                    c.gridy = 4;
                    adder.add( label4, c );
                    c.gridy = 5;
                    adder.add( label5, c );
                    c.gridy = 6;
                    adder.add( label6, c );
                    c.gridy = 7;
                    adder.add( label7, c );
                    c.gridy = 8;
                    adder.add( label8, c );
                    c.gridy = 9;
                    adder.add( label9, c );
                    panel.add( scroll, BorderLayout.CENTER );
                    panel.setVisible(true); Thanks

    Swing related questions should be posted in the Swing forum.
    You seem to be doing things backwards.
    Add a background image to a JPanel. (Examples can be found by searching the Swing forum using "background image" as the keywords.
    Then add your labels to the same panel and then add the panel to the scrollPane.

  • Can anyone suggest me a simple way to add a background image to JFrame ?

    I want to add a background image to JFrame in a simple way rather than overiding the paint method or paintComponent method. Just like adding an image to JButton or JLabel using two or three lines of code. Is it possible ? r there any methods for this purpose ? if so pls give the code.

    JFrame as such does not provide an option to set a background image.
    Extending JPanel, over-riding its paintComponent() and setting it as the contentPane of JFrame is one way of doing it. Though you have to do the overriding, it is not very complex though.

  • 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

    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);
    }

  • Trouble with background image in JPanel

    I'm trying to get an image to display as a background image, and then put JLabels with other content on top of that background image. I've tried several things but can't figure out how to do it. Everytime I try, I get the images to display but the content is displayed side by side because of the layout manager.
    I've tried this but the image is displayed very tiny and still is not displayed in the background.
       //*   Draw background images for panels            *
           public class BackgroundPanel extends JPanel
          // the image you want displayed in the background
             private Image background;
          // A constructor to build and initialise your panel.
          // @param resourceName
              public BackgroundPanel(String resourceName)
                background = new ImageIcon( getClass().getClassLoader().getResource( resourceName )).getImage();
                setOpaque(false);
          // @see java.awt.Container#paint(java.awt.Graphics)
              public void paint(Graphics g)
                g.drawImage(background, 0, 0, 200, 480, this);          
             // draw the rest of the panel and it's children
                  super.paint(g);
          }

    To add a background to a Swing component, do not overload the public void paint(Graphics g); methods, but the public void paintComponent(Graphics g); instead, otherwise, the background will always overlap the Swing components added.
    Here is a goog working code:
    public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawImage(myStaticBackground, this.getWidth() - 256, this.getHeight() - 256, this);
    }In this example, I draw a static Image sized 256�256 at the bottom right corner.
    Fabruccio
    Java J2SE 1.5

  • Problem with Background image and JFrame

    Hi there!
    I've the following problem:
    I created a JFrame with an integrated JPanel. In this JFrame I display a background image. Therefore I've used my own contentPane:
    public class MContentPane extends JComponent{
    private Image backgroundImage = null;
    public MContentPane() {
    super();
    * Returns the background image
    * @return Background image
    public Image getBackgroundImage() {
    return backgroundImage;
    * Sets the background image
    * @param backgroundImage Background image
    public void setBackgroundImage(Image backgroundImage) {
    this.backgroundImage = backgroundImage;
    * Overrides the painting to display a background image
    protected void paintComponent(Graphics g) {
    if (isOpaque()) {
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    if (backgroundImage != null) {
    g.drawImage(backgroundImage,0,0,this);
    super.paintComponent(g);
    Now the background image displays correct. But as soon as I click on some combobox that is placed within the integrated JPanel I see fractals of the opened combobox on the background. When I minimize
    the Frame they disappear. Sometimes though I get also some fractals when resizing the JFrame.
    It seems there is some problem with the redrawing of the background e.g. it doesn't get redrawn as often as it should be!?
    Could anyone give me some hint, on how to achieve a clear background after clicking some combobox?
    Thx in advance

    I still prefer using a border to draw a background image:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.border.*;
    public class CentredBackgroundBorder implements Border {
        private final BufferedImage image;
        public CentredBackgroundBorder(BufferedImage image) {
            this.image = image;
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            int x0 = x + (width-image.getWidth())/2;
            int y0 = y + (height-image.getHeight())/2;
            g. drawImage(image, x0, y0, null);
        public Insets getBorderInsets(Component c) {
            return new Insets(0,0,0,0);
        public boolean isBorderOpaque() {
            return true;
    }And here is a demo where I load the background image asynchronously, so that I can launch the GUI before the image is done loading. Warning: you may find the image disturbing...
    import java.awt.*;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.*;
    import javax.swing.*;
    import javax.swing.border.*;
    public class BackgroundBorderExample {
        public static void main(String[] args) throws IOException {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame f = new JFrame("BackgroundBorderExample");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JTextArea area = new JTextArea(24,80);
            area.setForeground(Color.WHITE);
            area.setOpaque(false);
            area.read(new FileReader(new File("BackgroundBorderExample.java")), null);
            final JScrollPane sp = new JScrollPane(area);
            sp.setBackground(Color.BLACK);
            sp.getViewport().setOpaque(false);
            f.getContentPane().add(sp);
            f.setSize(600,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            String url = "http://today.java.net/jag/bio/JagHeadshot.jpg";
            final Border bkgrnd = new CentredBackgroundBorder(ImageIO.read(new URL(url)));
            Runnable r = new Runnable() {
                public void run() {
                    sp.setViewportBorder(bkgrnd);
                    sp.repaint();
            SwingUtilities.invokeLater(r);
    }

  • Animated gif layered above background image

    I have a static background image which is displayed in a JPanel. I need to display animated gifs at certain positions on the base image. For every new frame of the animation, the paint method is called whcih attempts to load the entire base image + all the new animated images which is extremely slow!. It also means the animated gif keeps on getting loaded, and seldom gets beyond the first frame.
    Im sure many people have had to do something like this. Is the solution to draw the gifs on the glasspane (or use layered panes), and to have a seperate paint method? or is their a simple approach like setting a special clip area.

    thanks for the help...
    The size of the background image is approx 400*400 pixels (gif or jpg), and the icons are 25*25, and have between 1 and 6 frames.
    Here comes the code... I havent finished with the mediatracker stuff, but youll get the idea Im sure.
      public void paint(Graphics g) {
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image,0);
        try {tracker.waitForID(0);}
        catch (InterruptedException e){}
        g.drawImage(image, 0, 0, this); //draw background image (approx 400*400 pixels)
        int ICON_ID = -1;
        Image img;
        //draw all the icons...
        //all icons have between 1 and 6 frames. 25*25 pixels.   
        for (int i = 0; i < icon_IDs.size(); i++) {
          try {
            ICON_ID = new Integer(icon_IDs.elementAt(i).toString()).intValue();
            Point p = dm.getIconPosition(ICON_ID);
            if (isAlarm(ICON_ID)) {        //ITS AN ALARM - use animated gif
              img = getAlarmImage(ICON_ID);
            else                          //NORMAL ICON - no animation
              img = DataDefinition.getImageIcon(dm.getIconType(ICON_ID)).getImage();
            tracker.addImage(img,0);
            try {tracker.waitForID(0);}
            catch (InterruptedException e){}
            int width = DataDefinition.getSize(dm.getIconType(ICON_ID)).width;
            int height = DataDefinition.getSize(dm.getIconType(ICON_ID)).height;
            g.setClip(p.x, p.y, p.x+width, p.y+height);
            g.drawImage(img, p.x, p.y, p.x+width, p.y+height, 0,0,img.getWidth(this),img.getHeight(this),this);
          catch (SQLException sqle) {
            System.out.println("Could not find position of icon : " + ICON_ID);
      }

  • Problem in setting background Image to TabedPane.

    Hi Pals,
    I created a JFrame which consist of some components. The structure of the added components are given below.
    1) I added a JPanel (MainPanel) with background image in a JFrame.
    2) I added a JTabbedPane with 7 tabs in the previous JPanel (MainPanel).
    3) Each tabs in the JTabbedPane having JPanel with Background Image.
    This is my Frame Structure. but i haven't problem to run in JDK1.6,1.5. It is working perfect. But when am run in lower version like 1.3 or 1.4. The design is looking bad. The JTabbedPane overlap the (MainPanel). The background image of the (MainPanel) was hided. I set setOpaque(false) for the JTabbedPane. But it also not working. How can i solve this problem.
    By,
    Mohan

    Your problems appear to stem from your choice of layout manager(s). To get better help sooner, post a [_Short, Self Contained, Compilable and Executable, Example Program (SSCCE)_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.
    To post code, use the code tags -- [code]Your Code[/code] will display asYour CodeOr use the code button above the editing area and paste your code between the {code}{code} tags it generates.
    db

  • How to load a background Image on a JDialog object

    Hi All, Actually i am new to java programing and i am stuck in this problem. I am developing a java application which is dialog based (using JDialog objects) and i want to load a background image on my dialog. I hope this could be done and i really appreciate your help because i must deliver this project and this is a user interface requirement.

    Try something along the lines of this: Create a new class called BackgroundImagePanel and have it extend JPanel.
    Have an attribute in the class for your image, and a method to set it.
    Override isOpaque() to return true.
    Override the paintComponenet method as such:
    public void paintComponent (Graphics g) {
    super.paintComponent(g);
    if( image == null ) return;
    Icon icon = new ImageIcon(image);
    icon.paintIcon(this, g, x, y);
    Add this panel to your JDialog and then add other component to this panel.
    You might have to tweak this a bit, but it should get you close...
    Bill

  • Problem placing buttons on top of a background image

    My knowledge of Java isn't the greatest and I am currently having problems placing buttons on top of a background image within a JApplet (this is also my first encounter with Applets).
    I'm using a Card Layout in order to display a series of different screens upon request.
    The first card is used as a Splash Screen which is displayed for 5 seconds before changing to the Main Menu card.
    When the Main Menu card is called the background image is not shown but the button is.
    While the Applet is running no errors are shown in the console.
    Full source code can be seen below;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    // First extend JApplet
    public class WOT extends JApplet {
         //--------------- Variables are declared here -----------------
         private CardLayout contentCardLayout = new CardLayout();  // declare CardLayout
         private Container contentContain;     // declare content Container
         private JPanel contentCard, splashScreen, mainMenu, menuImage; // declare content Panels
         private JLabel splash, menu, map; // declare image labels
         private ImageIcon mapBtn; // declare ImageIcons
         private JButton mapOption; // declare option Buttons
         private Timer timer; // declare Timer
         private ActionListener actionListener, mapOptionListener; // declare ActionListener
    //--------------- Initialise Applet -----------------
      public void init() {
         //--------------- Set-up Card Layout -----------------
         contentCard = new JPanel(contentCardLayout); // assign card panels to CardLayout
         //--------------- Splash Screen -----------------
         splashScreen = new JPanel();
         splash = new JLabel(new ImageIcon(getClass().getResource("img/bg.gif")));
         splashScreen.add(splash);
         splashScreen.setSize(600,800);
         splashScreen.setLocation(0,0);
    //--------------- "View Map" Option Button -----------------
         mapBtn = new ImageIcon(getClass().getResource("img/map.gif"));
         mapOption = new JButton(mapBtn);
         mapOption.setBorder(null);
         mapOption.setContentAreaFilled(false);
         mapOption.setSize(150,66);
         mapOption.setLocation(150,450);
         mapOption.setOpaque(false);
         mapOption.setVisible(true);
    //--------------- Main Menu Screen -----------------
         //menuImage = new JPanel(null);
         //menuImage.add(mainMenu);
         //menuImage.setLocation(0,0);
         mainMenu = new JPanel(null);
         menu = new JLabel(new ImageIcon(getClass().getResource("img/menu.gif")));
         menu.setLocation(0,0);
         mainMenu.add(menu);
         //mainMenu.setBackground(Color.WHITE);
         mainMenu.setLocation(0,0);
         mainMenu.setOpaque(false);
         //mainMenu.setSize(150,66);
         mainMenu.add(mapOption);
         //--------------- Map Image Screen -----------------
         map = new JLabel(new ImageIcon(getClass().getResource("img/map.gif")));
         //--------------- Add Cards to CardLayout Panel -----------------
        contentCard.add(splashScreen, "Splash Screen");
         contentCard.add(mainMenu, "Main Menu");
         contentCard.add(map, "Map Image");
    //--------------- Set-up container -----------------
          contentContain = getContentPane(); // set container as content pane
          contentContain.setBackground(Color.WHITE); // set container background colour
           contentContain.setLocation(0,0);
           contentContain.setSize(600,800);
          contentContain.setLayout(new FlowLayout()); // set container layout
           contentContain.add(contentCard);  // cards added
           //--------------- Timer Action Listener -----------------
           actionListener = new ActionListener()
                    public void actionPerformed(ActionEvent actionEvent)
                             //--------------- Show Main Menu Card -----------------
                             contentCardLayout.show(contentCard, "Main Menu");
         //--------------- Map Option Button Action Listener -----------------
           mapOptionListener = new ActionListener()
                    public void actionPerformed(ActionEvent actionEvent)
                             //--------------- Show Main Menu Card -----------------
                             contentCardLayout.show(contentCard, "Map Image");
         //--------------- Timer -----------------               
         timer = new Timer(5000, actionListener);
         timer.start();
         timer.setRepeats(false);
    }Any help would be much appreciated!
    Edited by: bex1984 on May 18, 2008 6:31 AM

    1) When posting here, please use fewer comments. The comments that you have don't help folks who know Java read and understand your program and in fact hinder this ability, which makes it less likely that someone will in fact read your code and help you -- something you definitely don't want to have happen! Instead, strive to make your variable and method names as logical and self-commenting as possible, and use comments judiciously and a bit more sparingly.
    2) Try to use more methods and even classes to "divide and conquer".
    3) To create a panel with a background image that can hold buttons and such, you should create an object that overrides JPanel and has a paintComponent override method within it that draws your image using the graphics object's drawImage(...) method
    For instance:
    an image jpanel:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URISyntaxException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    public class BackgroundImage
        // **** this will have to be changed for your program:
        private static final String IMAGE_PATH = "../../m02/a/images/Forest.jpg";
        private BufferedImage myImage = null;
        private JPanel imagePanel = new JPanel()
            @Override
            protected void paintComponent(Graphics g)
            {   // *** here is where I draw my image
                super.paintComponent(g);  // **** don't forget this!
                if (myImage != null)
                    g.drawImage(myImage, 0, 0, this);
        public BackgroundImage()
            imagePanel.setPreferredSize(new Dimension(600, 450));
            imagePanel.add(new JButton("Foobars Rule!"));
            try
                myImage = createImage(IMAGE_PATH);
            catch (IOException e)
                e.printStackTrace();
            catch (URISyntaxException e)
                e.printStackTrace();
        private BufferedImage createImage(String path) throws IOException,
                URISyntaxException
            URL imageURL = getClass().getResource(path);
            if (imageURL != null)
                return ImageIO.read(new File(imageURL.toURI()));
            else
                return null;
        public JPanel getImagePanel()
            return imagePanel;
    }and an applet that uses it:
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    public class BackgrndImageApplet extends JApplet
        @Override
        public void init()
            try
                SwingUtilities.invokeAndWait(new Runnable()
                    @Override
                    public void run()
                        getContentPane().add(new BackgroundImage().getImagePanel());
            catch (InterruptedException e)
                e.printStackTrace();
            catch (InvocationTargetException e)
                e.printStackTrace();
    }

  • How to get Internal frames as well as a background image in a JFrame?

    Hi All,
    Here's my problem. I have a JFrame with a background image that I got by overridding the paintComponent() method of a JPanel and setting as the contentPane of a JFrame. Works great.
    Now what I want is to use internal frames with this Frame a la JInternalFrame classes. But you need to create a JDesktopPane object and set this as the contentPane of the JFrame to get internal frames to work, right? Is there a way for me to have my cake and eat it so I get both internal frames and a background image? Thanks!

    Hmmm,
    so simple, why didn't I think of that? Damn Mondays!
    thanks very much!

Maybe you are looking for

  • How do I get a Connect Training Course to update my Captivate Presentation Version?

    I am using a presentation with quiz created in Adobe Captivate 5.  I am using this as a online training course on Adobe Connect.  I published the first two versions, which are reflected in user reports in Connect. However, I have made some important

  • AcrobetX not functioning correctly

    I have CS3.  I just installed a new computer with Win7.  My updated Acrobat was 7 (I believe).  I downloaded AcrobatX but can't seem to get it to work like jAcro7 used to work.  It is my understanding that Acro7 will not work with Win7.  Is this corr

  • "WIB init failed!" sign.

    Sometimes my iphone 4 (iOS 6.0.1) goes into black screen with sign "WIB init failed!". What is this and what should I do to remove it.

  • Job to lock users in APO?

    Hi All, We're having some locking issues with batch jobs in DP (using macros) because users occasionally have CVCs locked when the jobs are running. We have looked into additional data views for job scheduling using the key figure lock setting on the

  • FAL_SERVER and FAL_CLIENT - Please help me

    Sir, What is the difference between FAL_SERVER and FAL_CLIENT? How we can configure this on Primary and Satndby databases? What will be the changes happen to this parameters at the time of Switchover and Failover? Please help me. regards Mathew