Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Again about default button

Thread view: 
ffellico@inwind.it - 29 Oct 2005 07:06 GMT
Yes. Yet I am unable to do it!

I read many of us asking for this need, but no one of the answers seem
to solve the problem.

As a sample I have a frame with only a jTextField and two jButtons
(jButton1 and jButton2).

I would like to fire automatically the jButton2 hitting ENTER when I am
writing in the jTextField.

Can anyone write for me the code to do this?

Thank you. Franco.
zero - 29 Oct 2005 12:32 GMT
ffellico@inwind.it wrote in news:1130566018.989557.54330
@g44g2000cwa.googlegroups.com:

> Yes. Yet I am unable to do it!
>
[quoted text clipped - 10 lines]
>
> Thank you. Franco.

Just add the same ActionListener to both.  That way the same will happen
when you hit enter on the textfield or press the button.  Use a different
listener (or a way to distinguish the source, eg event.getSource()) for the
second button.

ActionListener myActionListener = new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
     // ...
  }
};

textField.addActionListener(myActionListener);
button.addActionListener(myActionListener);
button2.addActionListener(new ActionListener
{
  // ...
});
ffellico@inwind.it - 29 Oct 2005 19:33 GMT
Hi.

You solved me also this problem ( before you suggest me the solution
for the statusbar at the bottom of a frame).

This is a new step for me in learning and using Java.  Thank you.
Franco from Italy.
Babu Kalakrishnan - 30 Oct 2005 13:57 GMT
> You solved me also this problem ( before you suggest me the solution
> for the statusbar at the bottom of a frame).
>
> This is a new step for me in learning and using Java.  Thank you.
> Franco from Italy.

A more generic solution for this is to use the "defaultButton" property
of the rootPane on the Window/Frame/Dialog. See the API docs for the
setDefaultButton method in the class javax.swing.JRootPane.

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.