• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ENTER must activate the default button when pressed on a JTable

 
Joel Truc
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here my problem:
- I have a JTable
- I have a KeyListener listening this Jtable.
- When i press Enter inside the JTable, i want it activate the default button of the JFrame.
- i know i must use a consume to forbid JTable to change selected row, but how i can activate the default button ? Or simulate a Enter strike in the JFrame?
Thx
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of actually making the enter key "PRESS" the button, why don't you create a method that is used for both scenerios.
For your Button
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
callSomeMethod();
}});
And then for your JTable when you press the ENTER key - callSomeMethod();
That way you don't have to worry about coding the firing event for the Button seperately.
Hope that helps.
 
Joel Truc
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx about the reply, but in fact, i am programming an application server, and in this part of the code, i really don t know what the default button do. So i can t make a methode that do like the button.
Is it not possible to generate a key event or a click on default button, or relaying an event to the mother component (I mean to relay the ENTER PRESSED event to the JFrame of the JTable)???

Thx again.
[ June 24, 2002: Message edited by: Joel Truc ]
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you can do, then, is create a generic interface...something like:
public interface MyInterface {
public void execute();
}
Have the ActionListener that responds to the default button press event also implement this interface. Then pass this listener object (as type MyInterface) to your KeyListener and call the execute() method when 'ENTER' is pressed. In this way, you can always respond correctly to the 'ENTER' key press and you don't care what the default button is. If I am not mistaken, this is something similar to the Command design pattern.
Hope this helps.
 
Joel Truc
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for this reply
It s damn hard to explain, but in this program i can not know what to do on ENTER, i only know i must action the default button, but i dunno what the default button do, or what the default button is.
This is all my problem
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK... if you are using "default button" as the default button of your JRootPane...



This simulates the user clicking the default button...
 
Joel Truc
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woot !!
That an nice anwser. Exactly what i need thx a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic