Solved

Java JTable - Hold selected row.

Posted on 2007-03-26
5
534 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

685 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