Hi,
In my application using the browse button i load the image.
I am creating an instance of ImageIcon by giving the above specific path in the constructor.
"ImageIcon icon1 = new ImageIcon(selectedFile.getAbsolutePath()); "
Then i use this icon instance to paint in a JPanel embeded in the scrollpane.
In the paintComponent method of JPanel I get the width, height of the image icon and set the preferred size
of the panel to display the full image.
Now because of some specific requirement in the project i resize(enlarged/reduce) the same image and try to reload the image through the browse button.
And this resized image should be painted in the JPanel.
How to resize the image : Open in paint and enlarge or reduce the size of the image.
But the image size does not changes in the panel.
But if i close the application and run it again, then the new height & width is taken.
I have gone through the constructor of ImageIcon, it says in the loadImage() method media trakcer already
loaded the image.
I think because of this it is not taking the new resized image.
Can anybody give me a solution to this problem.
I am attaching sample code for this.
Need not to go through the initComponents() method, as it is auto generated code from netbeans.
import java.awt.image.BufferedImage;
import javax.swing.JFileChooser;
import java.applet.*;
import java.io.File;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileFilter;
public class ImageSizeCheckFrame extends javax.swing.JFrame {
File selectedFile ;
int counter = 0;
ImageIcon icon;
/** Creates new form ImageSizeCheckFrame */
public ImageSizeCheckFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
prevImgPathLBL = new javax.swing.JLabel();
prevImgWidthLBL = new javax.swing.JLabel();
prevImgHeightLBL = new javax.swing.JLabel();
currImgPathLBL = new javax.swing.JLabel();
currImgWidthLBL = new javax.swing.JLabel();
currImgHeightLBL = new javax.swing.JLabel();
prevImgPathTXT = new javax.swing.JTextField();
prevImgWidthTXT = new javax.swing.JTextField();
prevImgHeightTXT = new javax.swing.JTextField();
currImgPathTXT = new javax.swing.JTextField();
currImgWidthTXT = new javax.swing.JTextField();
currImgHeightTXT = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Calculate");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
prevImgPathLBL.setText("Previous Image path:");
prevImgWidthLBL.setText("Previous Iamge width:");
prevImgHeightLBL.setText("Previous Image height:");
currImgPathLBL.setText("Current Image path:");
currImgWidthLBL.setText("Current Iamge width:");
currImgHeightLBL.setText("Current Image height:");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(48, 48, 48)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButton2)
.add(layout.createSequentialGroup()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(27, 27, 27)
.add(jButton1))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(prevImgPathLBL)
.add(prevImgWidthLBL)
.add(prevImgHeightLBL)
.add(currImgPathLBL)
.add(currImgWidthLBL)
.add(currImgHeightLBL))
.add(30, 30, 30)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(prevImgWidthTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(prevImgPathTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(prevImgHeightTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgPathTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgWidthTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgHeightTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(prevImgPathLBL))
.add(prevImgPathTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(prevImgWidthLBL)
.add(prevImgWidthTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(7, 7, 7)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(prevImgHeightLBL)
.add(prevImgHeightTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(49, 49, 49)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgPathLBL)
.add(currImgPathTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgWidthLBL)
.add(currImgWidthTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(7, 7, 7)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgHeightLBL)
.add(currImgHeightTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(47, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(counter > 0){
prevImgPathTXT.setText(selectedFile.getAbsolutePath());
icon = new ImageIcon(selectedFile.getAbsolutePath());
prevImgWidthTXT.setText(icon.getIconWidth()+"");
prevImgHeightTXT.setText(icon.getIconHeight()+"");
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
ImageFileFilter filter = new ImageFileFilter();
fileChooser.addChoosableFileFilter(filter);
int retVal = fileChooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
jTextField1.setText(fileChooser.getSelectedFile().getAbsolutePath());
selectedFile = fileChooser.getSelectedFile();
}
counter++;
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currImgPathTXT.setText(selectedFile.getAbsolutePath());
ImageIcon icon1 = new ImageIcon(selectedFile.getAbsolutePath());
currImgWidthTXT.setText(icon1.getIconWidth()+"");
currImgHeightTXT.setText(icon1.getIconHeight()+"");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ImageSizeCheckFrame obj = new ImageSizeCheckFrame();
obj.setSize(700, 400);
obj.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel currImgHeightLBL;
private javax.swing.JTextField currImgHeightTXT;
private javax.swing.JLabel currImgPathLBL;
private javax.swing.JTextField currImgPathTXT;
private javax.swing.JLabel currImgWidthLBL;
private javax.swing.JTextField currImgWidthTXT;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel prevImgHeightLBL;
private javax.swing.JTextField prevImgHeightTXT;
private javax.swing.JLabel prevImgPathLBL;
private javax.swing.JTextField prevImgPathTXT;
private javax.swing.JLabel prevImgWidthLBL;
private javax.swing.JTextField prevImgWidthTXT;
// End of variables declaration
}
class ImageFileFilter extends FileFilter{
private String description = "*.gif,*.jpg,*.png,*.jpeg";
private ArrayList list = new ArrayList();
{
list.add(".gif");
list.add(".jpg");
// anurag start
// don't show bmp files for selection
//list.add(".bmp");
list.add(".jpeg");
list.add(".png");
//list.add(".JPG");
//list.add(".JPEG");
// anurag end
}
public String getDescription(){
return description;
}
public boolean accept(File file){
if(file.isDirectory()) return true;
String fileName = file.getName().toLowerCase();
for(int i=0;i<list.size();i++){
String n = (String)list.get(i);
if(fileName.endsWith(n)) return true;
}
return false;
}
}