Home » Desktop Java » awt » event » Multicast event example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Multicast event example

In this tutorial we are going to see how to manage multicast events. Multicast events are quite useful when you have to work with many windows in your application and you just want to perform the same action or a group of actions in a number of windows in your application at the same time.

For example, if the user have opened many windows in the application, you might want to provide a command that closes all the windows at once.

In order to work with multicast events you have to:

  • Create a class that extends JPanel and implements ActionListener. This class should have a JButton component as a private member. This will be the button that will give the command to all the windows.
  • Override the actionPerformed method of this class to bundle a second button that performs a specific action. In our case the creation of a new window.
  • The new windows that will be launched will also implement ActionListener.
  • We will register the new ActionListeners to the button that gives the command to all the windows. So, now when this button is pressed all ActionListeners that are resistered to it will be launched and their actionPerformed method will be executed

Let’s take a look at the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MulticastEvent extends JPanel implements ActionListener {

    private int cnt = 0;
    private JButton closeAllButton;

    public MulticastEvent() {

  JButton jButton = new JButton("New");

  add(jButton);

  jButton.addActionListener(this);

  closeAllButton = new JButton("Close all");

  add(closeAllButton);
    }

    @Override
    public void actionPerformed(ActionEvent event) {

  CloseFrame closeFrame = new CloseFrame();

  cnt++;

  closeFrame.setTitle("Window " + cnt);

  closeFrame.setSize(200, 150);

  closeFrame.setLocation(30 * cnt, 30 * cnt);

  closeFrame.setVisible(true);

  closeAllButton.addActionListener(closeFrame);
    }

    private static void showUI() {

  JFrame jFrame = new JFrame();

  jFrame.setTitle("Multicast");

  jFrame.setSize(700, 500);

  jFrame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent event) {

    System.exit(0);

}

  });

  Container cPane = jFrame.getContentPane();

  cPane.add(new MulticastEvent());

  jFrame.setVisible(true);
    }

    class CloseFrame extends JFrame implements ActionListener {

  @Override

  public void actionPerformed(ActionEvent evt) {

setVisible(false);

  }
    }

    public static void main(String[] args) {

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

    showUI();

}

  });
    }
}

 
This was an example on how to work with multicast events in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of