• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1477
  • Last Modified:

How to detect a cell lost focus in a JTable?

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
Dsys
Asked:
Dsys
  • 4
  • 3
  • 3
  • +1
1 Solution
 
ozymandiasCommented:
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
 
DsysAuthor Commented:

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

 Do you have a code example?
0
 
ozymandiasCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Igor BazarnyCommented:
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
 
MoondancerCommented:
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
 
Igor BazarnyCommented:
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
 
ozymandiasCommented:
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
 
Igor BazarnyCommented:
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
 
MoondancerCommented:
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
 
ozymandiasCommented:
I was kidding actually.
Bzarny can have the points as far as I am concerned.
0
 
MoondancerCommented:
Thank you both, finalized today by Moondancer - EE Moderator
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now