Klassik
Klassik

Reputation: 11

I am trying to make a JFrame that displays a list of elements each representing a file in a directory

Okay so I am making a software that pulls user information from a folder of text files. Each text file has 2 lines. The first/last name and the phone number. I am making code that opens up a JFrame that displays a list of all the users in the folder. I wrote some code and tried troubleshooting it but I have no idea why it isn't working. I think it's a GUI thing but i've looked through so many java docs examples, I can't see what I did wrong.

I have my code in this method:

public void createDir() {
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setLocationRelativeTo(null);
f.setTitle("User Directory");

DefaultListModel listModel = new DefaultListModel();
JList list = new JList(listModel);

File directory = new File("Z:\\Documents\\users");
File[] listOfUsers = directory.listFiles(); // returns a file array of all txt files in the user folder

String n = null;
String p = null;
Scanner s;
for (File file : listOfUsers) // runs for each file in the listOfUsers file array, replacing 'file' with the current userfile
{
    if (file.isFile()) // if the item selected is a file
    {
        try
        {
            s = new Scanner(file); // creates a scanner to read the current user file
            int i = 0; // sets the counter that is used to read name and phone number associated with the file

            while(s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
            {   
                if(i==0)
                    n = s.nextLine();
                if(i==1)
                    p = s.nextLine();
                                i++;
            }

        listModel.addElement(n + " " + p); // the user shows up on the list item as "First Last ###-###-####"
        s.close(); // closes the scanner for re-use

        }
        catch (FileNotFoundException e) // incase of exception thrown
        {
            System.out.print("exception occured: " + e);
        }               
    }
}

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setVisibleRowCount(5);
    list.setSelectedIndex(0);
    JScrollPane listScrollPane = new JScrollPane(list); // creates a scrollpane for the JList component 'list'

    JPanel dirPanel = new JPanel();
    f.add(dirPanel);
    dirPanel.add(listScrollPane, BorderLayout.CENTER); // adds the component
}

Whenever the method is called, the JFrame appears but the entire window is black except for a small white sliver in the corner. At that point, I can't close it and it is frozen. And if you're thinking that it isn't closing because f.setDefaultCloseOperation() isn't called, it's because this is a method that is called by the actionListener of a button on the MAIN program. I get no error message and I have no idea what I should do. The entire program freezes up and becomes uncloseable.

Upvotes: 1

Views: 198

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

Having tested your code, you have a potential infinite loop.

while(s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
{   
    if(i==0)
        n = s.nextLine();
    if(i==1)
        p = s.nextLine();
    i++;
}

Basically, here, if the file actually has more then two lines, you loop will never exit, because you basically ignore any other lines

So, instead, either you need to ignore anything after the second line and break out of the loop or you need to throw an Exception so you know which files are giving your problems...

try (Scanner s = new Scanner(file)) {
    int i = 0; // sets the counter that is used to read name and phone number associated with the file

try (Scanner s = new Scanner(file)) {
    int i = 0; // sets the counter that is used to read name and phone number associated with the file

    while (s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
    {
        switch (i) {
            case 0:
                n = s.nextLine();
                break;
            case 1:
                p = s.nextLine();
                break;
            default:
                throw new IOException("Invalid file format, more then two lines have been found!");
        }
        i++;
        System.out.println(i);
    }

    listModel.addElement(n + " " + p); // the user shows up on the list item as "First Last ###-###-####"

} catch (IOException e) // incase of exception thrown
{
    System.out.print("exception occured: " + e);
    e.printStackTrace();
}

Also, as has already been noted, you should be calling setVisible last, after you have created the UI.

Have a look at The try-with-resources Statement for more details about the try (Scanner s = new Scanner(file)) { line

Upvotes: 1

Ibraheem
Ibraheem

Reputation: 100

Put f.setVisible(true) after add components to JFrame. i.e in the last line rather than the second

Upvotes: 0

Related Questions