2012-04-19 207 views
0

我在JFileChooser的JList中添加了一些文件。我添加了一個名爲「CHECK」的新按鈕,單擊它時會告訴JList中是否存在特定文件(已添加的文件中)。如果你們中的任何一位能夠告訴我什麼是正確的做法,這將是非常好的。檢查JList中是否存在文件

感謝您提前。

這是我目前的代碼;

  final JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
    fileChooser.setMultiSelectionEnabled(true); 
    getContentPane().add(fileChooser, "cell 0 0 3 9"); 

    JScrollPane scrollPane = new JScrollPane(); 
    getContentPane().add(scrollPane, "cell 10 1 3 8,grow"); 

    vector = new Vector<File>(); 
    final JList list = new JList(vector); 
    scrollPane.setViewportView(list); 

    JPanel panel = new JPanel(); 
    getContentPane().add(panel, "cell 3 4 7 1,grow"); 

    JButton btnNewButton = new JButton("Add Files"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      for (File file : fileChooser.getSelectedFiles()) { 
         vector.add(file); 
         System.out.println("Added..!!"); 
       } 
       list.updateUI(); 

      } 
    }); 
    panel.add(btnNewButton); 

    JButton btnNewButton_1 = new JButton("Remove Files"); 
    btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if(list.getSelectedIndices().length > 0) { 
        int[] selectedIndices = list.getSelectedIndices(); 
        for (int i = selectedIndices.length-1; i >=0; i--) { 
         vector.removeElementAt(i); 
         System.out.println("Removed..!!"); 
        } 
        } 
        list.updateUI(); 

     } 
     }); 
    panel.add(btnNewButton_1); 

    JButton btnNewButton_2 = new JButton("Check For Files"); 
    btnNewButton_2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String name = ""; 
      if(list.getSelectedIndices().length > 0) { 
         //// to check if a file exists ///// 
      } 
      } 
      }); 

    panel.add(btnNewButton_2); 
+0

'如果(file.isExists())'不工作? – 2012-04-19 11:18:57

+0

但是這只是檢查一個文件是否存在,一般來說..我想知道如果文件存在或不在JList中已經有一些文件ih,因爲我在第一步添加了它們。 – dmurali 2012-04-19 11:20:35

+0

我們很難在沒有顯示代碼的情況下提供幫助。你能發表你迄今爲止所嘗試過的/已有的嗎? – Jim 2012-04-19 11:29:44

回答

1

矢量有一個包含方法,您可以使用:

if(vector.contains(file)){ 
    //Vector has the file 
} 
+0

我認爲這可能只有當文件不包含完整路徑,只有文件名或包含字符串值而不是文件 – mKorbel 2012-04-19 11:41:47

+0

是的。你說得對。我能夠檢查存在..但在那種情況下,我需要將整個路徑指定爲輸入,以及如果我不知道它存在的路徑,該怎麼辦?例如,我指定像; File file = new File(「H:\\ abc.txt」); \t if(vector.contains(file)){---} – dmurali 2012-04-19 11:49:20

1
  1. 請使用集合框架的List爲VAR類型和ArrayList的具體類實例化。從Java 1.2開始,Vector一直是過去的事情。
  2. 迭代選擇的文件在兩端檢查規範的路徑:

final File toCheck = fileToCheckInList.getCanonicalFile(); 
for (File file : fileChooser.getSelectedFiles()) 
    if (file.getCanonicalFile().equals(toCheck)) return true; 
+0

感謝您的建議。但即使在這種情況下,我也要指定路徑,然後才能正確檢查存在。如果我以前不知道路徑(文件的路徑)怎麼辦? – dmurali 2012-04-19 12:02:24

+0

我們現在進入您沒有提供詳細信息的細節。究竟你用什麼字符串搜索,以及列表中的字符串究竟是什麼? (當我說字符串時,我用它鬆散地,我知道你有文件在那裏)。 – 2012-04-19 12:03:50

+0

我在我的JList中有幾個文件,比如zip文件和xml文件。我總共添加了3個文件,2個xml文件是一個十六進制文件,其中我想要搜索一個特定的xml文件是否存在。 – dmurali 2012-04-19 12:09:26