Search code examples
javaswingfor-loopjtablefilereader

How to read specific lines of a txt file and display to JTable


I'm trying to display the txt file into my JTable. The purpose is to show the usernames and the account type. I have set a JComboBox that has the choices Admins and Cashiers.

The txt file is used for the log in screen, and it is stored in a txt file. This is for a school project only.

Content of txt file of admin.txt:

Username:admin
Password:password
Type:admin

Username:admin2
Password:password2
Type:admin

Output:

enter image description here

This is the code:

 String filePath = "C:\\Users\\zagad\\IdeaProjects\\DATABYTES\\login\\admin.txt";
                File file = new File(filePath);
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    DefaultTableModel model = (DefaultTableModel) userTable.getModel();
                    Object[] tableLines = br.lines().toArray();

                    for (int i = 0; i < 7; i++) {
                        String line = tableLines[i].toString().trim();
                        String[] dataRow = line.trim().split(" ");
                        model.addRow(dataRow);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

What I want is to hide the text "Username: , Password: and Type:". I just want to show the username on the left and the account type on the right, which is admin.

I am not sure on how to code to split it and display them to their following JTable cells. And, the user will be adding more accounts, so the list will be longer, with the same format.

Any help would be appreciated thank you!

Expected output:

enter image description here

After trying the code:

enter image description here


Solution

  • You need to parse the data in your file.

    One way might be to create a HashMap of each pair of data.

    Then as your read each line in the file you would:

    1. split the data into two fields
    2. add the data to the HashMap
    3. when you have 3 items in the HashMap it is now time to add the data to the TableModel. Then you clear the HashMap.

    So the basic structure might be something like:

    HashMap<String, String> userInfo = new HashMap<String, String>();
    
    //for (int i = 0; i < 7; i++) // don't make assumptions about the size
    for (int i = 0; i < tableLines.length; i++) 
    {
        String line = tableLines[i].toString().trim();
        String[] dataRow = line.trim().split(" "); // why are you splitting on " "???
        userInfo.put(dataRow[0], dataRow[1]);
    
        if (userInfo.size() == 3)
        {
            Vector<String> row = new Vector<String>();
            row.add( userInfo.get("Username") );
            row.add( userInfo.get("Type") );
            model.addRow( row );
            userInfo.clear();
        }
    }
    

    Of course this solution assumes that the data is always in the correct format. You would also need error checking code for the blank line since the split(…) method will not return any data.