Java: introduzione alla classe JFileChooser
La classe javax.swing.JFileChooser consente un semplice ed immediato meccanismo per selezionare file e directory.
Il più semplice modo per istanziare questa classe è usare il costruttore:
JFileChooser(String currentDirectoryPath)
specificando il percorso di una directory, che verrà mostrata quando verrà aperto il file chooser.
Per indicare il tipo di file che deve essere scelto (file, directory o entrambi), utilizziamo il metodo
public void setFileSelectionMode(int mode)
dove mode è un dei seguenti valori:
- JFileChooser.FILES_ONLY
- JFileChooser.DIRECTORIES_ONLY
- JFileChooser.FILES_AND_DIRECTORIES
Se invece vogliamo abilitare/disabilitare la selezione multipla, utilizziamo il metodo:
public void setMultiSelectionEnabled(boolean b)
Infine, per aprire il JFileChooser, utilizziamo uno dei seguenti metodi:
public int showOpenDialog(Component parent) public int showSaveDialog(Component parent)
il primo se vogliamo utilizzare la funzionalità di apertura di file/directory, il secondo se vogliamo salvare un file.
Il valore di ritorno è uno tra questi tre:
- JFileChooser.CANCEL_OPTION
- JFileChooser.APPROVE_OPTION
- JFileChooser.ERROR_OPTION
Il codice che segue mostra come utilizzare i metodi descritti precedentemente:
import javax.swing.JLabel; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.File; public class GuiTest extends JFrame { JTextArea infoText; public GuiTest(String text) { super(text); JButton openButton = new JButton("Open"); JButton openDirButton = new JButton("Open Directory"); JButton saveButton = new JButton("Save"); JButton closeButton = new JButton("Close"); infoText = new JTextArea(); openButton.addActionListener(new OpenActionListener()); openDirButton.addActionListener(new OpenDirActionListener()); saveButton.addActionListener(new SaveActionListener()); closeButton.addActionListener(new CloseActionListener()); JPanel buttonPanel = new JPanel(new GridLayout(1, 4, 5, 5)); buttonPanel.add(openButton); buttonPanel.add(openDirButton); buttonPanel.add(saveButton); buttonPanel.add(closeButton); JPanel panel = new JPanel(new BorderLayout()); panel.add(infoText, BorderLayout.CENTER); panel.add(buttonPanel, BorderLayout.SOUTH); this.setContentPane(panel); this.setSize(600, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class OpenActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { JFileChooser fileChooser = new JFileChooser("~"); fileChooser.setMultiSelectionEnabled(true); int choose = fileChooser.showOpenDialog(GuiTest.this); if(choose == JFileChooser.APPROVE_OPTION) { File[] selection = fileChooser.getSelectedFiles(); for(File file : selection) { infoText.setText(infoText.getText() + "Selected file " + file.getName() + "\n"); } } } } public class OpenDirActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { JFileChooser fileChooser = new JFileChooser("~"); fileChooser.setMultiSelectionEnabled(false); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int choose = fileChooser.showOpenDialog(GuiTest.this); if(choose == JFileChooser.APPROVE_OPTION) { File directory = fileChooser.getSelectedFile(); infoText.setText(infoText.getText() + "Selected directory: " + directory.getName() + "\n"); } } } public class SaveActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { JFileChooser fileChooser = new JFileChooser("~"); int choose = fileChooser.showSaveDialog(GuiTest.this); if(choose == JFileChooser.APPROVE_OPTION) { File directory = fileChooser.getSelectedFile(); infoText.setText(infoText.getText() + "Saved " + directory.getName() + "\n"); } } } public class CloseActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { dispose(); } } public static void main(String[] args) { new GuiTest("File Chooser").setVisible(true); } }
Il risultato è questo (clicca per ingrandire):