*
The moose likes Swing / AWT / SWT and the fly likes Need help with event listener -> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with event listener ->" Watch "Need help with event listener ->" New topic
Author

Need help with event listener ->

Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hello all Swing/AWT freaks out there!
The problem I am trying to solve in the middle of the night (it's 2.15 am here in Sweden), is that I have a JButton in a JFrame. When this button is pressed an Event is raised which I have delegated to a sperate event listener class. When this particular event arises I want the current frame to hide (setVisible(false)) and I want another frame to appear instead(setVisible(true)).
This seems to be a problem, as I haven't got any reference variable in the event listener to the current instances of the JFrames.
This question might be really easy to solve, but as I am tired I hope you will be able to understand my problem and help me.
Regards
------------------
Dominic Steng�rd
Sun Certified Java 2 Programmer
------------------


Dominic Steng�rd<br />Sun Certified Java 2 Programmer
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi Dominic,
if u want to hide or show a particular frame then you have to have a reference to it somehow.
do u have some specific constraint/requirement?if possible do post your code.that may help us understand your problem.
regards
deeksha
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Ok, below you will find the code for my ActionListener class. The only requirement I have is that I somehow need to be able to show/hide certain JFrames from within this class.
// Listener Class ------>
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JButton source = (JButton)evt.getSource();
String s = source.getActionCommand();
if(s.equals("0")) {
;
}
else if(s.equals("2")) {
source.setVisible(false); //The source here gives me a reference to the button
//This is where I would need to be able to show/hide frames from which the Action event was raised
}
}
}
------------------
Dominic Steng�rd
Sun Certified Java 2 Programmer
------------------
Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
I would create a constructor for the event listener that takes a reference to the "owner" frame, like:
MyListener(JFrame owner)
Then it saves the owner reference to a private member, that can be used later in actionPerformed. To do this the JButton that contains the event listener may need to know its owner frame as well, so just use the same process.
Hope that helps.
Daniel


- Daniel
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Thanks Daniel for your help! )
I cannot believe I didn't think of that!! But every now and then one forgets about most the simple and good solutions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with event listener ->
 
Similar Threads
Is there another way to implement event listener (in general)?
event sequence
how can i open new window browser or Internet Explorer from java Applet
Regarding status bar resizing
Closing a frame from another frame