This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.......
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!