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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:37:52+00:00 2026-05-23T10:37:52+00:00

I know changing cell backgrounds in jtable is done by creating a new cellrenderer

  • 0

I know changing cell backgrounds in jtable is done by creating a new cellrenderer class. I’ve done that. I’ve read about DefaultTableRenderer “color memory” issue, but I can’t figure out how to work around it for my particular purpose.

My goal is simple enough: When a button is clicked, change the background color of all selected cells in the jtable.

I have the adequate method calls set up for the event, but I can’t get the renderer to work the way I want it to.

I have all selected cells stored in an arraylist of TableCells (a class containing row, column, and the cell’s text string data). Here is my code for getTableCellRendererComponent inside of my CustomCellRenderer.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
     for(TableCell c: selectedCells)
     {
         if(c.row ==row && c.col == column)
         {
             this.setBackground(Color.black);
         }
         else
         {
             this.setBackground(Color.BLUE);
         }
     }
     return this;
  }

This code sets the background of all table cells’ backgrounds to blue. Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.

Thanks.

  • 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-23T10:37:53+00:00Added an answer on May 23, 2026 at 10:37 am

    How about giving your renderer class a boolean variable, say btnClicked that is initialized to false but set to true in the button’s ActionListener, a listener which also instructs the table to repaint itself. Then in the renderer itself, you could use the selected property to see if a cell is selected or not. Perhaps something like:

       private boolean btnClicked = false;
    
       public void setBtnClicked(boolean btnClicked) {
          this.btnClicked = btnClicked;
       }
    
       public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
          if (btnClicked) {
             if (isSelected) {
                setBackground(Color.black);
             } else {
                setBackground(Color.blue);
             }
          } else {
             // if button not clicked
             setBackground(Color.lightGray);
          }
          return this;
       }
    

    Also regarding:

    Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.

    What is this “color memory issue that you speak of?

    Edit 1
    Here is a compilable example of what I meant. I’m still not sure what you mean by the color memory issue though, sorry.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    @SuppressWarnings("serial")
    public class DisplaySelectedTableCells extends JPanel {
       public static final Integer[][] DATA = {
          {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
          {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
          {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
       };
       public static final String[] COLS = {"A", "B", "C"};
       private static final int PREF_WIDTH = 400;
       private static final int PREF_HEIGHT = 300;
       private DefaultTableModel model = new DefaultTableModel(DATA, COLS) {
          @Override
          public Class<?> getColumnClass(int columnIndex) {
             return Integer.class;
          }
       };
       private JTable table = new JTable(model);
       private JToggleButton toggleBtn = new JToggleButton("Show Selected Rows");
       private MyCellRenderer myCellRenderer = new MyCellRenderer();
    
       public DisplaySelectedTableCells() {
          table.setDefaultRenderer(Integer.class, myCellRenderer);
          JPanel btnPanel = new JPanel();
          btnPanel.add(toggleBtn);
    
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
          add(btnPanel, BorderLayout.SOUTH);
    
          toggleBtn.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                myCellRenderer.setShowSelected(toggleBtn.isSelected());
                table.repaint();
             }
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_WIDTH, PREF_HEIGHT);
       }
    
       private static class MyCellRenderer extends DefaultTableCellRenderer {
          private static final Color SELECTED_COLOR = Color.pink;
          private static final Color UNSELECTED_COLOR = Color.lightGray;
          private static final Color BASE_COLOR = null;
          private boolean showSelected = false;
    
          public void setShowSelected(boolean showSelected) {
             this.showSelected = showSelected;
          }
    
          @Override
          public Component getTableCellRendererComponent(JTable table,
                   Object value, boolean isSelected, boolean hasFocus, int row,
                   int column) {
             Component superComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                      row, column);
    
             if (showSelected) {
                if (isSelected) {
                   superComponent.setBackground(SELECTED_COLOR);
                } else {
                   superComponent.setBackground(UNSELECTED_COLOR);
                }
             } else {
                superComponent.setBackground(BASE_COLOR);
             }
    
             return superComponent;
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("DisplaySelectedTableCells");
          frame.getContentPane().add(new DisplaySelectedTableCells());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how I would go about changing (transforming) an image based on
I know that with anonymous functions, local stack variables are promoted to a class,
I know that refactoring is changing the structure of a program so that the
Does anyone know why when changing the language on the iphone, the bluetooth message
Does anyone know a possibility to protect a string-wildcard from changing in a TextArea?
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Everything I've read says that storing serialised arrays in Mysql is a bad idea
Although my site is still far from done, I've started thinking about web security.
I would like to know if somebody knows a library to changing the playback
As i know,MPVolumeView can add to my app for changing volume.But now i want

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.