(Creating a Swing ImageViewer control)image not displayed or refreshing on JLabel
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nick_Selvam
Posted On:   Wednesday, August 18, 2004 11:06 AM

I am creating an image viewer thru which with a click of a JButton the user gets a JFileChooser to pick an image of type JPG,TIFF,GIF etc. Once the image file is selected, it gets displayed on the JLabel and also a reference of the file is stored in an ImageIcon variable to be saved into the database. So far, I am able to click at the JButton to choose the picture file, but when selected the image doesnt get displayed on the JLabel. I have to resize the JFrame or JInternaFrame, to have the image displayed. However, if I were to choose another picture, the new image doesnt get displayed on the JLabel, despite resizing the JFrame. Below is the code for the program named, JPicturePane.java. I have made use of some code from the Java Tutorial, on ScrollPaneDemo. Thos   More>>

I am creating an image viewer thru which with a click of a JButton the user gets a JFileChooser to pick an image of type JPG,TIFF,GIF etc. Once the image file is selected, it gets displayed on the JLabel and also a reference of the file is stored in an ImageIcon variable to be saved into the database. So far, I am able to click at the JButton to choose the picture file, but when selected the image doesnt get displayed on the JLabel. I have to resize the JFrame or JInternaFrame, to have the image displayed. However, if I were to choose another picture, the new image doesnt get displayed on the JLabel, despite resizing the JFrame. Below is the code for the program named, JPicturePane.java. I have made use of some code from the Java Tutorial, on ScrollPaneDemo. Those files are ImageFilter,ImagePreview dan Utils and can be found on at the site: http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html.
The part of the code where the image is layed on the JLabel
is found at the actionPerformed() method in JPicturePane.java.

---------------------------------------------------------------
package ttms.gui.custom;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.filechooser.*;

import ttms.service.util.ImageFilter;
import ttms.service.util.ImagePreview;

public class JPicturePane extends JPanel implements ActionListener {

JPanel photoPane;
JPanel buttonPane;
JButton chsButton;
JFileChooser fc;
ImageIcon img;
JLabel photoLabel;

public JPicturePane (int width, int height) {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
//setBorder(BorderFactory.createEtchedBorder());
setMinimumSize(new Dimension(width+30,height));
setPreferredSize(new Dimension(width+30,height));
setMaximumSize(new Dimension(width+30,height));

photoPane = new JPanel();
photoPane.setMinimumSize(new Dimension(width,height));
photoPane.setPreferredSize(new Dimension(width,height));
photoPane.setMaximumSize(new Dimension(width,height));
photoPane.setBorder(BorderFactory.createEtchedBorder());

buttonPane = new JPanel();
//buttonPane.setBorder(BorderFactory.createEtchedBorder());
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.Y_AXIS));
buttonPane.setMinimumSize(new Dimension(30,height));
buttonPane.setPreferredSize(new Dimension(30,height));
buttonPane.setMaximumSize(new Dimension(30,height));
buttonPane.add(Box.createRigidArea(new Dimension(0,height-20)));

chsButton = new JButton("...");
chsButton.setActionCommand("attach");
chsButton.addActionListener(this);
chsButton.setMinimumSize(new Dimension(30,20));
chsButton.setPreferredSize(new Dimension(30,20));
chsButton.setMaximumSize(new Dimension(30,20));
buttonPane.add(chsButton);

add(photoPane);
add(buttonPane);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("attach") ) {
fc = new JFileChooser();
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);
fc.setAccessory(new ImagePreview(fc));
int returnVal = fc.showDialog(this, "Attach");
if (returnVal == JFileChooser.APPROVE_OPTION) {
File imgFile = fc.getSelectedFile();
img = new ImageIcon(imgFile.getPath(), imgFile.getName());
System.out.println(imgFile.getPath());
photoLabel = new JLabel();
photoLabel.setIcon(img);
photoPane.add(photoLabel);
photoLabel.setPreferredSize(photoPane.getPreferredSize());
}

}
}

public static void main(String args[]) {
JFrame f = new JFrame();
//JPicturePane jpp = new JPicturePane("potrait",125,150);
JPicturePane jpp = new JPicturePane(200,175);
f.getContentPane().add(jpp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);

}

}
---------------------------------------------------------------

Please have a look at the and help me out
as to how I can solve this problem. If it works, I would
want to post it for open-source. It would be valuable for the community.

Please, someone help me..

Selvam

   <<Less
About | Sitemap | Contact