简体   繁体   English

检查JList中是否存在文件

[英]Check if file exists in the JList

I have added a few files in my JList from the JFileChooser. 我在JFileChooser的JList中添加了一些文件。 I am adding a new button named "CHECK" which when clicked, tells if a particular file exists in the JList(among the files already added). 我正在添加一个名为“CHECK”的新按钮,当单击该按钮时,它会告诉JList中是否存在特定文件(已添加的文件中)。 It would be really great if any of you could tell me what is the correct procedure to do this step. 如果你们中的任何一个人能告诉我这一步的正确程序是什么,那真的很棒。

Thanking You in Advance.. 提前感谢你..

This is my code currently; 这是我目前的代码;

            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);

Vector has a contains method which you can use: Vector有一个contains方法,你可以使用:

if(vector.contains(file)){
   //Vector has the file
}
  1. Kindly use the Collections Framework's List for the var types and ArrayList for the concrete class to instantiate. 请使用Collections Framework的List for var types和ArrayList来实例化具体类。 Vector has been a thing of the past since Java 1.2. 自Java 1.2以来, Vector已经成为过去。
  2. Iterate over the selected files checking canonical paths at both ends: 迭代所选文件,检查两端的规范路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM