I ma creating a Project on Java Uaing editor NetBeans 6.0

In this project, i want to put Dynemic image on JLabel which gets the Image Path from Database. It works Properly BUT....

The size of JLabel autometicaly adjust:(
If the Image size is 1024x672 then JLabel covers whole Swing JInnerFrame.

How to fix this problem.how to stretch the image to fit in the size of JLabel.

Code for Image is as Follows

int i = jTable1.getSelectedRow();
rstmp.absolute(i+1);
ImageIcon img = new ImageIcon(rstmp.getString("photo"));
LblPhoto.setIcon(img);

here,
rstmp = Recordset contain the records data
LblPhoto = JLabel which displays the Image

please give me sample code so i can Fix it.

Well, that's what you're telling to do (stay the original size, that is).

Try looking at the API docs for BufferedImage (and Graphics2D) and see if there is something there that can help you.

I tryed the following Code but it can't work

JblPhoto.add(new JLabel(new ImageIcon("Image Path")) {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(), getHeight(), null);
    }
});

Edited 3 Years Ago by Reverend Jim: Fixed formatting

No one told you to override paintComponent.

Load the image into a BufferedImage, get the Graphics2D, modify it, then create a ImageIcon from that.

I tryed following code to get image
image gathered into image object but can't displayed on panel (Now i am using JPanel instad on JLabel)
the JPanel class' paintComponent can't invock when i run the program in NetBeans.

public class NewJFrame extends javax.swing.JFrame {
    NewClass imgobj;
    Container con;
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        //Pan1 obj=new Pan1();]
        BufferedImage bimg = null;
        try
        {
                // Track the image to completion.
            bimg = ImageIO.read(new File("D:\\Books\\splash.png"));
            
       imgobj = new NewClass(bimg);
       imgobj.setImage(bimg);
      Image img =    imgobj.getImage(bimg);
      imgobj.prepareImage(img, this.getHeight(),this.getWidth(), null);
               
       getContentPane().add(imgobj,BorderLayout.CENTER);
        
       System.out.println("Image Readed...");
                 
      }
      catch(Exception e)
      {
            System.out.println("The error in Frame..."+e);
        }
        
    }
     
     
              
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JPanel NewClass;
    // End of variables declaration
    
}
class NewClass extends JPanel implements Serializable {

    Image image = null;

    public NewClass() {      }

    public NewClass(Image image) {
        this.image = image;
        System.out.println("Class Panel Constructed...");
    }
   

    public void setImage(Image image){
        this.image = image;
      
        System.out.println("Image set...");
    }
    
    public Image getImage(Image image){
        return image;
    }
    
    @Override
    public void paint(Graphics g) {
        super.paintComponent(g); //paint background
        if (image != null) { //there is a picture: draw it
            int height = this.getSize().height;
            int width = this.getSize().width;         
            g.drawImage(image,0,0, width, height, this);
            System.out.println("paint Component method ...");
               //g.drawImage(image, 0, 0, this); //original image size 
         }  //end if
    } //end paint
} //end class

in above code all methods execute when i execute program but No Display on JFrame or panel

Please HELP!!!!!!!!!:'(

You should be overriding paintComponent(), not paint(), in your "NewClass" class.
(And find a new name for that class because NewClass is an awful name for a component)

:) hay man It solved by following code it not works because Paint method not initialized

public class NewJFrame extends javax.swing.JFrame {
    LoadImg imgobj;
    Container con;
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        //Pan1 obj=new Pan1();]
        BufferedImage bimg = null;
        try
        {
       
        // Track the image to completion.
            bimg = ImageIO.read(new File("D:\\Books\\1.jpg"));
            JPanel cont = (JPanel) getContentPane();
            //cont.setLayout(new BorderLayout());
            imgobj = new LoadImg(bimg);
            imgobj.setBounds(50,100, 200, 200);
            cont = new NewClass(bimg);
            imgobj.setImage(bimg);
            Image img = imgobj.getImage(bimg);
            imgobj.prepareImage(imgobj.getImage(bimg), this.getHeight(),this.getWidth(), null);
            System.out.println("Image Readed...");
            getContentPane().add(imgobj,BorderLayout.CENTER);
            
        }
        catch(Exception e)
        {
            System.out.println("the error in frame..."+e);
        }
        
    }
  }

class LoadImg extends JPanel implements Serializable {
    Image image = null;
    
    public LoadImg() {    
    }

    public LoadImg(Image image) {
        this.image = image;
        System.out.println("Class Panel Constructed...");
    }
   

    public void LoadImg(Image image){
        this.image = image;
      
        System.out.println("Image set...");
    }
    
    public Image LoadImg(Image image){
        return image;
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //paint background
        if (image != null) { //there is a picture: draw it
            int height = this.getSize().height;
            int width = this.getSize().width;         
            g.drawImage(image,0,0, width, height, this);
            System.out.println("paint Component method ...");
               //g.drawImage(image, 0, 0, this); //original image size 
         }  //end if
    } //end paint
} //end class

But now i want to chang Image at runtime
fetching record to database at runtime. but the image can't drown
can i use method like update() is there please tell me the method name
:idea:

And Yes Thanks for Replay :cool:

Sorry but now when i test that code on my fully Application i realize that it Doesen't Work. can you give me another code or idea ?which i use for displaying Image.

Sorry but above code is wrong and doesen't work. don't look at that junk code it's a brain eater code.

Hi,

In an attempt to paint multiple icons between text in a jlabel...
for eg., <text1> <icon1> <text2> <icon2> a sequence like this in a single jlabel.
kindly assist me in going about doing this.

Thanks

Well dear i saw ur problem according to me u shoud use the Image class instead of LabelImage. bqz in paint method we use the drawImage(img,0,0,200,200,this); in this method we pass the six arguments and set the desired size of the any size of Image.
u have to import the java.net package.. for this
Thanx
Harpreet Singh

Is it not possible to Stretch the image JLabel!
or it is possible , if yes plz post the code for me..........

Is it not possible to Stretch the image JLabel!
or it is possible , if yes plz post the code for me..........

I haven't tried it but here is a suggestion, you can use.


The JLabel constructor takes as argument an Icon. Actually you call it with argument an ImageIcon which is the class that implements the Icon class:

ImageIcon icon = new ImageIcon("file name");
JLabel label = new JLabel(icon);

The JLabel has a method that returns that Icon: getIcon()

So just take that ImageIcon instance. If you see the API of that class there is a method getImage.

ImageIcon icon = new ImageIcon("file name");
Image img1 = icon.getImage();

Take a look at the Image API. There is a method called getScaledInstance. That is the method that you need:

ImageIcon icon = new ImageIcon("file name");
Image img1 = icon.getImage();
Image img2 = img1.getScaledInstance(.....);

Now that you have the new img2 scaled instance, you can use it to create a new ImageIcon instance (by using the right constructor from the API)
and then with that ImageIcon instance you can change the icon of the JLabel with the setIcon method of the JLabel API.


Just take a look at the API of those classes:
javax.swing.JLabel
javax.swing.ImageIcon: This one implements the Icon interface that the JLabel uses
java.awt.Image: For you to use the getScaledInstance method

This question has already been answered. Start a new discussion instead.