JTree et affichage du contenu.
Bonjour à tous,
Je travaille actuellement sur une petite appli java dont le but est d'afficher des photos à partir d'un arbre.
Voici mon code :
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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
public class JTreePhotos extends JSplitPane {
JTree arbre;
JPanel coteimage = new JPanel();
public JTreePhotos(File rep){
JPanel gauche = new JPanel(new BorderLayout());
coteimage.setLayout(new GridLayout(0,5));
ListerRepertoire liste = new ListerRepertoire();
Noeud racine = liste.arbreRep(rep);
JTree arbre = new JTree(racine);
gauche.add(arbre);
EcouteurArbre tsl = new EcouteurArbre();
arbre.addTreeSelectionListener(tsl);
this.setLeftComponent(new JScrollPane(gauche));
this.setRightComponent(coteimage);
}
class EcouteurArbre extends ListerRepertoire implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent evt){
coteimage.removeAll();
coteimage.repaint();
File fic= new File(arbre.getSelectionPath().getLastPathComponent().toString());
Vector v = listeFiles(fic);
int max = v.size();
for (int i=0; i<max;i++){
Photo p = new Photo((File)v.elementAt(i));
JPanel photocourante = new JPanel();
photocourante.add(p);
coteimage.add(photocourante);
}
coteimage.validate();
}
} |
Une photo se crée à partir du chemin d'un fichier. (public Photo (File fichier))
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
| public ListerRepertoire(){
vi = new Vector() ;
}
public Vector listeFiles(File rep){
if(rep.isDirectory()){ listeFileR(rep);
}
return vi;
}
private void listeFileR(File rep){
File[] files = rep.listFiles();
int max = files.length;
for(int i=0; i < max; i++){
File fichier = files[i];
if(fichier.isFile() && (fichier.getName().toUpperCase().endsWith(".GIF")) || (fichier.getName().toUpperCase().endsWith(".JPG")) ) {
vi.add(fichier);
System.out.println(fichier);
}else if (fichier.isDirectory()){
listeFiles(fichier);
}
}
} |
Mon problème est le suivant :
Une fois intégré dans une Frame, mon IDE rame pas mal et me retourne cette erreur :
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
| Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Source_tpnote.JTreePhotos$EcouteurArbre.valueChanged(JTreePhotos.java:40)
at javax.swing.JTree.fireValueChanged(Unknown Source)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(Unknown Source)
at javax.swing.JTree.setSelectionPath(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) |
Après moults essais infructueux, je viens à vous pour tenter de comprendre mon erreur...
Merci d'avance!