🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #67

Re: Button related question

#67
From: 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.



  
Technical Details
Message-ID: <43a22b75@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!130.59.10.21.MISMATCH!kanaga.switch.ch!switch.ch!news.belwue.de!LF.net!newsgroups.borland.com!not-for-mail
References: <43a208cd@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,84ac8c3cf3b463d
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
Message-ID: <43a22b75@newsgroups.borland.com>
From: "Paul Nichols [TeamB]" <paul@comp.net>
Subject: Re: Button related question
Newsgroups: borland.public.jbuilder.java.swing
Date: Fri, 16 Dec 2005 21:08:57 -0600
References: <43a208cd@newsgroups.borland.com>
Lines: 60
User-Agent: KNode/0.9.2
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
NNTP-Posting-Host: 68.113.120.52
X-Trace: newsgroups.borland.com 1134701429 68.113.120.52 (15 Dec 2005 18:50:29 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!130.59.10.21.MISMATCH!kanaga.switch.ch!switch.ch!news.belwue.de!LF.net!newsgroups.borland.com!not-for-mail