Question about JFileChooser

Discussion in 'Mac Programming' started by jtalerico, Feb 15, 2006.

  1. macrumors 6502

    Joined:
    Nov 23, 2005
    #1
    So I am trying to open a file using JFileChooser... What does the Open button return? Do i have to set a actionListener for it? Because there is a Open button there by default in OS X. Any idea how to set an actionListener to something that is already created?
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    I'm not sure what the return code since I haven't looked it up recently. Here is a sample of my code where I'm trying to get the path to a file.

    Code:
      public void protein1MouseClicked(java.awt.event.MouseEvent e)
      {
    
        JFileChooser fc = new JFileChooser();
    
        int returnCode = fc.showDialog(FPFrame.this, "Select");
        if(returnCode == JFileChooser.APPROVE_OPTION)
        {
          file1 = fc.getSelectedFile();
        }
      } // end protein1MouseClicked()
    
     
  3. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #3
    I am with you until FPFrame.this, what exactly is that? What componet?
     
  4. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    It's the parent window/container, a JFrame.
     
  5. tuc
    macrumors 6502

    Joined:
    Aug 25, 2003
    #5
    JFileChooser vs. FileChooser

    btw, you might want to use a java.awt.FileChooser
    instead of JFileChooser. You get the OS native widget
    instead of an approximation.
     
  6. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #6
    If your app is in Swing, then keep it all swing, and stick with JFileChooser.

    The JFrame argument to the JFileChooser allows it to attach the JFileChooser dialog onto that JFrame, so it can modally block input to the JFrame.
     
  7. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #7
    The extra behaviours that come with JFileChooser far outweigh its not being native. Besides, AWT-based applications probably still don't look the same deployed on a number of platforms. :(
     
  8. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #8
    No worries, i have written my own little Client appy... It is hidious.. I have never been to good at the GUI stuff.. Anyone suggest some good GUI hints?
     
  9. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #9
    If you're doing anything with Swing, I highly recommend the book Core Java Foundation Classes by Kim Topley. This book is fantastic and saved me a lot of pain back when I was learning Swing programming. IF it's still in print/available, not sure about that. I'm not doing Java development today, but if I were, I know I'd still be referencing this book.
     
  10. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #10
    Yeah, I am not in school right now. I am doing a coop here in Raleigh. I am trying to teach myself some stuff, and build myself a program that I can use :)
     

Share This Page