Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > actions on JPanel objects

Reply
Thread Tools

actions on JPanel objects

 
 
mwave3k
Guest
Posts: n/a
 
      10-04-2006
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

 
Reply With Quote
 
 
 
 
Ian Wilson
Guest
Posts: n/a
 
      10-05-2006
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.




 
Reply With Quote
 
 
 
 
Ian Wilson
Guest
Posts: n/a
 
      10-05-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPanel inside JPanel inside JFrame t1m1976 Java 1 11-07-2010 09:47 PM
[JPanel] Drop Down JPanel Raphael Jolivet Java 1 07-18-2008 12:30 AM
Need to add onMouseOver actions to select menu created with 'newoption' objects speedbird Javascript 0 03-24-2008 06:33 PM
Add a JPanel to a JPanel... Andreas Beresko Java 6 07-16-2004 12:10 AM
Resizing a jpanel and the objects on it???????? steve smith Java 0 07-10-2003 06:22 PM



Advertisments