Solved

How to detect a cell lost focus in a JTable?

Posted on 2002-04-10
11
1,115 Views
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.

Thanks,
Dsys.
0
Comment
Question by:Dsys
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 15

Expert Comment

by:ozymandias
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.

0
 

Author Comment

by:Dsys
ID: 6932090

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

 Do you have a code example?
0
 
LVL 15

Expert Comment

by:ozymandias
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.

0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 200 total points
ID: 6933381
Hi,

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

Regards,
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");
        frame.getContentPane().add(table);
        ListSelectionModel rowSelection = table.getSelectionModel();
        ListSelectionModel colSelection = table.getColumnModel().getSelectionModel();
        ListSelectionListener lst = new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event){
                printSelection(table);
            }
        };
        rowSelection.addListSelectionListener(lst);
        colSelection.addListSelectionListener(lst);
        frame.pack();
        frame.setVisible(true);
    }

    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);
    }
}
0
 
LVL 1

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
http://www.experts-exchange.com/questions/Q.20068272.html
http://www.experts-exchange.com/questions/Q.20068448.html
http://www.experts-exchange.com/questions/Q.20073560.html
http://www.experts-exchange.com/questions/Q.20076023.html
http://www.experts-exchange.com/questions/Q.20129982.html
http://www.experts-exchange.com/questions/Q.20215566.html
http://www.experts-exchange.com/questions/Q.20287052.html
http://www.experts-exchange.com/questions/Q.20289705.html




*****  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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
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.
 
Moondancer
Moderator @ Experts Exchange
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6957467
Moderators,

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.

Igor
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6957552
Moderators,

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)
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6957582
ozymandias,

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.

Regards,
Igor Bazarny
0
 
LVL 1

Expert Comment

by:Moondancer
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
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6957884
I was kidding actually.
Bzarny can have the points as far as I am concerned.
0
 
LVL 1

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 39
Java SE 8u111  Lot of stuff broke 11 54
servlet  URL Rewriting 1 27
couple of eclipse 5 25
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

920 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

15 Experts available now in Live!

Get 1:1 Help Now