Solved

Java JTable - Hold selected row.

Posted on 2007-03-26
5
519 Views
Last Modified: 2010-08-05
Hi all,

I'm looking for help with a JTable issue in Java.

I have a read only table which displays rows of info with multiple columns.

When an operator selects a row in the table the selected row is highlighted.

I have written an event handler to say if the operator presses the enter/return button following selection of a row in my table, a secondary dialog is popped up.

All fine up to here........

I cannot find a way to disable the JTable from moving the currently selected row to the next row down in the table following the enter/return button being pressed on a selection in the table.

If anyone knows how to stop the currently selected row from shifting down it would be greatly appreciated.
0
Comment
Question by:Zuzic
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18797942
You mean the row itself is being dragged down?
0
 

Author Comment

by:Zuzic
ID: 18798133
No, the row sizes do not change shape nor size.

I'm seeing the highlighting of row that I mouse clicked on and then pressed enter on shift down to the next row in the table.

e.g.

Table contains 3 rows with first name and surname colums filled in with:

Joe Bloggs
Jill Smith
Paul Emersons

I select Joe Bloggs with the mouse and that row is highlighted in the table.

I now press the enter button on the keyboard and my program brings us another dialog related to Joe Bloggs.

Observing my table I now see Jill Smith being highlighted because I pressed the enter button on the keyboard.

I'd like to see Joe Bloggs still highlighted at this point.
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 250 total points
ID: 18798393
You can consume() an event. So you get sth like:

class MyKeyListener
    extends KeyAdapter
{
    public void keyPressed(KeyEvent ke)
    {
         // open dialog
        ke.consume();
    }
}

Mark
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18800314
Yes, you can check for the key being the Enter key before that:

jTable.addKeyListener ( new KeyAdapter () {
    public void keyPressed ( KeyEvent ke )
    {
      if ( key.getKeyCode () == KeyEvent.VK_ENTER )
      {
         // open dialog
         ke.consume () ;
      } // end if

    }

} ) ;
0
 

Author Comment

by:Zuzic
ID: 18804570
It was the consume() call which I was after here in order to stop any calls being carried out behind the scenes against the JTable when the ENTER button is pressed on it.

Thanks for all input.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
servlet filter example 37 59
web application structure 18 76
Facing this issue for maven proxy setting 2 9
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

786 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