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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:08:02+00:00 2026-05-25T06:08:02+00:00

my question is, how to solve following problem: If i click on a cell

  • 0

my question is, how to solve following problem:

If i click on a cell in a JTable, i want to change its background. If i release the mousebutton, i want the background change back to normal color.

Is that possible?

Greetings ayk

  • 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-25T06:08:03+00:00Added an answer on May 25, 2026 at 6:08 am

    in your renderer you have to overide hasFocus

    for example

    enter image description here

    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableRenderer extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JTable table;
        private String[] columnNames = {"Date", "String", "Centered", "Integer", "Boolean"};
        private Object[][] data = {
            {new Date(), "A", "A", new Integer(1), true},
            {new Date(), "B", "B", new Integer(2), false},
            {new Date(), "C", "C", new Integer(10), null},
            {new Date(), "D", "D", new Integer(4), false}
        };
    
        public TableRenderer() {
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            table = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            getContentPane().add(scrollPane);
    
            //  Override default renderer on a specific Class
            TableCellRenderer colorRenderer = new ColorRenderer();
            table.setDefaultRenderer(String.class, colorRenderer);
    
            //  Override default renderer for a specific column
            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(JLabel.CENTER);
            table.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TableRenderer frame = new TableRenderer();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        /*
         **  Color the focused cell
         */
        private class ColorRenderer extends DefaultTableCellRenderer {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (hasFocus) {
                    setBackground(Color.cyan);
                } else if (isSelected) {
                    setBackground(table.getSelectionBackground());
                } else {
                    setBackground(table.getBackground());
                }
                return this;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve this problem, its not a homework question, its just code
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
Before starting writing this question, i was trying to solve following // 1. navigate
I have the following cyclic dependency problem I am trying to solve: typedef std::map<int,
I'm I'm trying to solve the following problem with linq (already solved it with
its me again with a php problem :) Following is part of my PHP
Following this question , I've met another problem where the request just doesn't go
I've used this question as a template to solve the same problem, but I'm
I am just curious if there is an elegant way to solve following problem
I need to solve the following question which i can't get to work by

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.