Ok so here is the assignment:
Create a simple picture viewer that will allow the user to click a button and then select an image from the file system and have that image displayed in a JLabel.
Here is the code I have:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main implements ActionListener{
JButton openButton;
JFileChooser fc;
File fileName;
JLabel image;
JFrame frame;
public Main(){
frame = new JFrame("Picture Viewer");
frame.setBounds(100,100,750,750);
frame.setLayout(null);
frame.setVisible(true);
openButton = new JButton("Choose an image to display...");
openButton.setBounds(0,0,750,50);
openButton.setLayout(null);
openButton.setVisible(true);
frame.add(openButton);
openButton.addActionListener(this);
}
public static void main(String[] args){
Main m = new Main();
}
@Override
public void actionPerformed(ActionEvent arg0) {
fc = new JFileChooser();
fc.showOpenDialog(null);
fileName = fc.getSelectedFile();
ImageIcon icon = new ImageIcon(fileName.getPath());
image = new JLabel(icon);
image.setBounds(15,65,720,655);
image.setLayout(null);
image.setVisible(true);
frame.add(image);
}
}
and here is the error I'm getting:
sun.awt.image.ImageFormatException: Unsupported color conversion request
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)