Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Show images in a JPanel  RSS feed

 
hema2001
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I'm really new in programming java and do need some help now.
How do I add some images (jpg-files, path out of a database) to a JPanel which extends a JScrollPane. The images should be resized to a thumbnail-size.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet is to create JLabels inside your panel to apply the images to. So you code for that would be something like
[CODE]
ImageIcon myIcon = new ImageIcon("path\\to\\file.jpg");
JLabel myLabel = new JLabel(myIcon2);
[\CODE]
Now, where does myIcon2 come from?? Well, to resize the image to a thumbnail size you do something like this:
[CODE]
Image img = myIcon.getImage();
Image newimg = img.getScaledInstance(230, 310, Image.SCALE_SMOOTH);
myIcon2 = new ImageIcon(newimg);
[\CODE]
Your labels should be set to a size the same as what size your Images will be scaled to.
Hope that helps
Gregg
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!