Results 1 to 5 of 5
  1. #1
    radhi is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default How to change the background image of panel

    Hi ,

    Please let me know how to change the panel background image when i click on button which is placed on panel

    Thanks in advance

    Regards
    Radhika

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    What have you tried? I googled "java background image" and got a ton of results. Did you try any of them? Where are you stuck?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by radhi View Post
    Hi ,

    Please let me know how to change the panel background image when i click on button which is placed on panel
    There doesn't exist no such thing as a 'background image' on any Swing component. You can paint an image on a JComponent and Swing does the rest. You do the painting in the paintComponent(Graphics g) method and Swing takes care of the clipping (in case the component has any visible child components).

    kind regards,

    Jos

  4. #4
    radhi is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Hi,

    The below is the code for the background image for panel ..
    and this panel has a button over there when i click i need to change the background image....

    ImageIcon othericon=new ImageIcon("images/03_Menu.png");
    Others = new javax.swing.JPanel()
    {
    protected void paintComponent(Graphics g)
    {
    g.drawImage(othericon.getImage(), 0,0, null);
    super.paintComponent(g);
    }
    }

    ;



    Thank you

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by radhi View Post
    protected void paintComponent(Graphics g)
    {
    g.drawImage(othericon.getImage(), 0,0, null);
    super.paintComponent(g);
    }
    First let the super class object do its painting and then paint your icon. (i.e. swap those two lines in your paintComponent( ... ) method).

    kind regards,

    Jos

Similar Threads

  1. change panel background at runtime
    By cahyadiakbar in forum AWT / Swing
    Replies: 2
    Last Post: 08-26-2010, 08:16 AM
  2. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 01:07 AM
  3. change Panel Color
    By aadi_j in forum AWT / Swing
    Replies: 2
    Last Post: 11-16-2009, 10:12 AM
  4. how to change windows xp background image through java
    By erosenthil in forum Advanced Java
    Replies: 1
    Last Post: 08-18-2009, 12:57 PM
  5. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-17-2008, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •