Multiple sources: A listener can be plugged into several sources. : Event Queue « Event « Java






Multiple sources: A listener can be plugged into several sources.

  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    JButton o = (JButton) e.getSource();
    String label = o.getText();
    System.out.println(label + " button clicked");
  }
}

public class MultipleSources {
  public static void main(String[] args) {
    JPanel panel = new JPanel();

    JButton close = new JButton("Close");
    close.addActionListener(new ButtonListener());

    JButton open = new JButton("Open");
    open.addActionListener(new ButtonListener());

    JButton find = new JButton("Find");
    find.addActionListener(new ButtonListener());

    JButton save = new JButton("Save");
    save.addActionListener(new ButtonListener());

    panel.add(close);
    panel.add(open);
    panel.add(find);
    panel.add(save);
    JFrame f = new JFrame();
    f.add(panel);
    f.setSize(400, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

   
    
  








Related examples in the same category

1.Use the Event queue to retrieve eventUse the Event queue to retrieve event
2.Event object has information about an event, that has happened.
3.Register several listeners for one event.
4.ActionEvent.getActionCommand()
5.Adding an InputMap to a Component
6.Event source and listener
7.Using an inner ActionListener class.
8.void java.awt.Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask)
9.int java.awt.event.WindowEvent.WINDOW_OPENED
10.JComponent.WHEN_IN_FOCUSED_WINDOW
11.Register action
12.Using EventQueue.invokeLater to start a Swing application