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 8675833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:02:33+00:00 2026-06-12T20:02:33+00:00

I have a JTable and one of its columns should display an image; I

  • 0

I have a JTable and one of its columns should display an image;
I overrided getTableCellRendererComponent method of DefaultTableCellRenderer to do this.
But the problem is while the image is not Null & cell is displaying it this method is called over & over (like it is called in an infinite loop) and uses 100% of CPU! (When the image is Null there is no problem!).

What is the problem?

My extended class is:

public class imageCellRenderer extends DefaultTableCellRenderer{

    @Override
    public void validate() {}
    @Override
    public void revalidate() {}
    @Override
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
    @Override
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(isSelected) {
            this.setBackground(table.getSelectionBackground());
            this.setForeground(table.getSelectionForeground());
        }else {
            this.setBackground(table.getBackground());
            this.setForeground(table.getForeground());
        }
        this.setIcon(null);
        Image Scaled;
        ImageIcon tmp;
        System.out.println("CR");
        if(value != null){
            System.out.println("CRP");
            if(value instanceof ImageIcon){
                tmp=(ImageIcon)value;
            }else{
                tmp=new ImageIcon(value.toString());
            }
            int w=tmp.getIconWidth();
            int h=tmp.getIconHeight();
            int refW=100;
            int refH=100;
            double ratio=(double)w/(double)h;
            if(w!=refW && h!=refH){
                int nh,nw;
                double relx=(double)w/(double)refW;
                if(((double)h/relx)<refH){
                    nh = (int) Math.round(refW / ratio);
                    nw = refW;
                }else{
                    nw=(int)Math.round(refH*ratio);
                    nh=refH;
                }
                Scaled=tmp.getImage().getScaledInstance(nw, nh, Image.SCALE_SMOOTH);

                tmp=new ImageIcon(Scaled);
            }
            this.setIcon(tmp);
            table.setRowHeight(row, tmp.getIconHeight());

            this.setSize(100, tmp.getIconHeight());
            this.setHorizontalAlignment(JLabel.CENTER);
        }
        return this;
    }

}

and I use it like this:

jTable1.getColumnModel().getColumn(2).setCellRenderer(new imageCellRenderer());
  • 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-06-12T20:02:35+00:00Added an answer on June 12, 2026 at 8:02 pm

    Remove the

    table.setRowHeight(row, tmp.getIconHeight());
    

    call from the renderer. This call will adjust the row height which triggers the renderer again

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

Sidebar

Related Questions

I have created one Jtable.This table consist of two columns name and timestamp. I
I have created one jTable. I want to display the data into table from
I have a JTable inside a JScrollPane. In one of the columns in the
My question like this: I have a JTable, I put a JProgressBar in one
I have a ScrollPane and JTable that should only have one row of data.
I have a JTable with four columns, the first one containing either a number
I have a JTable where one of the columns is an amount in dollars
Hi have a Jtable with a specific cell rendered model for one column. This
I have a JTable instance, containing a number of rows. The columns in this
I have a problem with the selection of rows on my JTable instances. This

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.