Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using JFileChooser to open and display an image

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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 );
    			}
    		}
    }
    Last edited by helloworld922; July 31st, 2010 at 09:18 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using JFileChooser to open and display an image

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using JFileChooser to open and display an image

    Look at the ImageIO class. It has methods for reading image files into Images.

Similar Threads

  1. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  2. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM
  3. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  4. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM
  5. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM