JFileChooser.CANCEL_OPTION : JFileChooser « javax.swing « Java by API






JFileChooser.CANCEL_OPTION

  

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JLabel;

public class MainClass {

  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setAccessory(new LabelAccessory(fileChooser));
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
      File selectedFile = fileChooser.getSelectedFile();
      System.out.println(selectedFile.getParent());
      System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
      System.out.println("JFileChooser.CANCEL_OPTION");
    }
  }
}

class LabelAccessory extends JLabel implements PropertyChangeListener {

  public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
  }

  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File) changeEvent.getNewValue();
      if (file != null) {
        setText(file.getName());
      }
    }
  }
}
           
         
    
  








Related examples in the same category

1.JFileChooser.APPROVE_OPTION
2.JFileChooser.DIRECTORIES_ONLY
3.JFileChooser.DIRECTORY_CHANGED_PROPERTY
4.JFileChooser.ERROR_OPTION
5.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
6.JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
7.JFileChooser: addActionListener(ActionListener l)
8.JFileChooser: addChoosableFileFilter(FileFilter filter)
9.JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)
10.JFileChooser: createDialog(Component parent)
11.JFileChooser: getCurrentDirectory()
12.JFileChooser: getFileSystemView()
13.JFileChooser: getSelectedFile()
14.JFileChooser: getTypeDescription(File f)
15.JFileChooser: isFileHidingEnabled()
16.JFileChooser: setAccessory(JComponent newAccessory)
17.JFileChooser: setApproveButtonMnemonic(char mnemonic)
18.JFileChooser: setApproveButtonText(String approveButtonText)
19.JFileChooser: setApproveButtonToolTipText(String toolTipText)
20.JFileChooser: setControlButtonsAreShown(boolean b)
21.JFileChooser: setCurrentDirectory(File dir)
22.JFileChooser: setFileFilter(FileFilter filter)
23.JFileChooser: setFileHidingEnabled(boolean b)
24.JFileChooser: setFileSelectionMode(int mode)
25.JFileChooser: setFileView(FileView fileView)
26.JFileChooser: setMultiSelectionEnabled(boolean b)
27.JFileChooser: setSelectedFile(File file)
28.JFileChooser: showOpenDialog(Component parent)