0

I'm at a complete loss here. I have a method that goes through a Vector of TreePaths containing a custom FileNode class, and extracts the File contained in each last node. The result is a printed list of all the filenames in the Vector. I had a for loop to increment through it, and it worked fine, but I changed it to a while loop to allow me to have multiple pages. Now, for some reason, it only prints the last 11 files.

I've figured out what's causing it, but I don't know why or how to fix it. I added in lines to print to the console 'lineCount' and 'printPaths.size()', and discovered that lineCount was incrementing until it hit 55, then reverting to 15 and continuing to increment. The names did not actually start printing until it reverted to 15.

I've stepped through it in debug mode, and it goes through the print loop 55 times, returns the code to the print function that tells it it's part of the printed page, then it drops into a print method for which I don't have the source, and when I step back up out of that, it is back at the beginning of the loop, and lineCount is at 14. The really odd part is that's when it prints out the 11 files, even though according to the program it hasn't even looked at that part of the Vector yet.

If anyone has the slightest idea what's causing that, I would really appreciate the help. Here's the chunk of code that deals with printing the list. Hopefully that's enough.

        lineCount = 13;
    int lineSpacing = 14;
    g.setFont(new Font("Dialog", Font.PLAIN, 10));
    boolean color = true;
    if (summ) {
        g.drawString(((TreePath) printPaths.get(0)).getPathComponent(
                ((TreePath) printPaths.get(0)).getPathCount() - 3)
                .toString()
                + " : "
                + ((TreePath) printPaths.get(0)).getPathComponent(
                        ((TreePath) printPaths.get(0)).getPathCount() - 5)
                        .toString(), 36, lineCount * lineSpacing);
        lineCount++;
        //for (int j = 1; j < printPaths.size(); j++) {
        while((printPaths.size()>1) && lineCount<55){
            String type = ((TreePath) printPaths.get(1)).getPathComponent(
                    ((TreePath) printPaths.get(1)).getPathCount() - 5)
                    .toString();
            String date = ((TreePath) printPaths.get(1)).getPathComponent(
                    ((TreePath) printPaths.get(1)).getPathCount() - 3)
                    .toString();
            String typeU = ((TreePath) printPaths.get(0))
                    .getPathComponent(
                            ((TreePath) printPaths.get(1)).getPathCount() - 5)
                    .toString();
            String dateU = ((TreePath) printPaths.get(0))
                    .getPathComponent(
                            ((TreePath) printPaths.get(1)).getPathCount() - 3)
                    .toString();
            if (!(type == typeU) && (date == dateU)) {
                lineCount++;
                g.setColor(c1);
                g.drawString(date + " : " + type, 36, lineCount
                        * lineSpacing);
                lineCount++;
            }
            if(color)
                g.setColor(c1);
            else
                g.setColor(c2);
            g.drawString(((TreePath) printPaths.get(1))
                    .getLastPathComponent().toString(), 54, lineCount
                    * lineSpacing);
            color=!color;
            lineCount++;
            printPaths.remove(0);
            System.out.println(printPaths.size());
            System.out.println(lineCount);
        }
    }
SaintWacko
  • 854
  • 3
  • 14
  • 35

2 Answers2

1

You appear to be removing from the collection printPaths while iterating through it in a forward direction. If so, I'm not surprised that this might be messed up. Consider using an iterator instead. You also appear to be doing this logic portion of your program inside of a paint or paintComponent method which is a big no-no. Do your logic outside of this method as you do not have full control over when or even if the method will fire.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • It's not technically iterating through it. The drawStrings grab from the same location each time, but the Path at the front is being removed, so it has the same effect as an iteration. I'm trying to do it that way so for the second page I can just use the same Vector. Would I be able to do something like that with an Iterator? And all of the logic there is inside of the print() method because that's the only place it's used. I tried to put it outside, but there doesn't seem to be any way to pass arguments to the print() method, so it wasn't working very well. – SaintWacko Nov 18 '11 at 23:27
  • Use the `remove()` method of the `Iterator`, rather than that of the `Vector`. – trashgod Nov 19 '11 at 03:35
0

I eventually discovered that Java implements pagination pretty well on its own, without needing the stuff I was trying to do.

SaintWacko
  • 854
  • 3
  • 14
  • 35