event
Multilistener example
In this tutorial we shall show you how to use an ActionListener
in order to create a Multilistener
component in a Java Desktop application. You may find this useful when you want some of your components to behave in the same way under certain circumstances.
In short, all you have to do in order to create a MultiListener
component is:
- Create a new
ActionListener
instance. - Override the
actionPerformed
method to customize the handling of an event. - Use the
addActionListener
method to add the aboveActionListener
to a number of components you to monitor.
Let’s see the code snippet that follows:
package com.javacodegeeks.snippets.desktop; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main extends JPanel implements ActionListener { JButton b1, b2; JTextArea topText; JTextArea bottomText; final static String nl = "n"; public Main() { super(new GridBagLayout()); GridBagLayout gridbaglayout = (GridBagLayout) getLayout(); GridBagConstraints constraints = new GridBagConstraints(); JLabel jLabel = null; constraints.fill = GridBagConstraints.BOTH; constraints.gridwidth = GridBagConstraints.REMAINDER; jLabel = new JLabel("MultiListener heard:"); gridbaglayout.setConstraints(jLabel, constraints); add(jLabel); constraints.weighty = 1.0; topText = new JTextArea(); topText.setEditable(false); JScrollPane topScrollPane = new JScrollPane(topText); Dimension preferredSize = new Dimension(220, 85); topScrollPane.setPreferredSize(preferredSize); gridbaglayout.setConstraints(topScrollPane, constraints); add(topScrollPane); constraints.weightx = 0.0; constraints.weighty = 0.0; jLabel = new JLabel("Eve heard:"); gridbaglayout.setConstraints(jLabel, constraints); add(jLabel); constraints.weighty = 1.0; bottomText = new JTextArea(); bottomText.setEditable(false); JScrollPane bottomScrollPane = new JScrollPane(bottomText); bottomScrollPane.setPreferredSize(preferredSize); gridbaglayout.setConstraints(bottomScrollPane, constraints); add(bottomScrollPane); constraints.weightx = 1.0; constraints.weighty = 0.0; constraints.gridwidth = 1; constraints.insets = new Insets(10, 10, 0, 10); b1 = new JButton("Jiber Jaber"); gridbaglayout.setConstraints(b1, constraints); add(b1); constraints.gridwidth = GridBagConstraints.REMAINDER; b2 = new JButton("Be serious"); gridbaglayout.setConstraints(b2, constraints); add(b2); b1.addActionListener(this); b2.addActionListener(this); b2.addActionListener(new ManInTheMiddle(bottomText)); setPreferredSize(new Dimension(600, 600)); setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 1, 2, 2, Color.black), BorderFactory.createEmptyBorder(5, 5, 5, 5))); } @Override public void actionPerformed(ActionEvent event) { topText.append(event.getActionCommand() + nl); topText.setCaretPosition(topText.getDocument().getLength()); } private static void dispGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(""); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Main(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { dispGUI(); } }); } } class ManInTheMiddle implements ActionListener { JTextArea mText; public ManInTheMiddle(JTextArea text) { mText = text; } @Override public void actionPerformed(ActionEvent event) { mText.append(event.getActionCommand() + Main.nl); mText.setCaretPosition(mText.getDocument().getLength()); } }
This was an example on how to create a Multilistener component in Java.