Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default JFileChooser - is it actually possible to upload a pic and store it into a variable?

    Just how the title says...is it possible to use JFileChooser to select a file from your computer...an image file....and store it into an Icon variable?

    Kind regards,
    Adi

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

  3. #3
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    yeah I actually found them while I was searching on google like crazy but I couldn't understand some things of it...I know how to put a filechooser on my gui...the problem is I don't understand how exactly I can add an action to that filechooser

  4. #4
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Hey buddy try this code, i have also using the same in my application::


    if (fileResult == JFileChooser.APPROVE_OPTION)
    {
    File f = photoChooser.getSelectedFile(); //Select the file
    String path = f.getPath(); //get the path of file
    Image image = Toolkit.getDefaultToolkit().getImage(path); // Now from that path get an image object which stores the file as an image object
    }

    //Now call that image object as for example a jLable icon:

    imageLable.setIcon(new ImageIcon(image));



    Hope this will help you....
    Much luck!

  5. #5
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by asifzbaig View Post
    Hey buddy try this code, i have also using the same in my application::


    if (fileResult == JFileChooser.APPROVE_OPTION)
    {
    File f = photoChooser.getSelectedFile(); //Select the file
    String path = f.getPath(); //get the path of file
    Image image = Toolkit.getDefaultToolkit().getImage(path); // Now from that path get an image object which stores the file as an image object
    }

    //Now call that image object as for example a jLable icon:

    imageLable.setIcon(new ImageIcon(image));


    Hope this will help you....
    Much luck!
    hey there !...first of all thanks very much for the help ..but I still got one question...do you have to use this code in your actionPerformed method ? or is there another method specific for the jFileChooser like ItemListener or something? I mean how do you add the event if the "Open" button (from the filechooser) is pressed? or is it this line :
    if (fileResult == JFileChooser.APPROVE_OPTION) ...approve_option = the open button?

    Thanks !!

  6. #6
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Dear APPROVE_OPTION is static final integer value returned by JFileChooser...

    Simply talking it is the "confirm" integer that makes sure that OPEN button is pressed successfully, or you can say that the file is selected successfully......

    Before if statement you have to initialize the fileResult integer as:

    int fileResult = photoChooser.showOpenDialog(null);

    this will done two work: 1) opens file chooser
    2) initializes fileResult integer

    Now, rewrite whole statements in that button action performed method by which you want to open the file chooser.....

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please don't spoon feed, the op doesn't really get much help if he isn't thinking about it at all.

    Also, if you do post code, please use code tags:
    [code]
    YOUR CODE HERE
    [/code]

  8. #8
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by asifzbaig View Post
    Dear APPROVE_OPTION is static final integer value returned by JFileChooser...

    Simply talking it is the "confirm" integer that makes sure that OPEN button is pressed successfully, or you can say that the file is selected successfully......

    Before if statement you have to initialize the fileResult integer as:

    int fileResult = photoChooser.showOpenDialog(null);

    this will done two work: 1) opens file chooser
    2) initializes fileResult integer

    Now, rewrite whole statements in that button action performed method by which you want to open the file chooser.....
    thanks a lot !

Similar Threads

  1. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  2. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 07:22 PM
  3. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 12:21 AM
  4. how to store the previous of a variable
    By Randa in forum New To Java
    Replies: 7
    Last Post: 09-22-2008, 08:16 PM
  5. Replies: 2
    Last Post: 12-05-2007, 01:19 PM

Posting Permissions

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