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

The Archive Base Latest Questions

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

There is a common method when using JTable TableCellRenderers for setting the background and

  • 0

There is a common method when using JTable TableCellRenderers for setting the background and foreground when the cell is selected. Here is an example question that was asked:

Why does my Java custom cell renderer not show highlighting when the row/cell is selected?

This solution is lacking one thing … the border around the cell. (Note I am not asking about a border around the row, as was asked here.) The border should highlight when the cell is selected. It is not acceptable to just create your own Border, and set it, because the border you create may not fit in with the Look & Feel.

I’ve successfully got the border by initializing a default renderer, and then scavenging it for its border, as follows:

private final DefaultTableCellRenderer defTblRend = new DefaultTableCellRenderer();
private final JComponent renderer = new ComplexCell(); // Whatever object type extends JComponent
@Override public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row,
    int column)
{
    // ... Set values on "renderer" object here ...
    renderer.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
    renderer.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
    renderer.setOpaque(!renderer.getBackground().equals(table.getBackground()));
    JComponent comp = (JComponent)defTblRend.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    renderer.setBorder(comp.getBorder());
    return renderer;
}

Is there a better way?

  • 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:07:47+00:00Added an answer on May 23, 2026 at 1:07 am

    You might be able to use the UIManager. See UIManager Defaults. “Table.focusCellHighlightBorder” would appear to be the property you want.

    ADDED BY ORIGINAL POSTER:

    Here is the solution I came up with based on camickr’s info. Optimizations/cleanup welcome.

    1. Set up static borders so they are available wherever you need them (I put them in a class called “UiUtils”):

      public static final Border focusedCellBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
      public static final Border unfocusedCellBorder = createEmptyBorder();
      private static Border createEmptyBorder()
      {
          Insets i = focusedCellBorder.getBorderInsets(new JLabel());
          return BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
      }
      
    2. Renderer

      @Override public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column)
      {
          // [... set component values here ...]
          label.setBorder(hasFocus ? UiUtils.focusedCellBorder : UiUtils.unfocusedCellBorder);
          return label;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there in the JDK or Jakarta Commons (or anywhere else) a method that
There's a common way to store multiple values in one variable, by using a
Is there a function in Common Lisp that takes a string as an argument
Is there a site online that lists common regular expressions or has a utility
Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
Are there any Common Lisp implementations for .Net?
Is there a common way to get the current time in or with milliseconds?
Is there a common way to express the usage of arguments in C++? I
There seems to be three common approaches for mapping an application end user to
Is there a list somewhere on common Attributes which are used in objects like

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.