import java.awt.BorderLayout;Output:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JFileChooserTest {
public static void main(String[] args) {
final JFrame window = new JFrame();
window.setSize(200,200);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("Ficher");
JMenuItem Open = new JMenuItem("Open");
jm.add(Open);
jmb.add(jm);
window.add(jmb,BorderLayout.NORTH);
JFileChooser fc = new JFileChooser();
Open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int val_retour = fc.showOpenDialog(window);
if (val_retour == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//display file name
System.out.println("File name: "+file.getName()+"\n");
//display the absolute path of the file
System.out.println("Absolute path: "+file.getAbsolutePath()+"\n");
} else {
System.out.println("Opening is canceled\n");
}
}
});
}
}
File name: decTo Binaire.cppTo retrieve the chosen file, use the method getSelectedFile(). If the return value is JFileChooser.APPROVE_OPTION, the returned value is an instance of the class File.
Filename: C:\Users\VAIO\Documents\CC++\decTo Binaire.cpp
final JFileChooser fc = new JFileChooser();Runtime:
fc.setMultiSelectionEnabled(true)
Open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int val_retour = fc.showOpenDialog(window);
if (val_retour == JFileChooser.APPROVE_OPTION) {
File[] file = fc.getSelectedFiles();
//Loop that runs through the set of files
for(File f:file){
System.out.println("File name: "+f.getName()+"\n");
System.out.println("Absolute path: "+f.getAbsolutePath()+"\n");
}
} else {
System.out.println("Opening is canceled\n");
}
}
});
Filename: decTo Binaire.cppReferences:
Absolute path: C:\Users\VAIO\Documents\CC++\decTo Binaire.cpp
Filename: decalage_tableau.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\decalage_tableau.cpp
File name: determinant_matrice.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\determinant_matrice.cpp
Filename: equation-1-degre.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\equation-1-degre.cpp
Please disable your ad blocker and refresh the window to use this website.