Problèmes avec mon JFileChooser
Bonjour,
Tout d'abord, j'espère poster au bon endroit... Je suis plutôt novice en Java...
J'ai parcouru les forums, la FAQ et les tutoriels sans trouver de réponse à mes interrogations, bien que ça me semble assez basique (je pense).
J'ai une fenêtre sur laquelle j'ai un JPanel et un bouton "Parcourir".
En cliquant sur ce bouton, je souhaite ouvrir mon JFileChooser (jusque là j'y arrive).
Mais... je souhaite mettre un filtre pour ne pouvoir sélectionner que les fichiers textes (d'extension ".txt").
1er problème : Où dois-je faire figurer la ligne de code
Code:
jFileChooser1.addChoosableFileFilter(new FiltreTexte());
car là à chaque fois que je clique sur le bouton "Parcourir" il me rajoute autant de filtres identiques.
2ème problème : je souhaite enlever "tous les fichiers" de la combobox du JFileChooser... Et là aucune idée de comment m'y prendre
Je vous mets mon code, généré sous netbeans ci-dessous, en attendant vos conseils, bonne journée !
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
package appli;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class FenetreAccueil extends javax.swing.JFrame {
/** Creates new form FenetreAccueil */
public FenetreAccueil() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Fichier Entrée"));
jButton1.setText("Parcourir");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jButton1)
.addContainerGap(267, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jFileChooser1.addChoosableFileFilter(new FiltreTexte());
jFileChooser1.showOpenDialog(null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FenetreAccueil().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
class FiltreTexte extends FileFilter {
public boolean accept(File file){
if(file.isDirectory()) {
return true;
}
if(file.getName().toLowerCase().endsWith(".txt"))
{
return true;
}
else
{
return false;
}
}
public String getDescription(){
return "Fichiers txt";
}
}
} |