Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1986 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  JFileChooser & Ouvrir un ficher avec son extension

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

JFileChooser & Ouvrir un ficher avec son extension

n°1821050
Xavlord
Posté le 03-12-2008 à 09:10:50  profilanswer
 


Bonjour,
 
Ça fait un bon moment que je cherche sur Google ou autre mais j'ai rien trouvé de très concluant.
Alors voilà, j'ai fait 2 fenêtres parcourir avec 2 filtres, .doc et .xls.
Maintenant il me reste plus qu'a trouver comment ouvrir le fichier grâce au logiciel correspondant à son extension.
Pourriez vous m'aider s'il vous plait ??
 
Merci !
 
MyFileSystemView.java

Code :
  1. import java.io.*;
  2. import javax.swing.*;
  3. import javax.swing.filechooser.*;
  4. public class MyFileSystemView extends FileSystemView {
  5. private final FileSystemView system = FileSystemView.getFileSystemView();
  6. private final File root;
  7. private MyFileSystemView(File root) {
  8.  // On récupère le chemin canonique du répertoire
  9.  this.root = root;
  10. }
  11. public static JFileChooser createFileChooser(File root) throws IOException {
  12.  root = root.getCanonicalFile(); // On récupère le chemin canonique
  13.  if (!root.isDirectory()) {
  14.   throw new IllegalArgumentException("root must be a directory" );
  15.  }
  16.  return new JFileChooser(root, new MyFileSystemView(root));
  17. }
  18. public boolean isRoot(File f) {
  19.  return this.root.equals(f.getAbsoluteFile());
  20. }
  21. public boolean isFileSystemRoot(File dir) {
  22.  return this.root.equals(dir.getAbsoluteFile());
  23. }
  24. public File[] getRoots() {
  25.  return new File[] { this.root.getAbsoluteFile() };
  26. }
  27. public File getHomeDirectory() {
  28.  return this.root.getAbsoluteFile();
  29. }
  30. public File getDefaultDirectory() {
  31.  return this.root.getAbsoluteFile();
  32. }
  33. public File getParentDirectory(File dir) {
  34.  if(isRoot(dir)) {
  35.   // Pas de parent director pour le répertoire root :
  36.   return this.root;
  37.  }
  38.  return system.getParentDirectory(dir);
  39. }
  40. public File createFileObject(String path) {
  41.  // Fichier crée lorsqu'on tape le nom directement :
  42.  // On crée le fichier selon le répertoire root :
  43.  File file = new File(this.root, path);
  44.  if (file.exists()) {
  45.   return file;
  46.  }
  47.  return this.root;
  48. }
  49. protected File createFileSystemRoot(File f) {
  50.  return null;
  51. }
  52. public File createFileObject(File dir, String filename) {
  53.  return system.createFileObject(dir, filename);
  54. }
  55. public File createNewFolder(File containingDir) throws IOException {
  56.  return system.createNewFolder(containingDir);
  57. }
  58. public File getChild(File parent, String fileName) {
  59.  return system.getChild(parent, fileName);
  60. }
  61. public File[] getFiles(File dir, boolean useFileHiding) {
  62.  return system.getFiles(dir, useFileHiding);
  63. }
  64. public String getSystemDisplayName(File f) {
  65.  return system.getSystemDisplayName(f);
  66. }
  67. public Icon getSystemIcon(File f) {
  68.  return system.getSystemIcon(f);
  69. }
  70. public String getSystemTypeDescription(File f) {
  71.  return system.getSystemTypeDescription(f);
  72. }
  73. public boolean isComputerNode(File dir) {
  74.  return system.isComputerNode(dir);
  75. }
  76. public boolean isDrive(File dir) {
  77.  return system.isDrive(dir);
  78. }
  79. public boolean isFileSystem(File f) {
  80.  return system.isFileSystem(f);
  81. }
  82. public boolean isFloppyDrive(File dir) {
  83.  return system.isFloppyDrive(dir);
  84. }
  85. public boolean isHiddenFile(File f) {
  86.  return system.isHiddenFile(f);
  87. }
  88. public boolean isParent(File folder, File file) {
  89.  return system.isParent(folder, file);
  90. }
  91. public Boolean isTraversable(File f) {
  92.  return system.isTraversable(f);
  93. }
  94. public static void main(String[] args) throws IOException {
  95. JFileChooser chooser = MyFileSystemView.createFileChooser(new File("C:\\Documents and Settings\\Xavier\\Mes documents\\Documents" ));
  96. chooser.showOpenDialog(null);
  97. }
  98. }


 
 
MonFiltre :

Code :
  1. import java.io.*;
  2. import java.io.File;
  3. import javax.swing.filechooser.FileFilter;
  4. public class MonFiltre extends javax.swing.filechooser.FileFilter {
  5.    String [] lesSuffixes;
  6.    String  laDescription;
  7.    public MonFiltre(String []lesSuffixes, String laDescription){
  8.       this.lesSuffixes = lesSuffixes;
  9.       this.laDescription = laDescription;
  10.    }
  11.    boolean appartient( String suffixe ){
  12.       for( int i = 0; i<lesSuffixes.length; ++i)
  13.          if(suffixe.equals(lesSuffixes[i]))
  14.             return true;
  15.          return false;
  16.    }
  17.    public boolean accept(File f) {
  18.       if (f.isDirectory())  return true;
  19.       String suffixe = null;
  20.       String s = f.getName();
  21.       int i = s.lastIndexOf('.');
  22.       if (i > 0 &&  i < s.length() - 1)
  23.          suffixe = s.substring(i+1).toLowerCase();
  24.       return suffixe != null && appartient(suffixe);
  25.    }
  26.    // la description du filtre
  27.    public String getDescription() {
  28.       return laDescription;
  29.    }
  30. }


 
Mon appel :

Code :
  1. if (e.getSource() == menuItemOuvrirModifQuestionnaire){
  2.   MonFiltre filtreDoc = new MonFiltre(
  3.             new String[]{"doc"},"Fichiers Word (*.doc)" );
  4.   JFileChooser selecteur = new JFileChooser();
  5.   selecteur.addChoosableFileFilter(filtreDoc);
  6.   int result = selecteur.showOpenDialog(this);
  7.   if (result == JFileChooser.APPROVE_OPTION)
  8.    nomFichier = selecteur.getSelectedFile() + "";
  9.    cheminFichier = selecteur.getSelectedFile().getAbsolutePath();
  10.  }

mood
Publicité
Posté le 03-12-2008 à 09:10:50  profilanswer
 

n°1823936
Xavlord
Posté le 09-12-2008 à 08:28:17  profilanswer
 

Il y à personne personne ??? svp

n°1824032
sielfried
Posté le 09-12-2008 à 11:10:52  profilanswer
 
n°1824422
Xavlord
Posté le 09-12-2008 à 18:28:32  profilanswer
 

Hey merci ! j'avais pas trouvé ça !

 
Code :
  1. if (e.getSource() == menuItemOuvrirModifQuestionnaire){
  2.   MonFiltre filtreDoc = new MonFiltre(
  3.             new String[]{"doc"},"Fichiers Word (*.doc)" );
  4.   JFileChooser selecteur = new JFileChooser();
  5.   selecteur.addChoosableFileFilter(filtreDoc);
  6.   int result = selecteur.showOpenDialog(this);
  7.   if (result == JFileChooser.APPROVE_OPTION)
  8.   {
  9.    nomFichier = selecteur.getSelectedFile() + "";
  10.    cheminFichier = selecteur.getSelectedFile().getAbsolutePath();
  11.    Desktop.Action action;
  12.           action = Desktop.Action.OPEN;
  13.      
  14.       //JOptionPane.showMessageDialog(this,cheminFichier);
  15.       private void onLaunchDefaultApplication(java.awt.event.ActionEvent evt){
  16.        try {
  17.          File fichier = new File(cheminFichier);
  18.          desktop.open(fichier);
  19.         }
  20.        catch (IOException ioe)
  21.         {
  22.                ioe.printStackTrace();
  23.         }
  24.       }
 

J'ai fait ça...mais ça marche pas encore bien bien bien !!
Ce qui est en rouge est souligné dans Eclipse.

 

1e erreur : "void is an invalid type for the variable onLaunchDefaultApplication"
2e erreur : "desktop cannot be resolved"


Message édité par Xavlord le 09-12-2008 à 19:08:07
n°1824501
sielfried
Posté le 09-12-2008 à 20:28:07  profilanswer
 

Oulà... t'essaies de déclarer une fonction à l'intérieur d'un if là... Pas possible en Java.  [:petrus75]  
 
Sinon, pour chopper le Desktop c'est Desktop.getDesktop(), suffit de lire la doc...


---------------
StarCraft Professional Gaming Database | [Ze Topic] Starcraft/BroodWar

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  JFileChooser & Ouvrir un ficher avec son extension

 

Sujets relatifs
Ouvrir lightbox au chargement d'une pageEst-il possible d'écrire dans un fichier xls sans l'ouvrir ?
popup pour ouvrir des photosOuvrir un userform d'un autre fichier Excel
Ouvrir 1 page dans 1 cadre avec bouton FlashOuvrir une nouvelle page de facon aléatoire
Prob d'extensionrestriction et extension
JFileChooser [Résolu]renvoyer le resultat d un .bat dans fichier txt puis l ouvrir
Plus de sujets relatifs à : JFileChooser & Ouvrir un ficher avec son extension


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR