actions on JPanel objects  
Author Message
mwave3k





PostPosted: 2006-10-5 7:32:00 Top

java-programmer, actions on JPanel objects 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





PostPosted: 2006-10-5 18:34:00 Top

java-programmer >> actions on JPanel objects mwave3k wrote:
> 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





PostPosted: 2006-10-5 18:44:00 Top

java-programmer >> actions on JPanel objects Ian Wilson wrote:
> mwave3k wrote:
>
>> 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());
> }
> }
>

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.