FAQ for HW3

Last modified: August 7, 2002 1:48:29 AM PDT

  • Why does the getContentLength method keep returning -1?
  • Why doesn't any of my data show up in the table until after ...
  • How can I get progress bars into my JTable?

    Back to the CS193J Homepage




    Q:

    Why does the getContentLength method keep returning -1?

    A:

    This is ok -- you're not doing anything wrong (well, maybe you are, but this issue is not indicative of that). Many sites don't give the content length, which means you can't track progress or determine the total download size before downloading. Note that you can, however, still track throughput.
    Back to top...


    Q:

    Why doesn't any of my data show up in the table until after all the links are processed?

    A:

    You are probably not spawning off a separate thread to process the starting link. You might reason that this is really not necessary; you can just have the "main" thread download the starting link and then spawn off threads to download subsequent links. You will most likely have an action listener for the button that starts the link testing, and you will be overriding the actionPerformed method. But, and this is the crucial part, the code being executed inside the actionPerformed method is NOT executed on the main thread, but rather on the Swing thread. All event notifications (actionPerformed, mouseClicked, whatever) happen on the Swing thread, and there's only one of those (Swing threads, that is). So, if you don't spawn off another thread inside the actionPerformed method to do the actual processing, you will be hogging the Swing thread and no GUI updates will occur until the actionPerformed method completes, at which point the Swing thread will be returned to the system. Of course, by this time, all of the link processing will be completed and the table data just appears all at once on the screen.
    Back to top...


    Q:

    How can I get progress bars into my JTable?

    A:

    Here's the story on getting progress bars into your table:

    To change the way a table cell displays, you need to create a class that implements the TableCellRenderer interface and install it in that particular cell. Implementing TableCellRenderer requires the implementation of one method:

    Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

    Basically, when the table wants to redraw the cell, it will ask your TableCellRenderer object for a Component, and it tell that component to paint itself. So your getTableCellRendererComponent method needs to hand over a pre-configured JProgressBar whenever it gets messaged with this method. See the TableCellRenderer interface for details about the arguments that get passed.

    You'll want to install a JProgressbar-enabled TableCellRenderer into the "Progress" column (one such TableCellRenderer is enough for the whole table). In order to install the TableCellRenderer, you can do as follows:
    table.getColumn("Progress").setCellRenderer(new ProgressRenderer());

    Check out the API pages for JTable to know more about the getColumn and setCellRenderer methods.

    Back to top...