Disabling selection of next line in a JTable: is this possible?

Posted on 2008-11-10
Last Modified: 2013-12-29
My application has a JTable with a custom editor defined on one of the columns.  What I'd like to do is to disable the JTable from selecting the next line when the user hits the Return key.  I know that pressing the Return key commits the change to the data being displayed.  I want to prevent the JTable from changing the selection to the next line after committing the change.  Is this possible?  If so, how do I do it? If you can, please either include a code snippet in your reply or point me to where I can see some code that does this.  Thanks in advance.
Question by:mwalker
    LVL 6

    Accepted Solution

    What I do is this:
    I  make a KeyListener and put an if:

    if (myTable.isFocusOwner() && e.getKeyCode() == 10)

    the code 10 is for the return key. Next, you get the selected row(it will be the next you selected) and create an object to select your own row. The last step is select the interval (rows). As the JTable selects the next row when you press enter key, you only need to select the previous row and it appears that nothing happened. (Actually the JTable has selected again the row  where user hits return key but you don't notice this)
    public void keyPressed(KeyEvent e) {
        if (myTable.isFocusOwner() && e.getKeyCode() == 10) {
            int selectedRow = myTable.getSelectedRow();
            ListSelectionModel selectionModel = myTable.getSelectionModel();
            selectionModel.setSelectionInterval(selectedRow - 1, selectedRow - 1);

    Open in new window


    Author Closing Comment

    That worked for me. Thanks.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    dividesSelf challange 15 60
    Logs to delete in Windows VCenter server ? 11 94
    maven java path setting 5 33
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    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…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now