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 2006

Tip: Looking for answers? Try searching our database.

actions on JPanel objects

Thread view: 
mwave3k - 05 Oct 2006 00:32 GMT
I have a main project GUI with tabs on it.  these tabs have JPanels
with various JComponents on them.  The JPanels are all made in seperate
classes that extend JPanel.  the point of that is to make my code a
little cleaner.  All the components in JPanel are private.  However, I
don't know how to add an ActionListener to buttons and other components
on the JPanel and recieve these actions in the main project rather then
the JPanel class.  Help please?  i would appreciate it
Ian Wilson - 05 Oct 2006 11:33 GMT
> I have a main project GUI with tabs on it.  these tabs have JPanels
> with various JComponents on them.  The JPanels are all made in seperate
> classes that extend JPanel.  the point of that is to make my code a
> little cleaner.  All the components in JPanel are private.  

Seems familar.

> However, I
> don't know how to add an ActionListener to buttons and other components
> on the JPanel and recieve these actions in the main project rather then
> the JPanel class.  

Me neither, so what I do instead is, have each panel implement
ActionListener and pass the panel a reference to the JFrame in it's
constructor, then the actionListener can invoke a method in the JFrame
to handle the event.

e.g. something not entirely unlike this

JPanel Foo extends JPanel implements ActionListener {
   JFrame parent;
   Foo(JFrame parent)  {
      this.parent = parent;
      ...
      fooButton.addActionListener(this);
      ...
   }
   public void actionPerformed(ActionEvent e) {
     parent.doCommand(e.getActionCommand());
   }
}

untested, caveat emptor.
Ian Wilson - 05 Oct 2006 11:43 GMT
>> I have a main project GUI with tabs on it.  these tabs have JPanels
>> with various JComponents on them.  The JPanels are all made in seperate
[quoted text clipped - 27 lines]
>    }
> }

It occurrs to me you could just do
  fooButton.addActionListener(parent);
and add some comment's in the parent's actionPerformed() to clarify that
it receives events from random JPanels.


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



©2008 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.