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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:51:32+00:00 2026-05-23T11:51:32+00:00

I am using the following code: public final class TableCellRendererCenter extends DefaultTableCellRenderer { public

  • 0

I am using the following code:

public final class TableCellRendererCenter extends DefaultTableCellRenderer {

    public static final TableCellRenderer INSTANCE
            = new TableCellRendererCenter();

    protected TableCellRendererCenter() {

        // Calling super
        super();

        this.setHorizontalAlignment(SwingConstants.CENTER);

    }

}

on a JTable column:

    TableColumnModel retrMod = ChartItemsTable.getColumnModel();
    TableColumn retrCol = retrMod.getColumn(2);

    retrCol.setHeaderRenderer(TableCellRendererCenter.INSTANCE);
    retrCol.setCellRenderer(TableCellRendererCenter.INSTANCE);

and the look-and-feel does not match other column headers anymore:

enter image description here

Why? How can I solve this issue?

EDIT

It seems like NetBeans is using DefaultTableCellHeaderRenderer which is coming from package sun.swing.table;

I read everywhere that I should not use sun packages. Grrrr… this does not help!

EDIT 2

The suggestion of 0verbose produces the following:

enter image description here

SOLUTION

Following eugener’s suggestion, I updated my code as following:

public final class TableCellRendererCenter extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {

        // returns component used for default header rendering
        // makes it independent on current L&F

        Component retr = table.getTableHeader().getDefaultRenderer().
            getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, column);

        if ( JLabel.class.isAssignableFrom(retr.getClass()) ) {

            JLabel jl = (JLabel) retr;
            jl.setHorizontalAlignment(SwingConstants.CENTER);

        }

        return retr;

    }

    @Override
    public void validate() {}

    @Override
    public void revalidate() {}

    @Override
    public void firePropertyChange(
        String propertyName, boolean oldValue, boolean newValue) {}

    @Override
    public void firePropertyChange(
        String propertyName, Object oldValue, Object newValue) {}

}

And I get this:

enter image description here

Look-and-feel is preserved and column header is centered !!! (Other column headers are centered too, but I can control this by making further tests on the int row, int column parameters).

  • 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-23T11:51:33+00:00Added an answer on May 23, 2026 at 11:51 am

    The issue is that you are using different cell renderer then the one used by your LAF. IMO the only way to achieve proper LAF is to use renderer already provided by LAF itself such as:

    public class TableHeaderRenderer extends JComponent implements TableCellRenderer {
    
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                            boolean hasFocus, int row, int column) {
    
                    // returns component used for default header rendering
                    // makes it independent on current L&F
    
            return table.getTableHeader().getDefaultRenderer().
                              getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            }
    
            // following methods are overriden for performance reasons
    
            @Override
            public void validate() {}
    
            @Override
            public void revalidate() {}
    
            @Override
            public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    
            @Override
            public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
    
    }
    

    Obviously, once you have proper renderer, you can cast it to a JLabel (after checking that it is, of course) and then center your text or do something else.

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

Sidebar

Related Questions

I have a question about the following code: public Class Settings{ public static final
I was just trying to understand delegates using the following code. public class delegatesEx
The following code using System.Threading; class Test { volatile int counter = 0; public
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I'm using Eclipse, and it is perfectly happy with the following code: public interface
I'm running the following code on Ubuntu 10.10, using OpenJDK 1.6.0_18: package mypkg; public
Consider the following code: public abstract class Base { public void getAnswer(); } public
Please look at the following code listing: #include <iostream> using namespace std; class Base

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.