Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6004887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:33+00:00 2026-05-23T01:19:33+00:00

I’m working with the following code that contains a JProgressBar inside of an AbstractTableModel.

  • 0

I’m working with the following code that contains a JProgressBar inside of an AbstractTableModel. The JProgressBar progress value is updated in “case 5: ” of the getValueAt() function by returning a Float. What I’m trying to figure out is how to access the underlying JProgressBar instance so I can modify some of it’s properties other than the progress value (such as setVisible()).

package org.jdamico.jhu.runtime;

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

// This class manages the upload table's data.
class TransfersTableModel extends AbstractTableModel implements Observer {

    /**
     * 
     */
    private static final long serialVersionUID = 2740117506937325535L;

    // These are the names for the table's columns.
    private static final String[] columnNames = { "Type","Name", "UUID","Status",
            "Stage","Progress","Total Start Time","Total End Time" };

    // These are the classes for each column's values.
    private static final Class[] columnClasses = { String.class,String.class,String.class, 
            String.class,String.class, JProgressBar.class, String.class, String.class};

    // The table's list of uploads.
    private ArrayList<ParentEntry> transferList = new ArrayList<ParentEntry>();

    // Add a new upload to the table.
    public void addTransfer(ParentEntry pe) {

        // Register to be notified when the upload changes.
        pe.addObserver(this);

        transferList.add(pe);

        // Fire table row insertion notification to table.
        fireTableRowsInserted(getRowCount(), getRowCount());
    }

    // Get a upload for the specified row.
    public ParentEntry getTransfer(int row) {
        return transferList.get(row);
    }

    // Remove a upload from the list.
    public void clearTransfer(int row) {
        transferList.remove(row);

        // Fire table row deletion notification to table.
        fireTableRowsDeleted(row, row);
    }

    // Get table's column count.
    public int getColumnCount() {
        return columnNames.length;
    }

    // Get a column's name.
    public String getColumnName(int col) {
        return columnNames[col];
    }

    // Get a column's class.
    public Class getColumnClass(int col) {
        return columnClasses[col];
    }

    // Get table's row count.
    public int getRowCount() {
        return transferList.size();
    }

    // Get value for a specific row and column combination.
    public Object getValueAt(int row, int col) {

        ParentEntry pe = transferList.get(row);
        switch (col) {
        case 0:
            return pe.getType();
        case 1: // URL
            return pe.getUrl();
        case 2: //UUID
            return pe.getUUID() + "";
        case 3: // Status
            return pe.getStatus().getDisplay();
        case 4: // Stage
            return pe.getStage().getDisplay();
        case 5: //Progress
            return new Float(pe.getProgress());
        case 6:
            if (pe.getTotalStartTime() != null)
                return pe.getTotalStartTime().getTime().toString();
            else
                return "";
        case 7:
            if (pe.getTotalEndTime() != null)
                return pe.getTotalEndTime().getTime().toString();
            else
                return "";
        }
        return "";
    }

    /*
     * Update is called when a Upload notifies its observers of any changes
     */
    public void update(Observable o, Object arg) {
        int index = transferList.indexOf(o);

        // Fire table row update notification to table.
        fireTableRowsUpdated(index, index);
    }
}

Update: This wasn’t originally my code and I’m still familiarizing myself with it, but I just discovered that there was a ProgressRenderer class that extends JProgressBar and implements TableCellRenderer. This is what is being used to render the progress bar in the table. So I’m going to alter this code to alter how the progress bar is being displayed:

package org.jdamico.jhu.runtime;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/*import org.jdamico.jhu.components.Controller;
import org.jdamico.jhu.components.FilePartition;
import org.vikulin.utils.Constants;*/

// This class renders a JProgressBar in a table cell.
class ProgressRenderer extends JProgressBar implements TableCellRenderer {




    // Constructor for ProgressRenderer.
    public ProgressRenderer(int min, int max) {
        super(min, max);
    }

    /*
     * Returns this JProgressBar as the renderer for the given table cell.
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // Set JProgressBar's percent complete value.
        Float tmp = ((Float) value).floatValue();
        if (tmp == -1) {
            this.setVisible(false);
        }
        else {
            setValue((int) ((Float) value).floatValue());
            if (this.isVisible() == false)
                this.setVisible(true);
        }
        return this;
    }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:19:34+00:00Added an answer on May 23, 2026 at 1:19 am

    You could add a method to the class that allows you to get the JProgressBar associated with the row of interest, something like:

       public JProgressBar getProgressBar(int row) {
          ParentEntry pe = transferList.get(row);
          return pe.getProgress();
       }
    

    It seems kind of funny to me that the model would hold a JProgressBar rather than a simple int representing percentage complete. It would make more sense to me to have any code for progress bar display be in the JTable’s cell renderer.

    Edit 1
    In reply to opike’s comment below:

    Does it really make the code that much more efficient to use the JProgressBar outside of the model vs inside of it? Is the gain enough so to expend time rewriting the code? The existing code works the way I need it to for the most part so I would prefer just tweaking it vs a more significant rewrite.

    My question and my concern is, is your cell renderer using the JProgressBar held by the model? If so, how can it if there is no way for it to obtain the JProgressBar? If it’s only using the Float value obtained, it simply makes no sense for the model to hold information and resources that are not used. And yes, this could slow your program down noticeably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.