简体   繁体   中英

How to store and display the for loop contents in a JTextField?

I want to display the missing file names in a text field.

How to store and display the for loop contents in a JTextField ?

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class CheckFiles extends JPanel {

    public String FILENAME = "f:\\branch_report.txt";
    protected ArrayList listFromFile;
    protected ArrayList listFromDir = new ArrayList();

    protected void getListFromFile() {
        listFromFile = new ArrayList();
        BufferedReader is;
        try {
            is = new BufferedReader(new FileReader(FILENAME));
            String line;
            while ((line = is.readLine()) != null) {
                listFromFile.add(line);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Can't open file list file.");
            return;
        } catch (IOException e) {
            System.err.println("Error reading file list");
            return;
        }
    }

    public void getListFromDirectory() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            listFromDir = new ArrayList();
            String[] l = new java.io.File(selectedFile.getAbsolutePath()).list();
            for (int i = 0; i < l.length; i++) {
                listFromDir.add(l[i]);
            }
        } else {
            JOptionPane.showMessageDialog(null, "No files selected");
        }
    }

    public void reportMissingFiles() {
        for (int i = 0; i < listFromFile.size(); i++) {
            if (!listFromDir.contains(listFromFile.get(i))) {
                JOptionPane.showMessageDialog(null, listFromFile.get(i), "File not found", JOptionPane.ERROR_MESSAGE);  // want to display the missing files in text field
            }
        }
        JFrame frame = new JFrame("Files Not Found");
//frame.getContentPane().add(frame,"Center");
        frame.setSize(800, 600);
        frame.setVisible(true);
        JTextField text = new JTextField();
        text.getSelectedFile().getName();
        frame.add(text);
        text.setVisible(true);
//final JLabel label = new JLabel();
//frame.add(label);
//label.setVisible(true);
    }

    public static void main(String[] argv) {
        CheckFiles cf = new CheckFiles();
        System.out.println("CheckFiles starting.");
        cf.getListFromFile();
        cf.getListFromDirectory();
        cf.reportMissingFiles();
        System.out.println("CheckFiles done.");
    }
}

You can replace reportMissingFiles() with below implementation:

public void reportMissingFiles() {
        // Set for containing missing files.
        Set<String> notFoundFileSet = new HashSet<>();
        for (int i = 0; i < listFromFile.size(); i++) {
            if (!listFromDir.contains(listFromFile.get(i))) {
                JOptionPane.showMessageDialog(null, listFromFile.get(i),
                        "File not found", JOptionPane.ERROR_MESSAGE);
                // add missing files in set
                notFoundFileSet.add(listFromFile.get(i).toString());
            }
        }
        JFrame frame = new JFrame("Files Not Found");
        frame.setSize(800, 600);
        frame.setVisible(true);
        JTextField text = new JTextField();
        // Stringify Set of missing file 
        text.setText(notFoundFileSet.toString());
        frame.add(text);
        text.setVisible(true);
    }

I have used Set<String> for containig missing files and the added string version of that Set to JTextField . You can simply use StringBuilder as well to format file names according to your need.

Note: Always prefer Generics in your code, eg instead of using ArrayList listFromFile you should have used ArrayList<String> listFromFile to make your program type safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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