MulticastEvent.java Source code

Java tutorial

Introduction

Here is the source code for MulticastEvent.java

Source

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 counter = 0;

    private JButton closeAllButton;

    public MulticastEvent() {
        JButton newButton = new JButton("New");
        add(newButton);
        newButton.addActionListener(this);

        closeAllButton = new JButton("Close all");
        add(closeAllButton);
    }

    public void actionPerformed(ActionEvent evt) {
        CloseFrame f = new CloseFrame();
        counter++;
        f.setTitle("Window " + counter);
        f.setSize(200, 150);
        f.setLocation(30 * counter, 30 * counter);
        f.show();
        closeAllButton.addActionListener(f);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("MulticastTest");
        frame.setSize(300, 200);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Container contentPane = frame.getContentPane();
        contentPane.add(new MulticastEvent());

        frame.show();
    }

    class CloseFrame extends JFrame implements ActionListener {
        public void actionPerformed(ActionEvent evt) { // handles Close all
            // button
            setVisible(false);
        }
    }
}