• 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

How to set Default button in Swing

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am having one dialog in my application which is having few buttons. I want to set one button as default button so that when the dialog is active, hitting enter key should result in action triggerd for a specific button in the dialog even if the button doesnot have keyboard focus.
I tried with following code in my dialog:

this.getRootPane().setDefaultButton(buttonObj);

But i havent got what i expected. On hitting enter key, the button which has keyboard focus triggers event not the default button i set.

Can anyone help me to get my desired bahaviour?
Note: I am using default java look and feel.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the dialog is modal,
this.getRootPane().setDefaultButton(buttonObj);
must be before
setVisible(true);
 
Karthikeyan Chandrasekaran
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. But this is what i have already done. It works well when the dialog gets displayed. In the dialog, i will click some buttons or i will type something in a textbox available. Now the keyboard focus will be on some componenet other than default button. In this situation if i hit enter key, the button which is having keyboard focus gets triggered. But i want my desired button to be triggered eventhough keyboard focus is available with some other button.

Can you give me a solution to my requirement?
[ June 01, 2006: Message edited by: Karthikeyan Chandrasekaran ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Can you give me a solution to my requirement?

you will have to post some code that demonstrates this non-standard behaviour
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic