• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ENTER must activate the default button when pressed on a JTable

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woot !!
That an nice anwser. Exactly what i need thx a lot.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic