Results 1 to 8 of 8

Thread: FileChooser

  1. #1
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default FileChooser

    I'm trying to work with my first FileChooser and was wondering if I am required to create a listening object for the cancel and open buttons, or does the FileChooser provide one with its actionPerformed method?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Unsub View Post
    I'm trying to work with my first FileChooser and was wondering if I am required to create a listening object for the cancel and open buttons, or does the FileChooser provide one with its actionPerformed method?
    I don't think I understand your question; when you show your dialog with the showOpenDialog( ... ) or showSaveDialog( ... ) method that method returns a status indicating which button was pressed. All you have to do is act accordingly.

    kind regards,

    Jos

  3. #3
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    Thanks JosAh,

    sorry I wasn't completely clear on what I wanted, but your suggestion lead me in the right direction.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    File chooser is in two forms, either to open a file or to save a file in your hard disk. showOpenDialog() and showSaveDialog() methods are use respectively to do that. In each file chooser dialog you can see either Open or Save button with the Cancel button (and some other tool buttons as well, like create new folder, etc..) When you click on a button, it'll return different status, which can be use to recognized which button you click, separately. Hope it's clear now, that's what JosAH explains to you.

  5. #5
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    Thanks again for the reply.

    I've managed to work out the FileChooser and the showOpenDialog() stuff, but now I need to find a way to get what file the user has selected once the Open button has been pressed.

    I checked out the docs and came across the getSelectedFile() method which returns an int but I need to get the string format of the selectedFile. Is there a method in which returns a string.

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

    Default

    Quote Originally Posted by Unsub View Post
    I checked out the docs and came across the getSelectedFile() method which returns an int
    That method does not return int. Go back to the docs.

    Quote Originally Posted by Unsub View Post
    Is there a method in which returns a string.
    1. Why would you want a String in the first place, when you're selecting a File?
    2. File has methods that return String.

    db

  7. #7
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    i'm developing a school project and one of the elements that I am trying to implement is to give the user the option to upload a profile picture.

    I wanted the FileChooser to return a string so that I could use it as the URL for the photo.

    I am under the assumption that the easiest way to do this was to create a JLabel and then use the file's URL to display the image.

    Like so:
    Java Code:
      import javax.swing.*;
    
    import java.awt.*;
    
    public class DemoImage extends JFrame {
    
        public void showImage() {
            // creates the actual frame with title 'My GUI' and dimensions
    
            JFrame frame = new JFrame("My GUI");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(200, 50);
    
            frame.setResizable(true);
    
            frame.setLocationRelativeTo(null);
            // Inserts the image icon
    
            String imgStr = "yoda.png";
    
            ImageIcon image = new ImageIcon(imgStr);
    
            JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
    
            frame.getContentPane().add(label1);
    
    
    
            frame.validate();
            frame.setVisible(true);
       }
    
        public static void main(String[] args) {
    
            DemoImage show1 = new DemoImage();
    
            show1.showImage();
    
        }
    }
    Now I just need to stick the FileChooser into the code somewhere, but am not sure where.

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

    Default

    Quote Originally Posted by Unsub View Post
    I wanted the FileChooser to return a string so that I could use it as the URL for the photo.
    Java Code:
    selectedFile.toURI().toURL();
    db

Similar Threads

  1. FileChooser
    By jperson in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 04:43 PM
  2. download javax.swing.filechooser?
    By spalax in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 11:27 AM
  3. Filechooser
    By Joe2003 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 07:51 AM
  4. show only PNG file in FileChooser
    By rabbia in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:04 AM
  5. FileChooser UI
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 11-27-2008, 03:31 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
  •