Using JFileChooser to open and display an image
Hey everyone, okay so im pretty new to java, i normally use c but now im learning java...
What i need to do is open and select an image then display it the panel but am not sure how to do this..
this is what i have so far:
Code Java:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.imageio.*;
public class assignment1 extends JPanel implements ActionListener {
JLabel label = new JLabel();
JMenuItem openItem, quitItem;
BufferedImage image = null;
public static void main(String[] args) {
new assignment1();
}
public void actionPerformed(ActionEvent event) {
JMenuItem source = (JMenuItem)event.getSource();
if (source == openItem){
JFileChooser chooser = new JFileChooser();
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new ImageFilter());
int retVal = chooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
File myFile = chooser.getSelectedFile();
try {
image = ImageIO.read(myFile);
}
catch( IOException e ){
e.printStackTrace();
}
}
}
else if (source == quitItem){
System.out.println("Quitting....");
System.exit(0);
}
}
public assignment1(){
JFrame frame = new JFrame("make window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
//JPanel panel = new JPanel();
//add menu bar.
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
//FILE MENU !!
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
//OPEN ITEM TOOL
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
//CLOSE ITEM TOOL
quitItem = new JMenuItem("Quit Item");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
frame.setVisible(true);
ImageDemo panel = new ImageDemo();
frame.setContentPane(panel);
// JPanel panel = new JPanel();
// panel.setVisible(true);
}
public void paintComponent( Graphics g ){
if( image != null ){
Graphics2D g2 = (Graphics2D)g;
System.out.println("Calling paint");
g2.drawImage( image, 0, 0, null );
}
}
}
Re: Using JFileChooser to open and display an image
There are several parts to your problem. Which are you having trouble with?
Get the path to a file using JFileChooser
Read the file from the path as an image
Display the image in a JPanel.
Work on each of these steps one at a time and test/show the results using println() and then move to the next step.
Re: Using JFileChooser to open and display an image
Quote:
Originally Posted by
Norm
There are several parts to your problem. Which are you having trouble with?
Get the path to a file using JFileChooser
Read the file from the path as an image
Display the image in a JPanel.
Work on each of these steps one at a time and test/show the results using println() and then move to the next step.
I am able to select the file from the dialog box i want to know how to store that image in a buffer or somthing then display it under my menubar. thxs.
Re: Using JFileChooser to open and display an image
Look at the ImageIO class. It has methods for reading image files into Images.