Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: AWT
Expertise: Beginner
May 2, 1999



Application Security Testing: An Integral Part of DevOps

Dispatching Mouse Events

How can I pass or send a mousedown to a choice menu when a button is pressed?

Sending an event to an AWT component is as simple as creating an event and dispatching it with dispatchEvent(), which is defined in java.awt.Component. However, you will find limited success using this technique with java.awt components, because user-dispatched events do not always have the desired effect on these native peer-based components. Unless you are building applets, I recommend that you stop using java.awt components and switch over completely to javax.swing components. They are more flexible and consistent in their range of allowable behavior. The following example demonstrates how to pop up a choice menu when a button is pressed using Swing components. There is no need to dispatch an event because JComboBox provides the convenient showPopup() method.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 * This program demonstrates how to make a choice menu pop up when a
 * button is clicked.  It uses Swing components because peer-based AWT
 * components will not reliably respond to user-dispatched events.
public class EventExample extends JFrame {
  private JButton __button;
  private JComboBox __choice;

  public EventExample() {
    __button = new JButton("Activate");
    __choice = new JComboBox();

    __button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

    getContentPane().add(__choice, BorderLayout.CENTER);
    getContentPane().add(__button, BorderLayout.SOUTH);

  public static void main(String[] args) {
    Frame frame;
    WindowListener exitListener;

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        Window window = e.getWindow();

    frame = new EventExample();


DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date