-
JtextField
hi
i have a problem while dealing with JtextFields.
actually i have created a browse button by using the JFileChooser.
now when the user selects the file, and clicks open button in browse window,
the text in the filed is supposed to get updated to the filename of selected file.
But i cannot figure out how to do this.
ActionListener aListener;
aListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(browse);
File selectedFile = fc.getSelectedFile();
filename = fc.getName(selectedFile);
filepath.setText(filename);
// filepath is the JTextField which shud get updated. and this does not do the trick
}
};
thanks
-
If the user cancels or closes the dialog without selecting a file you may not be interested in the value returned by getSelectedFile.
Code:
int returnVal = fc.showOpenDialog(browse);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
...
-
What do you see as the value of filename if you put a System.out.println("fn=" + filename);
after the getName() ?