0

I have small problem with changing JFrame background image. First I've added background image with JLabel and application is working good. But now I need to change it dynamically.

I've tried this code :

label = new JLabel(new ImageIcon(Toolkit.getDefaultToo... // old background image

public void changeImage(){

label.setVisible(false);

label2 = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("weatherall.gif"))));
setContentPane(label2);  // new Background image
label2.setVisible(true);
repaint();

} 
 switch (cmb.getSelectedItem().toString()) {
    case "ISTANBUL":
        x = 0;
        changeImage();
         //some codes......vs.vs.               
        break;

Also I'v tried it with timer (TimerTask) every 1 sec. Refreshing frame

Anybody have an idea about this?

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • there are two ways, add Image to JLabel (better wiil be by using local variables as loading ImageIcon at runtime, because intensive I/O required flush() for Icon/ImageIcon, before is added to JLabel), and second of the ways is by using painting to JPanel by using paintComponent, by default both ways are described in official Oracles tutorials – mKorbel May 23 '16 at 13:26
  • You can use this answer here with a modification to how the image is set, and you could find a few ways to set the image when the program loops through and calls paint. [Related Question](http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe?rq=1) – Mr00Anderson May 23 '16 at 13:27
  • @mkorbel Can u see my source code after that u can understand better.I need to change image ***at runtime*** and my app extends JFrame...[link](https://drive.google.com/file/d/0ByPqPK4HeCfMV3BNZFpJczBoTVU/view?usp=sharing) – Coder ACJHP May 23 '16 at 13:39
  • 2
    *"but not worked with me"* What *exactly* happened? General advice: 1) For better help sooner, post 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). 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson May 23 '16 at 13:42
  • I'm sorry for all wrong things, I'm new to Java and new to this site :) Just I need to solve my problem because I can't find any answer in Google. – Coder ACJHP May 23 '16 at 13:51
  • For [example](http://stackoverflow.com/a/5129757/230513). – trashgod May 23 '16 at 17:09
  • I'm solved the problem,just change the wallpaper on same Label not another one.thanks – Coder ACJHP May 31 '16 at 13:33

1 Answers1

0

Now we need to create JLabel and set it size like background(stretch it) after that just add image into the JLabel and when u want to change it just change the image from same JLabel don't try adding another JLabel,it's not working!!...thats it.

JLabel label = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(localweather))));
setContentPane(label);//when u want to change background image just replace 'localweather' another image.
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34