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?
Printable View
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?
Thanks JosAh,
sorry I wasn't completely clear on what I wanted, but your suggestion lead me in the right direction.
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.
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.
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:Now I just need to stick the FileChooser into the code somewhere, but am not sure where.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();
}
}