javaswingjlist

Is there a way to get vertical index with a JList?


I am trying to get the vertical index from a JList. My program saves website, username, and passwords in a list to a text file like this:

website
username
password

When I use a for loop to create a new instance of my usernamesAndPasswords class it reads it like this:

website website website
username username username
password password password. 

The code I think causing the problem is this:

        save.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String outFileName;
            String website, username, password;
            ArrayList<UsernamesAndPasswords> upInfo = new
                    ArrayList<UsernamesAndPasswords>();
            try{
                for (int i = 0; i < list1.getSize(); i++){
                    website = list1.getElementAt(i);
                    username = list1.getElementAt(i);
                    password = list1.getElementAt(i);
                    upInfo.add(new UsernamesAndPasswords(website, username, password));
                }

                Scanner sc = new Scanner(System.in);
                System.out.println("Enter the name of the file to write to: ");
                outFileName = sc.nextLine();
                upc.saveToTextFile(upInfo, outFileName);
            } catch (Exception ex){
                JOptionPane.showMessageDialog(null, "There was" +
                        "an error saving the file");
            }
        }});

If this just doesn't make sense let me know how I can fix it. Thanks.


Solution

  • Look at your loop:

    for (int i = 0; i < list1.getSize(); i++){
        website = list1.getElementAt(i);
        username = list1.getElementAt(i);
        password = list1.getElementAt(i);
        upInfo.add(new UsernamesAndPasswords(website, username, password));
    }
    

    When you run into an indexing issue like this, try to use your brain as a debugger by substituting values:

    // Loop iteration 1 (index = 0)
    
    website = list1.getElementAt(0);
    username = list1.getElementAt(0);
    password = list1.getElementAt(0);
    

    The first element in your list is a website name, but you don't increment the index before trying to set the corresponding username or password. You want:

    website = list1.getElementAt(0);
    username = list1.getElementAt(1);
    password = list1.getElementAt(2);
    

    Based on the structure of your file, you obviously need to increment that index within your loop.

    website = list1.getElementAt(i);
    i++;
    username = list1.getElementAt(i);
    i++;
    password = list1.getElementAt(i);
    

    The i++ of your for loop will take care of incrementing the index to element 4, and you have to change getSize() as the loop's exit condition to getSize() - 2 to avoid index out of bounds.

    You could also switch to saving your list with each website and corresponding data on its own line and splitting based on some separator (tab, comma, etc.), which may make things more conceptually simple for you or someone else looking at your code, but that change is not only somewhat trivial to implement, but also somewhat trivial in value.