?
Solved

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

Posted on 2008-11-10
2
Medium Priority
?
869 Views
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.
0
Comment
Question by:mwalker
2 Comments
 
LVL 6

Accepted Solution

by:
javaexperto earned 600 total points
ID: 22926875
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

0
 

Author Closing Comment

by:mwalker
ID: 31515222
That worked for me. Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 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