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

  • Home
  • SEARCH
  • 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 4573362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:46:29+00:00 2026-05-21T19:46:29+00:00

Hy everyone, I’m trying to do some calculations in a JTable after iteration and

  • 0

Hy everyone,

I’m trying to do some calculations in a JTable after iteration and mark with different background the cells that I need.

However, I have 2 problems at the moment:
1) Cells are not painted immediately, but after the whole iteration cycle
2) Areas are not painted properly – if I need to paint table[3, 4] and table[6, 5] it draws a rectangular from [3,4] up to [6, 5] instead of drawing the singular cells only.

About problem 1: could I call repaint() with priority without the need to finish everything and the JVM to decide whenever to paint? I try to do fireTableCellUpdated() and fireTableDataChanged() but they don’t get updated.

Here there is my custom cell renderer method that changes the BGcolor:

public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row,int column) {
    Component renderer = super.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, column);

    if(value instanceof Color) {
        Color c = (Color) value;
        renderer.setBackground(c);
        System.out.println("BG change [" + row + ":" + column + "]");
    }

    return renderer;
}

Here there is the loop where I paint my cells on a button click:

for(int paintJ = startIndex; paintJ < endIndex; paintJ++) {
     CrossCellRenderer rend =  (CrossCellRenderer) jTable1.getCellRenderer(i,  paintJ)
           .getTableCellRendererComponent(jTable1, Color.blue, true, true, i, paintJ);
     crossTableModel.fireTableCellUpdated(i, paintJ);
     jTable1.revalidate();
     jTable1.repaint();
     try {
          Thread.sleep(1000);
     }   catch(InterruptedException ie) {
           System.err.println("Exception sleeping the thread.");
     }
 }

Any tips on that?

  • 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-21T19:46:30+00:00Added an answer on May 21, 2026 at 7:46 pm

    Hate to say: but you do it (whatever you want to reach) completely wrong 😉

    • Never-ever do any direct manipulation/paint on a renderer, instead change the model data and the rest will happen automatically.
    • Never-ever call any of the fireXX methods on a model from outside the model, it’s the exclusive responsibility of the model to notify
      its listeners on change
    • Never-ever (or very very rarely, certainly not here 😉 there’s a need to call revalidate and/or repaint dircectly, will happen
      automatically if a model is well-behaved
    • ….

    Best to read Snoracle’s tutorial on howto-use tables to fully understand the rendering mechanism

    http://download.oracle.com/javase/tutorial/uiswing/components/table.html

    Roughly:

       // change the model, will notify its listeners
       model.setValueAt(....)
    
       // in a custom renderer, check the value and decorate as appropriate
       public Component getTableCellRendererComponent(....) {
            // ... normal config, f.i. done in super
            Component comp = super.get...
            if (myConditionForSpecialColor(table, value, ...) {
                comp.setBackground(myColor);
            } else {
                comp.setBackground(normalColor);
            }
       }
    
       // register the custom renderer 
       // per class
       table.setDefaultRenderer(Object.class, myRenderer)
       // or per column
       table.getColumnModel().getColumn(myColumn).setCellRenderer(myRenderer) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone. I'm trying to get data from some page, it's updated using javascript. First,
Everyone has accidentally forgotten the WHERE clause on a DELETE query and blasted some
Everyone managing open-source-software runs into the problem, that with the time the process of
Everyone uses source-code control to manage versions (right?) and this provides some level of
everyone. 1) I've always thought that if I leave my app through Home button
everyone. I am trying to create a database in which I can store pictures
Everyone remembers google browser sync right? I thought it was great. Unfortunately Google decided
Everyone I work with is obsessed with the data-centric approach to enterprise development and
Everyone (at least everyone who uses a compiled language) has faced compilation errors but
Everyone has this huge massively parallelized supercomputer on their desktop in the form of

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.