Results 1 to 18 of 18
  1. #1
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default how to display image selected by JFileChooser

    hello,
    i want to display an image file that is being selected by JFileChooser. i am not being able display the file. can u plz read below code & help me how should i go ahead


    code:
    import java.awt.*;
    import java.lang.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    import java.awt.image.*;




    class OpenDialog
    {
    OpenDialog()
    {
    JFileChooser fc=new JFileChooser();
    fc.addChoosableFileFilter(new ImageFilter());
    int returnVal = fc.showOpenDialog(OpenDialog.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
    ImagePanel panel=new ImagePanel(fc.getSelectedFile.getPath());
    }

    }
    }



    class ImageFilter extends javax.swing.filechooser.FileFilter
    {

    //Accept all directories and all gif, jpg, tiff, bmp or png files.
    public boolean accept(File f)
    {
    if (f.isDirectory())
    {
    return true;
    }

    String extension = Utils.getExtension(f);
    if (extension != null)
    {
    if (extension.equals(Utils.tiff)||

    extension.equals(Utils.bmp) ||

    extension.equals(Utils.tif) ||
    extension.equals(Utils.gif) ||
    extension.equals(Utils.jpeg)||
    extension.equals(Utils.jpg) ||
    extension.equals(Utils.png))
    {
    return true;
    }
    else
    {

    return false;
    }
    }

    return false;
    }

    //The description of this filter
    public String getDescription()
    {
    return "Just Images";
    }
    }



    class MenuHandler implements ActionListener
    {
    GUI gui;
    public MenuHandler(GUI gui)
    {
    this.gui=gui;
    }

    public void actionPerformed(ActionEvent ae)
    {
    String arg=(String)ae.getActionCommand();
    if(arg.equals("Open"))
    {
    OpenDialog od=new OpenDialog();
    od.setVisible(true);
    }

    }
    }


    class GUI
    {

    GUI()
    {
    //Create and set up the window.
    JFrame frame = new JFrame("Image Editor");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


    //Created menu bar.


    //Created menu

    //Set the menu bar and add the content pane.
    frame.setJMenuBar(MenuBar);
    Container contentPane=frame.getContentPane();

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    frame.setLocation(75,75);
    }
    }


    public class ImageEditor
    {
    public static void main(String args[])
    {
    GUI gui=new GUI();
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    For starters, since you have a line of code that says
    Java Code:
    ImagePanel panel=new ImagePanel(fc.getSelectedFile.getPath());
    you would need to have a class named ImagePanel with a constructor that takes a String parameter. Which looks like a bad idea, since it is apparent that the said class would need to construct a File from the String either explicitly or within the code of a JRE class.

    Also, use the code tags to make your code readable. They're there for a purpose.

    can u plz
    Generally, when someone asks for something using "plz" because it's shorter than "please" I answer "no" because it's shorter than "yes"

    Recommended reading:
    How To Ask Questions The Smart Way

    db
    Last edited by DarrylBurke; 07-17-2010 at 01:42 PM.

  3. #3
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    sorry for my mistake. as i am new to the forum, i didnt get how to do this.this is my code:
    code:
    import java.awt.*;
    import java.lang.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    import java.awt.image.*;




    class OpenDialog
    {
    OpenDialog()
    {
    JFileChooser fc=new JFileChooser();
    fc.addChoosableFileFilter(new ImageFilter());
    int returnVal = fc.showOpenDialog(OpenDialog.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
    ImagePanel panel=new ImagePanel(fc.getSelectedFile.getPath());
    }

    }
    }



    class ImageFilter extends javax.swing.filechooser.FileFilter
    {

    //Accept all directories and all gif, jpg, tiff, bmp or png files.
    public boolean accept(File f)
    {
    if (f.isDirectory())
    {
    return true;
    }

    String extension = Utils.getExtension(f);
    if (extension != null)
    {
    if (extension.equals(Utils.tiff)||

    extension.equals(Utils.bmp) ||

    extension.equals(Utils.tif) ||
    extension.equals(Utils.gif) ||
    extension.equals(Utils.jpeg)||
    extension.equals(Utils.jpg) ||
    extension.equals(Utils.png))
    {
    return true;
    }
    else
    {

    return false;
    }
    }

    return false;
    }

    //The description of this filter
    public String getDescription()
    {
    return "Just Images";
    }
    }



    class MenuHandler implements ActionListener
    {
    GUI gui;
    public MenuHandler(GUI gui)
    {
    this.gui=gui;
    }

    public void actionPerformed(ActionEvent ae)
    {
    String arg=(String)ae.getActionCommand();
    if(arg.equals("Open"))
    {
    OpenDialog od=new OpenDialog();
    od.setVisible(true);
    }

    }
    }


    class GUI
    {

    GUI()
    {
    //Create and set up the window.
    JFrame frame = new JFrame("Image Editor");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


    //Created menu bar.


    //Created menu

    //Set the menu bar and add the content pane.
    frame.setJMenuBar(MenuBar);
    Container contentPane=frame.getContentPane();

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    frame.setLocation(75,75);
    }
    }


    public class ImageEditor
    {
    public static void main(String args[])
    {
    GUI gui=new GUI();
    }

    }
    then how should i proceed?

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Welcome :)

    I dearly hope I got the question... anyway...

    To solve this thread I recommend reading this How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    from this point

    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    actualy my question is about displaying image file chosen by Jfilechooser. In my code, filechooser is selecting the file, but i am not getting how should it be displayed on the content pane of the frame.
    Should i add below snippet in my code & how will the selected image be displayed on the content pane?
    Last edited by khushi.cutegal; 07-18-2010 at 09:27 AM.

  6. #6
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    i have made some changes, but image is not displaying. what changes should i do to make the selected image file display on the content pane?
    Java Code:
    class OpenDialog extends Component
    {  
        OpenDialog()
        {	
    	JFileChooser fc=new JFileChooser();
    	fc.addChoosableFileFilter(new ImageFilter());
            int returnVal = fc.showOpenDialog(OpenDialog.this);
    	if (returnVal == JFileChooser.APPROVE_OPTION)
    	{
    	    try
    	    {
    		ImagePanel panel= new ImagePanel(fc.getSelectedFile());
    	    }
    	    catch(IOException ex)
    	    {
    		JOptionPane.showMessageDialog(this, "Could not open 
    
    file", "Error", JOptionPane.ERROR_MESSAGE);		
    	    }
    	    
    	}
        }   
    }
    
    class ImagePanel extends JPanel
    {
        Image img;
        JPanel panel;
        File file;
        ImagePanel(File file)  throws IOException
        {
    	this.file=file;
    	img=ImageIO.read(file);
        }
         public void paintComponent(Graphics g) 
        {
    	super.paintComponent(g);
    	if(img != null) 
    	{
    		super.paint(g);
    		g.drawImage(img, 0, 0, this);
    		
    	}
        }
    }
    class ImageFilter extends javax.swing.filechooser.FileFilter
    {
    
        //Accept all directories and all gif, jpg, tiff, bmp or png files.
        public boolean accept(File f)
        {
            if (f.isDirectory())
            {
                 return true;
            }
    
            String extension = Utils.getExtension(f);
            if (extension != null)
            {
                if (extension.equals(Utils.tiff)|| 
    
                   extension.equals(Utils.bmp) || 
    
                    extension.equals(Utils.tif) ||
                    extension.equals(Utils.gif) ||
                    extension.equals(Utils.jpeg)||
                    extension.equals(Utils.jpg) ||
                    extension.equals(Utils.png))
                    {
                        return true;
                    }
                   else
                   {
    
                          return false;
                   }
            }
    
            return false;
        }
    
        //The description of this filter
         public String getDescription()
        {
            return "Just Images";
        }
    }
    
    
    
    class MenuHandler implements ActionListener
    {
        GUI gui;
        public MenuHandler(GUI gui)
        {
            this.gui=gui;
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            String arg=(String)ae.getActionCommand();
            if(arg.equals("Open"))
            {
                OpenDialog od=new OpenDialog();
                od.setVisible(true);
            }
    
        }
    }
    
    
    class GUI
    {
    
        GUI()
        {
        //Create and set up the window.
        JFrame frame = new JFrame("Image Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    
    
    //Created menu bar.
    
    
    //Created menu
    
        //Set the menu bar and add the content pane.
        frame.setJMenuBar(MenuBar);
        Container contentPane=frame.getContentPane();
    
        
        frame.pack();
        frame.setVisible(true);
        frame.setLocation(75,75);
        }
    }
    
    
    public class ImageEditor
    {
        public static void main(String args[])
        {
            GUI gui=new GUI();
        }
    
    }
    Last edited by khushi.cutegal; 07-19-2010 at 03:31 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Nowhere do I see code that adds the ImagePanel to the GUI. I do see some meaningless code comments -- you need to remember that a computer is incredibly stupid: it does only what you tell it to do, not what you vaguely want it to do.

    db

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Also, do you usually write your code all left-aligned, without indents?

    And never never call super.paint(...) from paintComponent(...) -- in fact, that would probably have resulted in an infinite recursion if your ImagePanel had actually been in a visible hierarchy.

    db

  9. #9
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    That is what i am not getting how to add image panel to GUI? Actualy i am new to java thats why enable to make it out,please help me... What should i do after getting file selected from JFilechooser....?

    I write the code with indents not all left aligned... I didn't noticed while posting this code. now i have made it indented

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Oh, no :)

    JFileChooser is just for returning file absolute path that's all :)
    Just to refresh my fingers...
    Things you need after the file path has been detected is using code like a

    Java Code:
    int a=fc.showOpenDialog(null);
    Image image;
    ImageIcon icon;
    JLabel label=new JLabel();
    
    if(a==JFileChooser.APPROVE_OPTION)
    {
    
      image=ImageIO.read(new File(fc.getSelectedFile()));
      icon=new ImageIcon(image);
      label.setIcon(icon);
      
    }
    That is a classic case of using file path ;)

    For more information I recommend reading this http://www.java2s.com/Tutorial/Java/...NCELOPTION.htm
    Last edited by Webuser; 07-19-2010 at 06:57 PM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  12. #12
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    Cross posting is no good I guess :(

  13. #13
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Oh sorry, i didn't know about cross-posting
    Java Code:
    class OpenDialog extends Component
    { 
        BufferedImage img;
        ImageIcon icon;
        JLabel label=new JLabel();
        OpenDialog()
        {	
    	JFileChooser fc=new JFileChooser();
    	fc.setFileFilter(new ImageFilter());
            int status = fc.showOpenDialog(null);
    	if (status == JFileChooser.APPROVE_OPTION)
    	{
    	    try
    	    {
    		File file = fc.getSelectedFile();
            	img=ImageIO.read(file);
    	        icon=new ImageIcon(img);
    	        label.setIcon(icon);
    	    }
    	    catch(IOException ex)
    	    {
    	    }  
    	}
        }   
    }
    In above code, label is not added to the frame/content pane, so my question is how to add this label to the frame that is created in other class:
    Java Code:
    class GUI
    {
    
        GUI()
        {
        //Create and set up the window.
        JFrame frame = new JFrame("Image Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    
    
    //Created menu bar.
    
    
    //Created menu
    
        //Set the menu bar and add the content pane.
        frame.setJMenuBar(MenuBar);
        Container contentPane=frame.getContentPane();
    
        
        frame.pack();
        frame.setVisible(true);
        frame.setLocation(75,75);
        }
    }
    
    
    public class ImageEditor
    {
        public static void main(String args[])
        {
            GUI gui=new GUI();
        }
    
    }
    Last edited by khushi.cutegal; 07-20-2010 at 08:55 AM.

  14. #14
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    frame.getContentPane().add(label);
    That's all :)

    P.S. But still I recommend you to read Java tutorials more closely. Try each tutorial by running and see what you get if to modify them...
    Last edited by Webuser; 07-20-2010 at 06:27 PM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  15. #15
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    i tried:
    Java Code:
    class OpenDialog extends Component
    { 
        BufferedImage img;
        ImageIcon icon;
        JLabel label=new JLabel();
        JFrame frame;
        OpenDialog()
        {	
    	JFileChooser fc=new JFileChooser();
    	fc.setFileFilter(new ImageFilter());
            int status = fc.showOpenDialog(null);
    	if (status == JFileChooser.APPROVE_OPTION)
    	{
    	    try
    	    {
    		File file = fc.getSelectedFile();
            	img=ImageIO.read(file);
    	        icon=new ImageIcon(img);
    	        label.setIcon(icon);
                    frame.getContentPane().add(label);
    	    }
    	    catch(IOException ex)
    	    {
    	    }  
    	}
        }   
    }
    but exception is occuring:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at OpenDialog.<init>(ImageEditor.java:39)
    at MenuHandler.actionPerformed(ImageEditor.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:20
    13)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
    a:2336)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
    .java:405)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:260
    )
    at javax.swing.AbstractButton.doClick(AbstractButton. java:375)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:1
    238)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMen
    uItemUI.java:1282)
    at java.awt.Component.processMouseEvent(Component.jav a:6100)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3288)
    at java.awt.Component.processEvent(Component.java:586 5)
    at java.awt.Container.processEvent(Container.java:209 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4461)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2157)
    at java.awt.Component.dispatchEvent(Component.java:42 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4455
    )
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4119)

    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4049)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Window.dispatchEventImpl(Window.java:2555 )
    at java.awt.Component.dispatchEvent(Component.java:42 87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 605)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
    ad.java:276)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
    java:201)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.java:191)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:186)

    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:178)

    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:139)

  16. #16
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    I think the code should be in the outer the if block

    Java Code:
    File file = fc.getSelectedFile();
    it is null because the file chooser dialog window is closed and nothing can be selected then ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    xception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at OpenDialog.<init>(ImageEditor.java:39)
    at MenuHandler.actionPerformed(ImageEditor.java:155)
    What code is at line 39 in ImageEditor? What reference at that line is null?

  18. #18
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks a lot for all your replies... :)
    problem of null pointer exception is solved now. If i find any difficulty afterwards, i'll ask you.
    :) :)

Similar Threads

  1. How to display image in JEditorPane
    By usccar in forum New To Java
    Replies: 1
    Last Post: 12-10-2009, 06:34 AM
  2. How to display image in a panel with JFileChooser
    By srisar in forum AWT / Swing
    Replies: 14
    Last Post: 10-11-2009, 06:06 PM
  3. Replies: 2
    Last Post: 04-24-2009, 08:10 AM
  4. How to display image ?
    By Birkoff in forum AWT / Swing
    Replies: 7
    Last Post: 06-09-2008, 07:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •