Advertisement
Guest User

Untitled

a guest
May 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package gui;
  2.  
  3. import javax.swing.JFileChooser;
  4. import javax.swing.JFrame;
  5. import javax.swing.JButton;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. public class Form extends JFrame{
  11.     private static final long serialVersionUID = -3526248974305872670L;
  12.    
  13.     private Listener l = new Listener();
  14.     private JButton toButton;
  15.    
  16.     public Form(){
  17.        
  18.         toButton = new JButton("...");
  19.         toButton.setBounds(395, 86, 24, 20);
  20.         toButton.addActionListener(l); // c'est ici que le listener est declarer pour le buton "toButton"
  21.         add(toButton);
  22.        
  23.         setVisible(true);
  24.     }
  25.  
  26.     public class Listener implements ActionListener{
  27.        
  28.         final JFileChooser fc = new JFileChooser();
  29.  
  30.         @Override
  31.         public void actionPerformed(ActionEvent e) {
  32.             if(e.getSource() == toButton){
  33.                 fc.setMultiSelectionEnabled(false);
  34.                 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  35.                 fc.setCurrentDirectory(new File(System.getProperty("user.home"))); // met ce que tu veux ici
  36.                
  37.                 int returnVal = fc.showSaveDialog(Form.getInstance().getFocusOwner());
  38.                
  39.                 if(returnVal == JFileChooser.APPROVE_OPTION) {
  40.                     if(fc.getSelectedFile().isFile()){
  41.                         //si fichier
  42.                     } else {
  43.                         //si dossier
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement