Thursday, December 4, 2014

Exemple jfilechooser

Dans ce tutorial nous allons montrer un exemple qui montre comment choisir in fichier sur le disque de l'utilisateur en utlisant le composant JFileChooser.


/**
 *
 * @author Ram
 *
 */

public class DemoJFileChooser {
JFrame f = new JFrame("JFileChooser Demo");
JButton btn = new JButton("Parcourir...");
JTextField txtpath = new JTextField(20);
JFileChooser chooser = new JFileChooser();

public void graphique() {

JPanel panel = new JPanel();

panel.add(btn);
panel.add(txtpath);
f.add(panel);
f.setBounds(300, 300, 500, 150);
f.setVisible(true);



btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int returnVal = chooser.showOpenDialog(f);

if (returnVal == JFileChooser.APPROVE_OPTION) {

String path = chooser.getSelectedFile().getPath();
txtpath.setText(path);

} else {

JOptionPane.showMessageDialog(null,
"Vous n'avez rien sélectionné.","Attention",JOptionPane.ERROR_MESSAGE);

}

}

});

}


et voici la methode main pour l'execution de cet exemple:

public static void main(String[] args) {

DemoJFileChooser demo=new DemoJFileChooser();
demo.graphique();

}

}

No comments:

Post a Comment