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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:21:58+00:00 2026-05-18T00:21:58+00:00

I have a JTable where I want to highlight some rows using a custom

  • 0

I have a JTable where I want to highlight some rows using a custom background-color. This I have done with the following class:

public class MyTable extends JTable {

    private List<RefData> data = null;

    public List<RefData> getData() {
        return data;
    }

    public void setData(List<RefData> data) {
        this.data = data;
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component comp = super.prepareRenderer(renderer, row, column);

        if (this.data == null || row < 0 || row > this.data.size()-1){
            return comp;
        }

        RefData rowData = this.data.get(row);
        if (rowData.getStatus() < 3000){
            comp.setBackground(Color.YELLOW);
        } else {
            comp.setBackground(Color.WHITE);
        }

        return comp;
    }

}

All this works like a charm, and I get exactly what I want. Next, while looking at the resulting GUI, I realise that the table looks way too condensed. Everything looks squished together. As always with the default JTable settings 😉

Well, that’s easily solved I thought:

myTable.setIntercellSpacing(new java.awt.Dimension(10, 1));

Now, the cells are nicely spaced but, the added cell margins are now in the default table background color, which in my case is white. This looks butt-ugly.

I assume that the intercell spacing adds spacing between the cell border and the component returned by prepareRenderer. This would explain the result. But how can I get it to change the background of the cell itself?

Is my prepareRenderer solution is not suited for this task? Or is there another solution?

  • 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-18T00:21:58+00:00Added an answer on May 18, 2026 at 12:21 am

    I now came up with the following solution. It seems OK to me. But can someone please confirm that this is the correct (or the right way) of doing it in wing please?

    public class MyTable extends JTable {
    
        // [snip]
    
        private Border paddingBorder = BorderFactory.createEmptyBorder(2, 3, 2, 3);
    
        // [snip]
    
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component comp = super.prepareRenderer(renderer, row, column);
    
            if (JComponent.class.isInstance(comp)){
                ((JComponent)comp).setBorder(paddingBorder);
            }
    
            // [snip]
        }
    
        // [snip]
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Jtable and I want to highlight a row by adding a
I have a JTable that I want to use to display some data (a
I have a JTable with a custom TableModel called DataTableModel . I initialized the
I have a custom cell renderer set in JTable and it works but instead
I have a JTable in Java that has a custom dataMOdel and custom renderer.
I have a JTable with a set of uneditable cells and I want all
Suppose you have a JTable and for each cell you want to display three
I have a JTable of data. I want it so that when a row
I have a JTable with JLabel[][] as data. Now I want to detect a
I have a JTable displays the event accordingly, I want to do like when

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.