Can i have some sample Swing code that uses Dynamic Proxies to implement some meaningful functionality? Say a one single place to handle a bunch of listeners?

Davanum Srinivas

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

/**
 * Example to show how to use the Dynamic Proxy API of the J2SE version 1.3
 */
public class ProxyDemo extends JFrame implements InvocationHandler {

    private JLabel lblName;
    private JTextField txfName;
    private JButton btnOk;
    private JButton btnClear;
    private Proxy aListenerProxy;

    /**
     * Create a new ProxyDemo instance.
     */
    public ProxyDemo() {
        super("Dynamic Proxy Demo");

        initGui();

        initListeners();
    }

    /**
     * Initializes GUI components.
     */
    private void initGui() {
        lblName = new JLabel("Title Name:");
        txfName = new JTextField(15);
        btnOk = new JButton("Ok");
        btnClear = new JButton("Clear");
        btnClear.setEnabled(false);

        JPanel pnlName = new JPanel(new GridBagLayout());
        pnlName.add(lblName, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 6, 5, 5), 0, 0));
        pnlName.add(txfName, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 5, 5), 0, 0));


        JPanel pnlButton = new JPanel(new GridBagLayout());
        pnlButton.add(btnOk, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 6, 5, 5), 0, 0));
        pnlButton.add(btnClear, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 6, 5, 5), 0, 0));

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(pnlName, BorderLayout.CENTER);
        getContentPane().add(pnlButton, BorderLayout.SOUTH);
        pack();
    }

    /**
     * Intializes the proxy and add itself to the components.
     */
    private void initListeners() {
        // Create the dynamic proxy
        aListenerProxy = (Proxy) Proxy.newProxyInstance(getClass().getClassLoader(),
                new Class[] { WindowListener.class, ActionListener.class, FocusListener.class, DocumentListener.class },
                this);

        // Add the proxy to the components
        addWindowListener((WindowListener) aListenerProxy);
        btnOk.addActionListener((ActionListener) aListenerProxy);
        btnClear.addActionListener((ActionListener) aListenerProxy);
        txfName.addActionListener((ActionListener) aListenerProxy);
        txfName.addFocusListener((FocusListener) aListenerProxy);
        txfName.getDocument().addDocumentListener((DocumentListener) aListenerProxy);
    }

    /**
     * Implementation of the InvocationHandler interface.
     */
    public Object invoke(Object aProxy, Method aMethod, Object[] someArguments) {
        // If a method invocation was done on the WindowListener interface
        if (aMethod.getDeclaringClass() == WindowListener.class) {
            // Close the demo and exit the VM
            if (aMethod.getName().equals("windowClosing")) {
                System.exit(0);
            }

        // If a method invocation was done on the ActionListener interface
        } else if (aMethod.getDeclaringClass() == ActionListener.class) {
            // Perform the action of the buttons
            ActionEvent anEvent = (ActionEvent) someArguments[0];
            if (anEvent.getSource() == btnOk ||
                anEvent.getSource() == txfName) {
                setTitle(txfName.getText());
            } else {
                txfName.setText("");
            }

        // If a method invocation was done on the FocusListener interface
        } else if (aMethod.getDeclaringClass() == FocusListener.class) {
            // Set the background color in yellow if the text field has the focus
            if (aMethod.getName().equals("focusGained")) {
                txfName.setBackground(Color.yellow);
            } else {
                txfName.setBackground(Color.white);
            }

        // If a method invocation was done on the DocumentListener interface
        } else if (aMethod.getDeclaringClass() == DocumentListener.class) {
            // Enable / Disable the clear button
            if (txfName.getText().equals("")) {
                btnClear.setEnabled(false);
            } else {
                btnClear.setEnabled(true);
            }
        }

        return null;
    }

    /**
     * Main
     */
    public static void main(String[] args) {
        new ProxyDemo().show();
    }
}
More details can be found at:Building Classes Dynamically with Java2.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact