Connecting Tech Pros Worldwide Forums | Help | Site Map

GUI (JLabel) .JPEG not recegnized

momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#1: Jun 11 '08
my code works fine with .GIF but not with .JPEG!!??
jdk.6.0_02
jre1.6.0_05

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jun 11 '08

re: GUI (JLabel) .JPEG not recegnized


Please explain. Even better, provide a SSCCE: http://mindprod.com/jgloss/sscce.html
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#3: Jun 11 '08

re: GUI (JLabel) .JPEG not recegnized


Quote:

Originally Posted by BigDaddyLH

Please explain. Even better, provide a SSCCE: http://mindprod.com/jgloss/sscce.html

Expand|Select|Wrap|Line Numbers
  1. ort java.awt.BorderLayout;
  2. import javax.swing.ImageIcon;
  3. import javax.swing.JLabel;
  4. import javax.swing.JFrame;
  5.  
  6. public class LabelDemo
  7. {
  8.    public static void main( String args[] )
  9.    {
  10.       // Create a label with plain text
  11.       JLabel northLabel = new JLabel( "North" );
  12.  
  13.       // create an icon from an image so we can put it on a JLabel
  14.       ImageIcon labelIcon = new ImageIcon( "C:\\Users\\george bush\\Desktop\\ring_sat.GIF" );
  15.  
  16.       // create a label with an Icon instead of text
  17.       JLabel centerLabel = new JLabel( labelIcon );
  18.  
  19.       // create another label with an Icon
  20.       JLabel southLabel = new JLabel( labelIcon );
  21.  
  22.       // set the label to display text (as well as an icon)
  23.       southLabel.setText( "South" );
  24.  
  25.        // create a frame to hold the labels
  26.       JFrame application = new JFrame();
  27.  
  28.       application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  29.  
  30.       // add the labels to the frame; the second argument specifies
  31.       // where on the frame to add the label 
  32.       application.add( northLabel, BorderLayout.NORTH );
  33.       application.add( centerLabel, BorderLayout.CENTER );      
  34.       application.add( southLabel, BorderLayout.SOUTH );      
  35.  
  36.       application.setSize( 300, 300); // set the size of the frame
  37.       application.setVisible( true ); // show the frame
  38.    } // end main
  39. } // end class LabelDemo
  40.  
Quote:
when I replace the GIF with the JPEG version the panel is empty but stillI candisplay text
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Jun 11 '08

re: GUI (JLabel) .JPEG not recegnized


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.net.*;
  3. import javax.swing.*;
  4.  
  5. public class ImageTest {
  6.     public static void main(String[] args) {
  7.         EventQueue.invokeLater(new Runnable(){
  8.             public void run() {
  9.                 try {
  10.                     launchGUI();
  11.                 } catch (MalformedURLException e) {
  12.                     e.printStackTrace();
  13.                 }
  14.             }
  15.         });
  16.     }
  17.  
  18.     static void launchGUI() throws MalformedURLException {
  19.         URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
  20.         JFrame f = new JFrame("ImageTest");
  21.         f.getContentPane().add(new JLabel(new ImageIcon(url)));
  22.         f.pack();
  23.         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  24.         f.setLocationRelativeTo(null);
  25.         f.setVisible(true);
  26.     }
  27. }
ImageIcon has no problems displaying JPEG images. Perhaps your path is wrong or the file is corrupted.
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#5: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


thank you nice picture !
but is there any difference if I use uper/lowercase?
does the size of the image matter?
PS: the code is pefectely working on netbeans but as weird as it can sounds it's not working using cmd
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#6: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


sorry never mind its working now
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#7: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


now I wanna resize the pic to the demesions of the frame? but can't find any method ??
plz help
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#8: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


Quote:

Originally Posted by momotaro

now I wanna resize the pic to the demesions of the frame? but can't find any method ??
plz help

Did you look at the methods of Image?
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#9: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


Quote:

Originally Posted by BigDaddyLH

Did you look at the methods of Image?

there is a method called:
paintIcon(component c, Graphics g, x, y);

but I don't know what c and g are or how to use them!

plz help
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#10: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


Quote:

Originally Posted by momotaro

there is a method called:
paintIcon(component c, Graphics g, x, y);

but I don't know what c and g are or how to use them!

plz help

Try again: did you look at the methods of Image?
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#11: Jun 12 '08

re: GUI (JLabel) .JPEG not recegnized


Quote:

Originally Posted by BigDaddyLH

Try again: did you look at the methods of Image?

thx i think I can figure it out from here ! :)
I was looking at the wrong place
Reply