This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

doing same job on two or more events  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JButton print=new JButton("Print");

print.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("hello");
//other codes
}
});

print.addKeyListener(new KeyAdapter()
{
public void keyPressed(keyEvent e)
{
if(e.getKeyCode()==10) // trap enter key press
{
// i want here to do the same job of button clicking, which is defined in actionPerformed() method
}
}
});

i have created a JButton object and add action listener to it, defined actionPerformed() method, now i want that the same job will also be done when some one presses enter key on the JButton without writing the code again. How can i do this???

i want to know is there any way or any method which can automatically generate button click event?
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While posting code, please UseCodeTags

You do not need the key listener. The action performed is triggered if the user clicks on the button or hits Enter when it has focus
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question would be more suitable on the UI forums. Moving thread.
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:The action performed is triggered if the user clicks on the button or hits Enter when it has focus


Actually not. Space yes, Enter no.

I'd use an InputMap / ActionMap pair:
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another alternative is to set the button as the default button

frame.getRootPane().setDefaultButton(print);

so whenever you press enter (except for when components that use enter have focus e.g. textArea) the actionListener fires
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for your kind support.......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!