Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Display Thumbnail in jPanel?

Thread view: 
Barkster - 05 Jun 2006 00:42 GMT
Is there a simplier way to display a thumbnail of an image from a given
path to the file on the hard drive?  I've got it working using a bunch
of code and drawing the graphics but I thought there was an easier way
to do it using createthumbnail?  I'm trying to build simple app that
has a file dialog and select a file and a thumbnail is show in a
jpanel.

Thanks
Oliver Wong - 05 Jun 2006 17:44 GMT
> Is there a simplier way to display a thumbnail of an image from a given
> path to the file on the hard drive?  I've got it working using a bunch
> of code and drawing the graphics but I thought there was an easier way
> to do it using createthumbnail?  I'm trying to build simple app that
> has a file dialog and select a file and a thumbnail is show in a
> jpanel.

   See http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

   - Oliver
Barkster - 05 Jun 2006 20:28 GMT
Thanks, I've seen that but wasn't sure how to size the files.  I'll
take another look

> > Is there a simplier way to display a thumbnail of an image from a given
> > path to the file on the hard drive?  I've got it working using a bunch
[quoted text clipped - 6 lines]
>
>     - Oliver
Oliver Wong - 05 Jun 2006 20:45 GMT
[post re-ordered]

>> > Is there a simplier way to display a thumbnail of an image from a given
>> > path to the file on the hard drive?  I've got it working using a bunch
[quoted text clipped - 7 lines]
> Thanks, I've seen that but wasn't sure how to size the files.  I'll
> take another look

   Actually, you might not be able to directly resize the image via that
class. I wasn't aware that that was the main problem. You might want to look
at these classes:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/AffineTransformOp.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.html

   - Oliver
Barkster - 05 Jun 2006 21:25 GMT
I'll take a look at that then, thanks

> [post re-ordered]
>
[quoted text clipped - 18 lines]
>
>     - Oliver
Barkster - 05 Jun 2006 21:26 GMT
Any ideas as to why this doesn't work for me?  I can select a file but
it doesn't display anything.

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

Public class ImageViewer extends JFrame implements ActionListener {
 public ImageViewer() {
   setTitle("ImageViewer");
   setSize(400, 600);

   JMenuBar mbar = new JMenuBar();
   JMenu m = new JMenu("File");
   openItem = new JMenuItem("Open");
   openItem.addActionListener(this);
   m.add(openItem);
   exitItem = new JMenuItem("Exit");
   exitItem.addActionListener(this);
   m.add(exitItem);
   mbar.add(m);
   setJMenuBar(mbar);

   label = new JLabel();
   Container contentPane = getContentPane();
   contentPane.add(label, "Center");
   label.setText("What the hell");
 }

 public void actionPerformed(ActionEvent evt) {
   Object source = evt.getSource();
   if (source == openItem) {
     JFileChooser chooser = new JFileChooser();
     chooser.setCurrentDirectory(new File("."));

     chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
       public boolean accept(File f) {
         return f.getName().toLowerCase().endsWith(".gif")
             || f.isDirectory();
       }

       public String getDescription() {
         return "GIF Images";
       }
     });

     int r = chooser.showOpenDialog(this);
     if (r == JFileChooser.APPROVE_OPTION) {
       String name = chooser.getSelectedFile().getName();
       label.setIcon(new ImageIcon(name));
     }
   } else if (source == exitItem)
     System.exit(0);
 }

 public static void main(String[] args) {
   JFrame frame = new ImageViewer();
   frame.show();
 }

 private JLabel label;

 private JMenuItem openItem;

 private JMenuItem exitItem;
}

> [post re-ordered]
>
[quoted text clipped - 18 lines]
>
>     - Oliver
Oliver Wong - 06 Jun 2006 16:23 GMT
> Any ideas as to why this doesn't work for me?  I can select a file but
> it doesn't display anything.

[most of the code snipped]
>      JFileChooser chooser = new JFileChooser();
[...]

>      int r = chooser.showOpenDialog(this);
>      if (r == JFileChooser.APPROVE_OPTION) {
>        String name = chooser.getSelectedFile().getName();
>        label.setIcon(new ImageIcon(name));
>      }

File.getName() only returns the filename. use getAbsolutePath() instead.

   - Oliver
Barkster - 06 Jun 2006 16:41 GMT
Thanks that was it.  Appreciate the help, now hopefully with the
combination of these functions I can put something together.

How do you know which classes you need to do a particular function?
Say for instance manipulate an image?  Will java tell you when you
compile that it needs a library?  I'm very new to java as you can tell

Thanks

> > Any ideas as to why this doesn't work for me?  I can select a file but
> > it doesn't display anything.
[quoted text clipped - 12 lines]
>
>     - Oliver
Oliver Wong - 06 Jun 2006 18:38 GMT
> How do you know which classes you need to do a particular function?

   Experience and guesswork. If you go to
http://java.sun.com/j2se/1.5.0/docs/api/ you'll see 3 frames. The bottom
left one contains a list of all classes in the standard Java class library.
You can browse through that and hope that their names accurately reflect
what they do.

> Say for instance manipulate an image?

   I'd look for classes that have the word "image" or "graphic" in them.

>  Will java tell you when you
> compile that it needs a library?

   There's a bit of a terminology problem here. I'll try to clarify the
problem with an analogy: In the "real-non-Java-world", a library is a place
with a bunch of books. When you're doing a research project, it's not that
you borrow the library. Rather, you borrow books from the library.

   So in Java, instead of "borrowing books", you "import classes" (you can
also import interfaces). That's what the import statements at the top of
most java source files are for: they tell the compiler which classes you're
going to need for this program.

   If you try to use a class without importing it, the compiler will
complain.[*]

   - Oliver

[*] There are two exceptions to this rule:
(1) Some classes, like Object, String, Thread, Math, etc. are imported
automatically. You don't need to import these.
(2) You can avoid importing classes by giving their full names. Instead of
importing ArrayList, you can use the full name "java.util.ArrayList".
Barkster - 06 Jun 2006 19:50 GMT
Cool, thanks for that great explaination.  That is helpful

> > How do you know which classes you need to do a particular function?
>
[quoted text clipped - 31 lines]
> (2) You can avoid importing classes by giving their full names. Instead of
> importing ArrayList, you can use the full name "java.util.ArrayList".


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.