charlonien | Bonjour à tous !
J'avais déjà fais appel a vous par le passé et j'en était satisfait .
J'ai donc cette fois-ci un problème d'un autre genre.
J'ai un projet en Java qui consiste a détecter les Pdf a partir d'un chemin donné.
Mon code se compose en plusieurs classe:
la première contient le main c'est elle qui va créer une fenêtre en faisant appel à la classe que j'ai créée
voici donc le main
Code :
- package pdfFinder;
- public class MainPdfFinder {
- public static void main(String[] args) {
- new PdfViewer();
- }
- }
|
et maintenant voici ma fenêtre attention c'est gros !
Code :
- package pdfFinder;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- public class PdfViewer extends JFrame {
- private PdfFinder find = new PdfFinder();
- private boolean chooseSearch = false;
- private boolean chooseViewer = false;
- private String searchPath;
- private String viewerPath;
- private ImageIcon viewerOk = new ImageIcon("ok.png" );
- private ImageIcon viewerWrong = new ImageIcon("wrong.png" );
- private ImageIcon searchOk = new ImageIcon("ok.png" );
- private ImageIcon searchrWrong = new ImageIcon("wrong.png" );
- private JPanel north = new JPanel(new GridLayout(3, 1));
- private JPanel viewerPan = new JPanel();
- private JPanel searchPan = new JPanel();
- private JPanel startPan = new JPanel();
- private JPanel center = new JPanel();
- private JPanel east = new JPanel(new GridLayout(2,1));
- private JPanel foundPan = new JPanel(new GridLayout(2, 1));
- private JPanel numberPan = new JPanel();
- private JPanel numberPdfPan = new JPanel();
- private JPanel openPan = new JPanel();
- private JPanel south = new JPanel();
- private JButton viewerBut = new JButton("Choose" );
- private JButton searchBut = new JButton("Choose" );
- private JButton startBut = new JButton("Search PDF" );
- private JButton openBut = new JButton("Open PDF" );
- private JLabel viewerLab = new JLabel("Viewer Path : " );
- private JLabel viewerIcon = new JLabel(viewerWrong);
- private JLabel searchLab = new JLabel("Search Path : " );
- private JLabel searchIcon = new JLabel(searchrWrong);
- private JLabel numberLab = new JLabel("Files Found : " );
- private JLabel numberPdfLab = new JLabel();
- private JLabel southLab = new JLabel(" Create by CHAUMIENNE Charles " );
- private JTextField viewerText = new JTextField(20);
- private JTextField searchText = new JTextField(20);
- private String title[] = {"name", "number of pages", "size", "last modification", "path", "version", "encrypted"};
- private JTable tab = new JTable();
- public PdfViewer() {
- setTitle("PDF Finder" );
- setSize(570, 585);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setLayout(new BorderLayout());
- centerPanel();
- southPanel();
- northPanel();
- eastPanel();
- setVisible(true);
- }
- class viewerListener implements ActionListener{
- public void actionPerformed(ActionEvent a){
- viewerPath = viewerText.getText();
- if(find.verifPath(viewerPath)){
- chooseViewer = true;
- viewerPan.remove(viewerIcon);
- viewerIcon.updateUI();
- viewerIcon = new JLabel(viewerOk);
- viewerPan.add(viewerIcon);
- viewerIcon.updateUI();
- openBut.setEnabled(true);
- }
- else{
- chooseViewer = false;
- viewerPan.remove(viewerIcon);
- viewerIcon.updateUI();
- viewerIcon = new JLabel(viewerWrong);
- viewerPan.add(viewerIcon);
- viewerIcon.updateUI();
- openBut.setEnabled(false);
- }
- }
- }
- class searchListener implements ActionListener{
- public void actionPerformed(ActionEvent a){
- searchPath = searchText.getText();
- if(find.verifPath(searchPath)){
- chooseSearch = true;
- searchPan.remove(searchIcon);
- searchIcon.updateUI();
- searchIcon = new JLabel(searchOk);
- searchPan.add(searchIcon);
- searchIcon.updateUI();
- startBut.setEnabled(true);
- }
- else{
- chooseSearch = false;
- searchPan.remove(searchIcon);
- searchIcon.updateUI();
- searchIcon = new JLabel(searchrWrong);
- searchPan.add(searchIcon);
- searchIcon.updateUI();
- startBut.setEnabled(false);
- }
- }
- }
- class StartListener implements ActionListener{
- public void actionPerformed(ActionEvent a){
- try {
- if(chooseSearch == true){
- find.searchAlgo(new File(searchPath));
- numberPdfLab.setText(""+find.getNbPdf());
- find.setNbPdf(0);
- find.afficher();
- center.updateUI();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class OpenListener implements ActionListener{
- public void actionPerformed(ActionEvent a){
- if( chooseViewer == true){
- }
- }
- }
- public void northPanel(){
- viewerPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- viewerPan.add(viewerLab);
- viewerPan.add(viewerText);
- viewerBut.addActionListener(new viewerListener());
- viewerPan.add(viewerBut);
- viewerPan.add(viewerIcon);
- north.add(viewerPan);
- searchPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- searchPan.add(searchLab);
- searchPan.add(searchText);
- searchBut.addActionListener(new searchListener());
- startBut.setEnabled(false);
- searchPan.add(searchBut);
- searchPan.add(searchIcon);
- north.add(searchPan);
- startPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- startBut.addActionListener(new StartListener());
- startPan.add(startBut);
- north.add(startPan);
- getContentPane().add(north, BorderLayout.NORTH);
- }
- public void southPanel(){
- south.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- south.add(southLab);
- getContentPane().add(south, BorderLayout.SOUTH);
- }
- public void eastPanel(){
- numberPan.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
- numberLab.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
- numberPan.add(numberLab);
- foundPan.add(numberPan);
- numberPdfPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- numberPdfLab.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
- numberPdfPan.add(numberPdfLab);
- foundPan.add(numberPdfPan);
- openPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- openBut.setEnabled(false);
- openBut.addActionListener(new OpenListener());
- openPan.add(openBut);
- east.add(foundPan);
- east.add(openPan);
- getContentPane().add(east, BorderLayout.EAST);
- }
- public void centerPanel(){
- center.add(new JScrollPane(tab));
- getContentPane().add(center, BorderLayout.CENTER);
- }
- }
|
Lorsque le StartListener est appelé il fait appel à une methode SearchAlgo qui est l'algorithme de recherche qui se trouve dans une classe que j'ai nommé PdfFinder et qui contient une aray liste d'une autre classe de ma création qui se nomme Pdf voici donc PdfFinder
Code :
- package pdfFinder;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import com.itextpdf.text.pdf.PdfReader;
- public class PdfFinder {
- private ArrayList<Pdf> lsPdf = new ArrayList<Pdf>();
- private String pdfViewerPath;
- private int nbPdf =0 ;
- public PdfFinder() {
- }
- public static boolean isPdf(String path) {
- try {
- PdfReader pdfR = new PdfReader(path);
- return true;
- }
- catch (IOException e) {
- return false;
- }
- }
- public void searchAlgo(File dir) throws IOException {
- File[] file;
- file = dir.listFiles();
- for (int i = 0; i < file.length; i++) {
- if (isPdf(dir.getAbsolutePath())) {
- Pdf pdf = new Pdf(file[i].getAbsolutePath());
- lsPdf.add(pdf);
- afficher(pdf);
- setNbPdf (getNbPdf() + 1 );
- }
- else if (file[i].isDirectory()) {
- searchAlgo(file[i]);
- }
- }
- }
- public boolean verifPath(String path){
- boolean exist ;
- File file = new File(path);
- exist = file.exists();
- return exist;
- }
- public Object[][] setData(){
- Object [][] data = new Object[lsPdf.size()][7];
- for(int i=0 ; i < lsPdf.size() ; i++){
- data[i][0] = (lsPdf.get(i).getNameFile());
- data[i][1] = (lsPdf.get(i).getNbPages());
- data[i][2] = (lsPdf.get(i).getSize());
- data[i][3] = (lsPdf.get(i).getLastModif());
- data[i][4] = (lsPdf.get(i).getPath());
- data[i][5] = (lsPdf.get(i).getVersion());
- data[i][6] = (lsPdf.get(i).getIsEncrypted());
- }
- return data;
- }
- public void afficher(Pdf pdf){
- System.out.println(pdf.toString());
- }
- public void setNbPdf(int nbPdf){
- this.nbPdf = nbPdf;
- }
- public int getNbPdf(){
- return nbPdf;
- }
- }
|
et maintenant la classe Pdf qui elle contient toutes les caractéristiques d'un Pdf
Le problème est qu'il m'indique une aray out of bouds exeception a la ligne 66 dans le setData de la classe PdfFinder que je n'ai invoquée nul part et ceci se passe lorsque je clique sur le bouton start de ma fenêtre Je vous remercie d'avance pour votre aide |