J
jimmy
Hi All,
I am developing a GUI to display jpg images in a JPanel. I have
created two ways for the images to be selected: (i) a JFileChooser
button to select a single jpg, and (ii) a JFileChooser to select a
directory containing jpg files. Once the images have been selected,
they should be displayed on the JPanel.
In the case of opening the single file, this works as I hoped. However
when the open directory scenario is used, nothing is displayed.
Through the use of println statements I have been able to establish
the programme is executing the same Classes and Methods as in the
single image display. In addition to displaying an image, I have a
JLabel in which I display the name of the current image. Again for the
single image file selection, this works. For the open directory
scenario, only the name of the final image in the directory is
displayed (without the image).
Thinking that maybe there was not sufficient time for the image to be
displayed, I added a Thread.sleep(4000); line, but this did not help.
Included below are the two actionPerformed methods from each button,
plus the DrawImage and MyFilter Classes. I would appreciate if anyone
could indicate possible causes for the failure of the open directory
display method.
Many thanks,
Jimmy
BufferedImage image;
String filename;
DrawImage pic = null;
DrawImage pic2 = null;
String filename2;
BufferedImage image2 = null;
private void
openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(file);
}
catch (IOException ex) {
System.out.println("There was a problem opening
the selected file: " + ex);
}
//mainImagePanel is the target JPanel for the image
Graphics g=mainImagePanel.getGraphics();
pic = new DrawImage();
pic.setImage(image);
pic.paintComponent(g);
//Add label text
filename = file.getPath();
jLabel1.setText(filename);
}
}
private void
openBatchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
//JFileChooser to select a batch directory
JFileChooser chooserBatch = new JFileChooser();
chooserBatch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValBatch =
chooserBatch.showOpenDialog(chooserBatch);
File fileBatch = chooserBatch.getSelectedFile();
if (returnValBatch == JFileChooser.APPROVE_OPTION) {
System.out.println("Batch directory chosen: " +
fileBatch);
} else {
System.out.println("Error selecting batch directory: ");
}
//Get a file array of all jpgs in this directory
FilenameFilter only = new MyFilter("jpg");
File[] fileArray = fileBatch.listFiles(only);
//loop through jpg files
for (int i=0; i < fileArray.length; i++){
try {
image2 = ImageIO.read(fileArray);
} catch (IOException ex) {
Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE,
null, ex);
}
System.out.println("current file: " + fileArray);
//load image to main image panel
Graphics gg = mainImagePanel.getGraphics();
pic2 = new DrawImage();
pic2.setImage(image2);
pic2.paintComponent(gg);
//add filename to label
filename2 = fileArray.getPath();
jLabel1.setText(filename2);
}
}
/*****************************/
/*** Draw Main Image Panel ***/
/*****************************/
class DrawImage extends JPanel{
BufferedImage img;
public DrawImage() {
}
public void setImage(BufferedImage image){
this.img = image;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
if(img != null) {
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);
}
else {
System.out.println("Image passed for drawing to main
panel is null");
}
}
}
/***********************/
/*** Filename Filter ***/
/***********************/
public class MyFilter implements FilenameFilter {
String ext;
public MyFilter(String ext) {
this.ext = "." + ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
I am developing a GUI to display jpg images in a JPanel. I have
created two ways for the images to be selected: (i) a JFileChooser
button to select a single jpg, and (ii) a JFileChooser to select a
directory containing jpg files. Once the images have been selected,
they should be displayed on the JPanel.
In the case of opening the single file, this works as I hoped. However
when the open directory scenario is used, nothing is displayed.
Through the use of println statements I have been able to establish
the programme is executing the same Classes and Methods as in the
single image display. In addition to displaying an image, I have a
JLabel in which I display the name of the current image. Again for the
single image file selection, this works. For the open directory
scenario, only the name of the final image in the directory is
displayed (without the image).
Thinking that maybe there was not sufficient time for the image to be
displayed, I added a Thread.sleep(4000); line, but this did not help.
Included below are the two actionPerformed methods from each button,
plus the DrawImage and MyFilter Classes. I would appreciate if anyone
could indicate possible causes for the failure of the open directory
display method.
Many thanks,
Jimmy
BufferedImage image;
String filename;
DrawImage pic = null;
DrawImage pic2 = null;
String filename2;
BufferedImage image2 = null;
private void
openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(file);
}
catch (IOException ex) {
System.out.println("There was a problem opening
the selected file: " + ex);
}
//mainImagePanel is the target JPanel for the image
Graphics g=mainImagePanel.getGraphics();
pic = new DrawImage();
pic.setImage(image);
pic.paintComponent(g);
//Add label text
filename = file.getPath();
jLabel1.setText(filename);
}
}
private void
openBatchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
//JFileChooser to select a batch directory
JFileChooser chooserBatch = new JFileChooser();
chooserBatch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValBatch =
chooserBatch.showOpenDialog(chooserBatch);
File fileBatch = chooserBatch.getSelectedFile();
if (returnValBatch == JFileChooser.APPROVE_OPTION) {
System.out.println("Batch directory chosen: " +
fileBatch);
} else {
System.out.println("Error selecting batch directory: ");
}
//Get a file array of all jpgs in this directory
FilenameFilter only = new MyFilter("jpg");
File[] fileArray = fileBatch.listFiles(only);
//loop through jpg files
for (int i=0; i < fileArray.length; i++){
try {
image2 = ImageIO.read(fileArray);
} catch (IOException ex) {
Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE,
null, ex);
}
System.out.println("current file: " + fileArray);
//load image to main image panel
Graphics gg = mainImagePanel.getGraphics();
pic2 = new DrawImage();
pic2.setImage(image2);
pic2.paintComponent(gg);
//add filename to label
filename2 = fileArray.getPath();
jLabel1.setText(filename2);
}
}
/*****************************/
/*** Draw Main Image Panel ***/
/*****************************/
class DrawImage extends JPanel{
BufferedImage img;
public DrawImage() {
}
public void setImage(BufferedImage image){
this.img = image;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
if(img != null) {
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);
}
else {
System.out.println("Image passed for drawing to main
panel is null");
}
}
}
/***********************/
/*** Filename Filter ***/
/***********************/
public class MyFilter implements FilenameFilter {
String ext;
public MyFilter(String ext) {
this.ext = "." + ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}