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
  • 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.


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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.


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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now