Advertisement
nguyenvanquan7826

get path file

Aug 21st, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. import javax.annotation.processing.FilerException;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16.  
  17. public class DemoFile extends JFrame implements ActionListener {
  18.     String path = getClass().getProtectionDomain().getCodeSource()
  19.             .getLocation().getPath();
  20.  
  21.     // when use jar file
  22.     String absolutePathJarFile = path.substring(0, path.lastIndexOf("/"))
  23.             + "/duoi hinh bat chu";
  24.  
  25.     // when run in project
  26.     String absolutePath = "duoi hinh bat chu";
  27.  
  28.     private File file = new File(absolutePathJarFile);
  29.     private JLabel label, dapan;
  30.     private JButton btNext, btPrev, btDapAn;
  31.     private ArrayList<String> list = new ArrayList<String>();
  32.     private ArrayList<String> listName = new ArrayList<String>();
  33.  
  34.     private int count = -1;
  35.  
  36.     public DemoFile() {
  37.         btNext = createButton("Next");
  38.         btPrev = createButton("Prev");
  39.         btDapAn = createButton("Result");
  40.         dapan = new JLabel("Dap An");
  41.         dapan.setFont(new Font("arial", Font.BOLD, 30));
  42.  
  43.         label = new JLabel("");
  44.  
  45.         JPanel panel = new JPanel(new FlowLayout());
  46.         JPanel panel1 = new JPanel(new FlowLayout());
  47.         panel1.add(label);
  48.  
  49.         panel.add(btPrev);
  50.         panel.add(btNext);
  51.         panel.add(btDapAn);
  52.  
  53.         this.setLayout(new BorderLayout());
  54.         this.add(dapan, BorderLayout.NORTH);
  55.         this.add(panel1, BorderLayout.CENTER);
  56.         this.add(panel, BorderLayout.SOUTH);
  57.         this.setSize(600, 500);
  58.         this.setVisible(true);
  59.     }
  60.  
  61.     private JButton createButton(String s) {
  62.         JButton bt = new JButton(s);
  63.         bt.addActionListener(this);
  64.         return bt;
  65.     }
  66.  
  67.     private ImageIcon createIcon(String name) {
  68.         String pathImage = file.getAbsolutePath() + File.separator + name;
  69.         System.out.println(pathImage);
  70.         return new ImageIcon(file.getAbsolutePath() + File.separator + name);
  71.     }
  72.  
  73.     public void listDirectory(File file) throws FilerException {
  74.         if (!file.exists()) { // kiểm tra xem đường dẫn có tồn tại hay không
  75.             System.out.print("Duong dan khong ton tai");
  76.             return;
  77.         }
  78.  
  79.         if (file.isDirectory()) {
  80.             String[] listDirectoryChild = file.list();
  81.             for (int i = 0; i < listDirectoryChild.length; i++) {
  82.                 listDirectory(new File(file.getPath(), listDirectoryChild[i]));
  83.                 listName.add(listDirectoryChild[i]);
  84.             }
  85.             return;
  86.         }
  87.         if (file.isFile()) {
  88.             try {
  89.                 list.add(file.getCanonicalPath());
  90.             } catch (IOException e) {
  91.                 e.printStackTrace();
  92.             }
  93.             return;
  94.         }
  95.  
  96.         return;
  97.     }
  98.  
  99.     public static void main(String args[]) {
  100.  
  101.         DemoFile demoFile = new DemoFile();
  102.         System.out.println(demoFile.file.getAbsolutePath());
  103.         try {
  104.             demoFile.listDirectory(demoFile.file);
  105.         } catch (FilerException e) {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109.  
  110.     @Override
  111.     public void actionPerformed(ActionEvent e) {
  112.  
  113.         if (e.getSource() == btNext) {
  114.             if (++count < list.size()) {
  115.                 label.setIcon(createIcon(listName.get(count)));
  116.                 dapan.setText("");
  117.             }
  118.         }
  119.  
  120.         if (e.getSource() == btPrev) {
  121.             if (count > 0) {
  122.                 label.setIcon(createIcon(listName.get(--count)));
  123.                 dapan.setText("");
  124.             }
  125.         }
  126.  
  127.         if (e.getSource() == btDapAn) {
  128.             String s = listName.get(count).substring(0,
  129.                     listName.get(count).indexOf("."));
  130.             dapan.setText(s);
  131.  
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement