How to detect a cell lost focus in a JTable?

Posted on 2002-04-10
Last Modified: 2012-06-27
Hello all,

 Here is the problem. Within a Jframe I have a JTable and as a user keys in values to the JTable, I want to perform an action, as they move from cell to cell. How would I do this? Code example would be great.

Question by:Dsys
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
LVL 15

Expert Comment

ID: 6931943
Create a custom CellRenderer for the table and then add a FocusListener to it.

For help on implementing these two classes see the JDK documentation.


Author Comment

ID: 6932090

 I dont see CellRenderer in SDK doc 1.4, I see one called CellRendererPane?

 Do you have a code example?
LVL 15

Expert Comment

ID: 6932413
Sorry, its TableCellRenderer.

I seem to remember you just extend DefaultTableCellRenderer. (Basically its a specialised JLabel.)

Create a class that extends DefaultTableCellRenderer and implements the FocusLister interface.

Then you just write code for the focusGained and focusLost event methods from the FocusListener interface.

I'm afraid I don't have any sample code at the moment as my dev machine got trashed.

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Accepted Solution

Igor Bazarny earned 200 total points
ID: 6933381

Try a piece of code below, does it solve your problem?

Igor Bazarny,
Brainbench MVP for Java 1

package ee.swing;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableSelection{
    public static void main(String[] args){
        final JTable table = new JTable(
            new Object[][]{{"1-1", "1-2", "1-3"},
             {"2-1", "2-2", "2-3"},
             {"3-1", "3-2", "3-3"},
             {"4-1", "4-2", "4-3"},
            ,new Object[]{"1", "2", "3"});
        JFrame frame = new JFrame("Table Selection");
        ListSelectionModel rowSelection = table.getSelectionModel();
        ListSelectionModel colSelection = table.getColumnModel().getSelectionModel();
        ListSelectionListener lst = new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event){

    private static void printSelection(JTable table){
        int col = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
        int row = table.getSelectionModel().getLeadSelectionIndex();
        System.out.println("Lead selection row: "+row+", col: "+col);

Expert Comment

ID: 6956390
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link 
POINTS FOR EXPERTS awaiting comments are listed here ->
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thank you everyone.
Moderator @ Experts Exchange

Expert Comment

by:Igor Bazarny
ID: 6957467

I produced some working code and suggested it to Dsys. AFAIU it's one of the possible solutions to described problem. I think I deserve points.

LVL 15

Expert Comment

ID: 6957552

I produced a clear design pattern describing how to implement the appropriate listeners but refering the asker to the documentation for further information thereby encouraging them to read and learn for themsleves.

Give a man a piece of code and he will run it, copy it  and be satisfied. But teach a man how to read the fscking documentation and he will become a productive and useful memeber of the community. I think I deserve some sort of recognition :0)

Expert Comment

by:Igor Bazarny
ID: 6957582

Did you try to follow your suggestion? IMO, your design is incorrect, because Component used in CellRenderer never gets added to any container and never receives focus.

Igor Bazarny

Expert Comment

ID: 6957792
Let's give Dsys a few days to receive this notif and respond.  If nothing transpires, would a Point Split between you two make sense?
Thank you for your help here,
Moondancer - EE Moderator
LVL 15

Expert Comment

ID: 6957884
I was kidding actually.
Bzarny can have the points as far as I am concerned.

Expert Comment

ID: 6959978
Thank you both, finalized today by Moondancer - EE Moderator

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question