🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
8 messages
8 total messages Started by GEORGE Fri, 16 Dec 2005 00:23
Button related question
#65
Author: GEORGE
Date: Fri, 16 Dec 2005 00:23
7 lines
1093 bytes
I would like that when the user presses the Enter key in a dialog, the 
actionPerformed() of a designated button should be executed.  Is there a 
button property for this purpose?  How to achieve this effect?

Thanks

George 
Re: Button related question
#66
Author: GEORGE
Date: Fri, 16 Dec 2005 03:03
77 lines
3399 bytes
Paul,

Thanks for replying.  I'm sorry to have made you type so much but my problem
is much simpler than what you thought.

I have a dialog with bunch of controls one of which is the OK button.  What
I want is that should the user press Enter when this dialog has been
activated,  then the actionPerformed of the OK button should be executed
(even if the cursor is currently in , say, a text field).   Hope this makes
sense.

George

"Paul Nichols [TeamB]" <paul@comp.net> wrote in message 
news:43a22b75@newsgroups.borland.com...
> GEORGE wrote:
>
>> I would like that when the user presses the Enter key in a dialog, the
>> actionPerformed() of a designated button should be executed.  Is there a
>> button property for this purpose?  How to achieve this effect?
>>
>> Thanks
>>
>> George
>
> I am not exactly sure what you are asking, but I will take a stab at 
> making
> an assumption.
>
>
> You have a Dialog and you want to activate the Event listener on another
> JFrame or JPanel. The solution is to get the instance of that parent
> container.
>
> The easiest way to do this, is to have a static instance of the container
> you wish to access. Example:
>
> public class MyJPanel extends JPanel {
>
>  static MyJPanel panel;
>
>
>  public MyJPanel() {
>   super();
>
>  }
>
>  public static MyJPanel getMyJPanel(){
>    if(panel==null){
>      panel= new MyJPanel();
>    }
>   return panel;
>  }
>
>
>
>  //Now call the getMyJPanel from the Dialog
>
>  public void jDialogButton1_actionPerformed(ActionEvent actionEvent) {
>     MyJPanel panel=MyJPanel.getMyJPanel();
>         panel.jButton2_actionPerformed(null);
>  }
>
>  Of course another and a much better way to do thisand avoid dependencies
> upon your view Components, is to create Listener class(es) that will
> implement listeners according to usage in the application. Those listeners
> that would be commonly used in the application or which need to be able to
> access methods across Frames, Dialogs, etc. could have static references 
> or
> be contained in a static class. This enforces better separation between
> classes. Since I do not know your programming background, I do not know
> what to reference as a a concept here. If you have database programming
> background, think of a linking table concept.
>
>
>
> 
Re: Button related question
#68
Author: Alexey N. Solofnenko
Date: Fri, 16 Dec 2005 04:16
88 lines
3703 bytes
I think you are looking for this: 
dialog.getRootPane().setDefaultButton(submitButton);

- Alexey.

GEORGE wrote:
> Paul,
> 
> Thanks for replying.  I'm sorry to have made you type so much but my problem
> is much simpler than what you thought.
> 
> I have a dialog with bunch of controls one of which is the OK button.  What
> I want is that should the user press Enter when this dialog has been
> activated,  then the actionPerformed of the OK button should be executed
> (even if the cursor is currently in , say, a text field).   Hope this makes
> sense.
> 
> George
> 
> "Paul Nichols [TeamB]" <paul@comp.net> wrote in message 
> news:43a22b75@newsgroups.borland.com...
>> GEORGE wrote:
>>
>>> I would like that when the user presses the Enter key in a dialog, the
>>> actionPerformed() of a designated button should be executed.  Is there a
>>> button property for this purpose?  How to achieve this effect?
>>>
>>> Thanks
>>>
>>> George
>> I am not exactly sure what you are asking, but I will take a stab at 
>> making
>> an assumption.
>>
>>
>> You have a Dialog and you want to activate the Event listener on another
>> JFrame or JPanel. The solution is to get the instance of that parent
>> container.
>>
>> The easiest way to do this, is to have a static instance of the container
>> you wish to access. Example:
>>
>> public class MyJPanel extends JPanel {
>>
>>  static MyJPanel panel;
>>
>>
>>  public MyJPanel() {
>>   super();
>>
>>  }
>>
>>  public static MyJPanel getMyJPanel(){
>>    if(panel==null){
>>      panel= new MyJPanel();
>>    }
>>   return panel;
>>  }
>>
>>
>>
>>  //Now call the getMyJPanel from the Dialog
>>
>>  public void jDialogButton1_actionPerformed(ActionEvent actionEvent) {
>>     MyJPanel panel=MyJPanel.getMyJPanel();
>>         panel.jButton2_actionPerformed(null);
>>  }
>>
>>  Of course another and a much better way to do thisand avoid dependencies
>> upon your view Components, is to create Listener class(es) that will
>> implement listeners according to usage in the application. Those listeners
>> that would be commonly used in the application or which need to be able to
>> access methods across Frames, Dialogs, etc. could have static references 
>> or
>> be contained in a static class. This enforces better separation between
>> classes. Since I do not know your programming background, I do not know
>> what to reference as a a concept here. If you have database programming
>> background, think of a linking table concept.
>>
>>
>>
>>
> 
> 

-- 
Alexey N. Solofnenko
home: http://trelony.cjb.net/
Re: Button related question
#71
Author: GEORGE
Date: Fri, 16 Dec 2005 13:36
129 lines
5022 bytes
Paul & Alex,

Thanks a lot!  I had forgotten all about root panes.  Haven't been there for 
the longest time :-)


"Paul Nichols [TeamB]" <paul@comp.net> wrote in message 
news:43a24386@newsgroups.borland.com...
> GEORGE wrote:
>
>> Paul,
>>
>> Thanks for replying.  I'm sorry to have made you type so much but my
>> problem is much simpler than what you thought.
>>
>> I have a dialog with bunch of controls one of which is the OK button.
>> What I want is that should the user press Enter when this dialog has been
>> activated,  then the actionPerformed of the OK button should be executed
>> (even if the cursor is currently in , say, a text field).   Hope this
>> makes sense.
>>
>> George
>>
>> "Paul Nichols [TeamB]" <paul@comp.net> wrote in message
>> news:43a22b75@newsgroups.borland.com...
>>> GEORGE wrote:
>>>
>>>> I would like that when the user presses the Enter key in a dialog, the
>>>> actionPerformed() of a designated button should be executed.  Is there 
>>>> a
>>>> button property for this purpose?  How to achieve this effect?
>>>>
>>>> Thanks
>>>>
>>>> George
>>>
>>> I am not exactly sure what you are asking, but I will take a stab at
>>> making
>>> an assumption.
>>>
>>>
>>> You have a Dialog and you want to activate the Event listener on another
>>> JFrame or JPanel. The solution is to get the instance of that parent
>>> container.
>>>
>>> The easiest way to do this, is to have a static instance of the 
>>> container
>>> you wish to access. Example:
>>>
>>> public class MyJPanel extends JPanel {
>>>
>>>  static MyJPanel panel;
>>>
>>>
>>>  public MyJPanel() {
>>>   super();
>>>
>>>  }
>>>
>>>  public static MyJPanel getMyJPanel(){
>>>    if(panel==null){
>>>      panel= new MyJPanel();
>>>    }
>>>   return panel;
>>>  }
>>>
>>>
>>>
>>>  //Now call the getMyJPanel from the Dialog
>>>
>>>  public void jDialogButton1_actionPerformed(ActionEvent actionEvent) {
>>>     MyJPanel panel=MyJPanel.getMyJPanel();
>>>         panel.jButton2_actionPerformed(null);
>>>  }
>>>
>>>  Of course another and a much better way to do thisand avoid 
>>> dependencies
>>> upon your view Components, is to create Listener class(es) that will
>>> implement listeners according to usage in the application. Those
>>> listeners that would be commonly used in the application or which need 
>>> to
>>> be able to access methods across Frames, Dialogs, etc. could have static
>>> references or
>>> be contained in a static class. This enforces better separation between
>>> classes. Since I do not know your programming background, I do not know
>>> what to reference as a a concept here. If you have database programming
>>> background, think of a linkiOK,ng table concept.
>>>
>>>
>>>
>>>
> Whew,
>
> You do not want much, do you :)
>
>
> OK, here is how I made it work.
>
> First, you will need to define an AbstractEvent (I am doing this in the
> jbInit() method)
>
>
>
> public void jbInit() throws Exception {
>  //All of the normal initialization code first. Dots below represent code.
>  //DO NOT TYPE THE DOTS!!
>  ..............
>
>   //Put this code in your code.. Of course change the names to your
>   // component names
> //replace with your JDialog name
>     final SimpleDialog sd=(SimpleDialog)this;
> //Create a new AbstractAction javax.swing.AbstractAction
>     javax.swing.Action anAction = new javax.swing.AbstractAction() {
>          public void actionPerformed(ActionEvent e) {
>           //assuming jButton1 is your name and the actionPerformedEvent
>            sd.jButton1_actionPerformed(null);
>          }
>         };
>          javax.swing.JComponent jc=(javax.swing.JComponent)
>              sd.getComponent(0);
>          jc.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
>            put(javax.swing.KeyStroke.getKeyStroke("ENTER"),"myAction");
>          jc.getActionMap().put("myAction",anAction);
>    }
>  }
>
> That should do it for you..
> 
Re: Button related question
#72
Author: Lori M Olson [TeamB]
Date: Fri, 16 Dec 2005 16:37
32 lines
1962 bytes
GEORGE wrote:
> Paul & Alex,
> 
> Thanks a lot!  I had forgotten all about root panes.  Haven't been there for 
> the longest time :-)
> 
> 

There is one little "feature" that you should be aware of.  If one of 
the controls in your page is a "TextArea", then it will be capturing the 
"Enter" key when it has focus.  That's really just a "gotcha" that you 
can't really do anything about, unless you want to break the 
functionality of the TextArea.

-- 

Regards,

Lori Olson [TeamB]

------------

Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.

Google Advanced Newsgroup Search
     http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
     http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
     http://www.visi.com/~gyles19/fom-serve/cache/1.html
Re: Button related question
#67
Author: Paul Nichols [TeamB]
Date: Sat, 17 Dec 2005 03:08
60 lines
2626 bytes
GEORGE wrote:

> I would like that when the user presses the Enter key in a dialog, the
> actionPerformed() of a designated button should be executed.  Is there a
> button property for this purpose?  How to achieve this effect?
> 
> Thanks
> 
> George

I am not exactly sure what you are asking, but I will take a stab at making
an assumption.


You have a Dialog and you want to activate the Event listener on another
JFrame or JPanel. The solution is to get the instance of that parent
container.

The easiest way to do this, is to have a static instance of the container
you wish to access. Example:

public class MyJPanel extends JPanel {

  static MyJPanel panel;


  public MyJPanel() {
   super();
   
  }

  public static MyJPanel getMyJPanel(){
    if(panel==null){
      panel= new MyJPanel();
    }
   return panel;
  }



  //Now call the getMyJPanel from the Dialog
   
  public void jDialogButton1_actionPerformed(ActionEvent actionEvent) {
     MyJPanel panel=MyJPanel.getMyJPanel();
         panel.jButton2_actionPerformed(null);
  }

  Of course another and a much better way to do thisand avoid dependencies
upon your view Components, is to create Listener class(es) that will
implement listeners according to usage in the application. Those listeners
that would be commonly used in the application or which need to be able to
access methods across Frames, Dialogs, etc. could have static references or
be contained in a static class. This enforces better separation between
classes. Since I do not know your programming background, I do not know
what to reference as a a concept here. If you have database programming
background, think of a linking table concept.



  
Re: Button related question
#69
Author: Paul Nichols [TeamB]
Date: Sat, 17 Dec 2005 04:51
116 lines
4630 bytes
GEORGE wrote:

> Paul,
> 
> Thanks for replying.  I'm sorry to have made you type so much but my
> problem is much simpler than what you thought.
> 
> I have a dialog with bunch of controls one of which is the OK button. 
> What I want is that should the user press Enter when this dialog has been
> activated,  then the actionPerformed of the OK button should be executed
> (even if the cursor is currently in , say, a text field).   Hope this
> makes sense.
> 
> George
> 
> "Paul Nichols [TeamB]" <paul@comp.net> wrote in message
> news:43a22b75@newsgroups.borland.com...
>> GEORGE wrote:
>>
>>> I would like that when the user presses the Enter key in a dialog, the
>>> actionPerformed() of a designated button should be executed.  Is there a
>>> button property for this purpose?  How to achieve this effect?
>>>
>>> Thanks
>>>
>>> George
>>
>> I am not exactly sure what you are asking, but I will take a stab at
>> making
>> an assumption.
>>
>>
>> You have a Dialog and you want to activate the Event listener on another
>> JFrame or JPanel. The solution is to get the instance of that parent
>> container.
>>
>> The easiest way to do this, is to have a static instance of the container
>> you wish to access. Example:
>>
>> public class MyJPanel extends JPanel {
>>
>>  static MyJPanel panel;
>>
>>
>>  public MyJPanel() {
>>   super();
>>
>>  }
>>
>>  public static MyJPanel getMyJPanel(){
>>    if(panel==null){
>>      panel= new MyJPanel();
>>    }
>>   return panel;
>>  }
>>
>>
>>
>>  //Now call the getMyJPanel from the Dialog
>>
>>  public void jDialogButton1_actionPerformed(ActionEvent actionEvent) {
>>     MyJPanel panel=MyJPanel.getMyJPanel();
>>         panel.jButton2_actionPerformed(null);
>>  }
>>
>>  Of course another and a much better way to do thisand avoid dependencies
>> upon your view Components, is to create Listener class(es) that will
>> implement listeners according to usage in the application. Those
>> listeners that would be commonly used in the application or which need to
>> be able to access methods across Frames, Dialogs, etc. could have static
>> references or
>> be contained in a static class. This enforces better separation between
>> classes. Since I do not know your programming background, I do not know
>> what to reference as a a concept here. If you have database programming
>> background, think of a linkiOK,ng table concept.
>>
>>
>>
>>
Whew,

You do not want much, do you :)


OK, here is how I made it work.

First, you will need to define an AbstractEvent (I am doing this in the
jbInit() method)



public void jbInit() throws Exception {
  //All of the normal initialization code first. Dots below represent code.  
  //DO NOT TYPE THE DOTS!!
  ..............

   //Put this code in your code.. Of course change the names to your
   // component names
//replace with your JDialog name
     final SimpleDialog sd=(SimpleDialog)this;
 //Create a new AbstractAction javax.swing.AbstractAction
     javax.swing.Action anAction = new javax.swing.AbstractAction() {
          public void actionPerformed(ActionEvent e) {
           //assuming jButton1 is your name and the actionPerformedEvent
            sd.jButton1_actionPerformed(null);
          }
         };
          javax.swing.JComponent jc=(javax.swing.JComponent)
              sd.getComponent(0);
          jc.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
            put(javax.swing.KeyStroke.getKeyStroke("ENTER"),"myAction");
          jc.getActionMap().put("myAction",anAction);
    }
  }  

That should do it for you..
Re: Button related question
#70
Author: Paul Nichols [TeamB]
Date: Sat, 17 Dec 2005 04:57
19 lines
1641 bytes
Alexey N. Solofnenko wrote:

> I think you are looking for this:
> dialog.getRootPane().setDefaultButton(submitButton);
> 
> - Alexey.
> 
> GEORGE wrote:
>> Paul,
>> 

>> I have a dialog with bunch of controls one of which is the OK button. 
>> What I want is that should the user press Enter when this dialog has been
>> activated,  then the actionPerformed of the OK button should be executed
>> (even if the cursor is currently in , say, a text field).   Hope this
>> makes sense.
 
This is definitely the easiest way for a single key event for a single
button and a single keystoke.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads