0

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.");
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"I want to display the missing file names in a textfield"* Better to display them in a list [`JList`](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html) or [`JTable`](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html).. See the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784) for use of good (file) icons in a tree or table. – Andrew Thompson Apr 11 '16 at 09:50
  • Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Apr 11 '16 at 09:55
  • `text.getSelectedFile().getName()` You should have mentioned that this code will not compile.. – Andrew Thompson Apr 11 '16 at 10:00
  • removing that the code works fine. only problem is i want to display the missing file names in textfield. – Pavan Kumar Apr 11 '16 at 10:17
  • *"i want to display the missing file names in textfield."* Umm.. yes. Why do you keep repeating that? – Andrew Thompson Apr 11 '16 at 10:31
  • i am not gettin how to store it in a variable and then display it in a new textfield in new frame.. pls help me with it. m trying from past 4 days.. :( – Pavan Kumar Apr 11 '16 at 10:33
  • *"..in new frame.."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) And since you seem to be ignoring my suggestion of a list or table of missing files, why a text field specifically? – Andrew Thompson Apr 11 '16 at 10:37
  • oh k.. but how could i be able to add a text box in the same frame?? as the files list may be many,i want to list them all in text field else textlabel. – Pavan Kumar Apr 11 '16 at 10:43
  • please send me the code you suggest to. – Pavan Kumar Apr 11 '16 at 10:49
  • *"as the files list may be many,i want to list them all in text field else textlabel."* A list or a table, each in a scroll-pane, would be far better than a text field or a label. *"but how could i be able to add a text box in the same frame??"* Please fix that stuck '?' key. One '?' indicates a question, 2 or more indicates a buffoon. Why not put the text field (list or table) into the frame at the beginning, but set the contents or model when needed? Also note that the accepted answer to the question about multiple frames suggested many possibilities for doing this in the same or different.. – Andrew Thompson Apr 11 '16 at 10:55
  • .. top level container. I'm beginning to get the impression you are not following links and trying to understand the content. *"please send me the code"* That's not how SO works. Make an attempt yourself and ask a specific question when you run into a problem. Don't ask for code. – Andrew Thompson Apr 11 '16 at 10:56

1 Answers1

0

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.

justAbit
  • 4,226
  • 2
  • 19
  • 34