Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionclass OpenFilePanel extends JPanel { . JButton bouton; . JFileChooser jFileChooser1; . public OpenFilePanel() . { . jFileChooser1 =new JFileChooser(); . bouton=new JButton("ouvrir"); . bouton.addActionListener(new ActionListener() . { . public void actionPerformed(ActionEvent e) { . JDialog JD = new JDialog(); . JD.setModal(true); . JD.setSize(640,350); . JD.setTitle("Choose a file"); . . //recupere le choix de l'utilisateur et attribut une action appropiée . int returnVal = jFileChooser1.showOpenDialog(this); . if(returnVal == JFileChooser.CANCEL_OPTION) { . JD.setVisible(false); . } . if(returnVal == JFileChooser.APPROVE_OPTION) { . //recupere le fichier selectionné String theFile = null; while (theFile == null){ . theFile = jFileChooser1.getSelectedFile().getName(); . } . JD.setVisible(false); . } . } . }); . add(bouton, BorderLayout.CENTER); . } }
class OpenFilePanel extends JPanel { JButton bouton; JFileChooser jFileChooser1; public OpenFilePanel() { jFileChooser1 =new JFileChooser(); bouton=new JButton("ouvrir"); bouton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JDialog JD = new JDialog(); JD.setModal(true); JD.setSize(640,350); JD.setTitle("Choose a file"); //recupere le choix de l'utilisateur et attribut une action appropiée int returnVal = jFileChooser1.showOpenDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION) { JD.setVisible(false); } if(returnVal == JFileChooser.APPROVE_OPTION) { //recupere le fichier selectionné String theFile = null; while (theFile == null){ theFile = jFileChooser1.getSelectedFile().getName(); } JD.setVisible(false); } } }); add(bouton, BorderLayout.CENTER); }
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.beans.*; import java.util.*; import java.io.*; import javax.swing.*; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileView; import java.applet.*; import javax.swing.border.*; public class OpenButton extends Applet { JButton bouton=new JButton("ouvrir");; JFileChooser jFileChooser1; public void init() { bouton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDialog JD = new JDialog(); JD.setModal(true); JD.setSize(640,350); JD.setTitle("Choose a file"); //recupere le choix de l'utilisateur et attribut une action appropiée int returnVal = jFileChooser1.showOpenDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION) { JD.setVisible(false); } if(returnVal == JFileChooser.APPROVE_OPTION) { //recupere le fichier selectionné String theFile = null; while (theFile == null){ theFile = jFileChooser1.getSelectedFile().getName(); } JD.setVisible(false); } } }); } }
public class FileChooserTest { public static void main(String[] args) { ImageViewerFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class ImageFrame extends JFrame { public ImageFrame() { setTitle("FileChooserTest"); setSize(300,400); JButton open=new JButton("ouvrir"); open.addActionListener(new FileOpenListener()); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(open); chooser = new JFileChooser(); } private class FileOpenListener implements ActionListener { public void actionPerformed(ActionEvent evt) { // configure le selecteur de fichier chooser.setCurrentDirectory(new File(".")); // accepte les fichiers .jpg, .jpeg, .gif // ExtensionFileFilter n'est pas defini dans ce code final ExtensionFileFilter filter = new ExtensionFileFilter(); filter.addExtension("jpg"); filter.addExtension("jpeg"); filter.addExtension("gif"); filter.setDescription("Image files"); chooser.setFileFilter(filter); // affiche ma boite de dialog int result =chooser.showOpenDialog(ImageFrame.this); // recupere l'adresse du fichier selectionné if(result == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } } private JLabel label; private JFileChooser chooser; }