aspose file tools
The moose likes Swing / AWT / SWT and the fly likes how to add images dynamically to jpanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "how to add images dynamically to jpanel?" Watch "how to add images dynamically to jpanel?" New topic
Author

how to add images dynamically to jpanel?

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 902
hello, i added some images to jpanel, with some animation.
i put some images in a folder, and i m display/moving them on jpanel simultaneously.
my question is can i add images dynamically??
i mean what i want is, i want to add a button on that panel, and when i click on that button, one more image (randomly) should added to my jpanel from that folder.
how can i do this??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the code to add the image is identical to
how you'd add it when the frame first opens.

because the frame is now visible, after
adding it you need to have these lines

panel.add(image);//however you add it
panel.revalidate();
panel.repaint();
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 902
but how even i add add those images on button click one by one, i mean how i get images randomly from my image folder??


above is what i am doing right now.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
you need to use Math.random()
check the API and tutorials


SCJP
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4167
    
    3

Randall Twede wrote:you need to use Math.random()

Or java.util.Random


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i mean how i get images randomly from my image folder??

another option, at program startup, is to load them all into a List,
then Collections.shuffle(list) and add them to your panel in the
'shuffled' order of the list.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to add images dynamically to jpanel?
 
Similar Threads
Help!
JPanel & JScrollPane
dynamic indexed properties
Iterating through images in Resource Folder
Dynamically adding values to a Drop down menu