File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes ENTER must activate the default button when pressed on a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ENTER must activate the default button when pressed on a JTable" Watch "ENTER must activate the default button when pressed on a JTable" New topic
Author

ENTER must activate the default button when pressed on a JTable

Joel Truc
Greenhorn

Joined: Jun 19, 2002
Posts: 7
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Joel Truc
Greenhorn

Joined: Jun 19, 2002
Posts: 7
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

Joined: Jan 07, 2002
Posts: 89
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.


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
Joel Truc
Greenhorn

Joined: Jun 19, 2002
Posts: 7
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

Joined: Oct 18, 2000
Posts: 4121

OK... if you are using "default button" as the default button of your JRootPane...



This simulates the user clicking the default button...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joel Truc
Greenhorn

Joined: Jun 19, 2002
Posts: 7
Woot !!
That an nice anwser. Exactly what i need thx a lot.
 
wood burning stoves
 
subject: ENTER must activate the default button when pressed on a JTable