java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.awt.event arrow Call events on a Frame from a Panel
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4088
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Call events on a Frame from a Panel E-mail
User Rating: / 19
PoorBest 

A component on a Panel can easily call events on the parent Frame. This way, we can put all the logic in one place. In this example, a Frame contains 1 button and a Panel with 2 buttons on it. The first button on the Panel will generate an event for the button on the Frame while the second panel button will trigger a request to close the Frame and the application.

[TestEventPanel.java]

import java.awt.*;
import java.awt.event.*;

public class TestEventPanel extends Panel {
  Button b1,b2;
 
  TestEventPanel(){
    super();
    setLayout(new FlowLayout());
    setBackground(new Color(0).black);
    b1 = new Button("call event on the frame");
    add(b1);
    b2 = new Button("close the parent frame");
    add(b2);
    }
}

The Frame after adding the Panel, will act as an ActionListener for events for the 2 Panel buttons.

[TestEventFrame.java]
        
import java.awt.*;
import java.awt.event.*;

public class TestEventFrame extends Frame implements
        ActionListener, WindowListener  {
    TestEventPanel p1;
    Button b1;
    
    TestEventFrame(String title){
        super(title);
        setLayout(new FlowLayout());
        p1 = new TestEventPanel();
        
        b1 = new Button("A dummy button");
        add(b1);
        
        // the Panel with 2 buttons on it
        add(p1);
        createFrame();
        
        
        // add the actionlistener
        b1.addActionListener(this);
        p1.b1.addActionListener(this);
        p1.b2.addActionListener(this);
        addWindowListener(this);
    }
    
    void createFrame() {
        Dimension d = getToolkit().getScreenSize();
        setLocation(d.width/4,d.height/3);
        setSize(400,100);
        setVisible(true);
    }
    
    public void actionPerformed(ActionEvent ae){
        
        if (ae.getSource()==p1.b1) {
            System.out.println(ae.getActionCommand());
            ActionEvent new_ae =
                    new ActionEvent(b1,
                    ActionEvent.ACTION_PERFORMED,
                    "Panel b1 is calling the dummy button");
            b1.dispatchEvent(new_ae);
        }
        
        if (ae.getSource()==b1) {
            System.out.println("dummy receive :" + ae.getActionCommand());
        }
        
        if (ae.getSource()==p1.b2) {
            System.out.println(ae.getActionCommand());
            processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
        }
        
    }
    
    public void windowClosing(WindowEvent e) {
        this.dispose();
        System.exit(0);
    }
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
    public void windowClosed(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowOpened(WindowEvent e) { }
}

and finally:


[TestEventApplet.java]
        
import java.awt.*;

public class TestEventApplet extends java.applet.Applet {
    TestEventFrame myTestEventFrame;
    
    public void init() {
        myTestEventFrame =
                new TestEventFrame("TestEvent Frame");
    }
}

 Related Tips

 
Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.