wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Event Handleing Help ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handleing Help ..." Watch "Event Handleing Help ..." New topic
Author

Event Handleing Help ...

Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
I am have a class that extends JPanel with a CloseButton on it. I want to be able to handle the Close event in the Panel Class itself and in the container that uses it.
The problem seems to be that the event is being handled in the containing class before it is being handled in the Panel. How can I handle my Panel class before it reaches the containing class. Also can I somehow catch the event in the panel class if I need to and not pass it to the containing class?

Below are some code snipits of the panel and containing classes.

ClassWithCloseButton ...
class MyEventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == getCloseButton()){
doMyMethod();
}
};
}
public void removeCloseButtonActionListener(ActionListener listener){
getCloseButton().removeActionListener(listener);
}
public void addCloseButtonActionListener(ActionListener listener){
getCloseButton().addActionListener(listener);
}
ClassThatUsesOtherClass ....
class MyEventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == getMyClass().getCloseButton()){
doMyMethod();
}
};
}
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
OK, your code didn't help me that much, so maybe I'm a bit dense today. Here is what I do in situations like this.
You are already subclassing the JPanel anyways, so why not use it as the ActionListener as well. That keeps things simple. Just implenet ActionListener in its declaration. Then as soon as you create the button, and add it to the JPanel, also addActionListener(this/jpanel) depends on where you are calling the add from.
This should give your panel first/only rights to actions from this button unless you add another action listener to it, which you won't cause it would confuse things. Make sure to include the public void actionPerformed(ActionEvent e) in your custom JPanel. implement whatever your button should do inside that method.
It looks like you are trying to do something with adding and removing closebuttonlisteners. Don't. You don't need to since it is already done for you in the ActionListener setup.
That is all you should need with the exception of a JFrame somewhere to put your custom JPanel in. No other custom classes are needed.
HTH
Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
Maybe I can be more clear as to what I am attempting.
I have a Class that extends JPanel with a Close button on it, I use this class in a second Class that extends JFrame. I want to handle the close action event in both classes, first the panel, second the frame.
i.e. When the user click on the close button, I close any connections contained in the Panel class and then after they have been closed pass the event along to the Frame so I can remove the Panel from the tabbed pane in the frame.
I need to be bale to listen and handle the same event in 2 classes, can I do this?
Robert
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
ooooh, well, remember when I said up there not to add another action listener because it would just confuse things? Well now all you have to do is just add your custom JFrame as an actionlistener as well. You'll have to implement that in your JFrame, but it should be easy.
One question tho, why make 2 custom classes (JFrame and JPanel) when you could probably get away with 1 custom JFrame and use the standard JPanel? Just implement all your special connections and so forth in your JFrame and save yourself some work. Plus then you are back to a single ActionListener that knows about everything and can respond to the button click in every way needed and in the proper order.
HTH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event Handleing Help ...
 
Similar Threads
Add ActionListener to JButton
Calc with Events - Help !!
Key Events firing for non displayed controls
JComboBox oddity
addActionListener problem