Solved

Java JTable - Hold selected row.

Posted on 2007-03-26
5
497 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:mayankeagle
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:mayankeagle
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
sumHeights  challenge 17 60
ArrayLists in ArrayLists/HashMaps? 4 40
solarwind tftp server 2 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

14 Experts available now in Live!

Get 1:1 Help Now