Question

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);
        }
    }
Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top