Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: implement mouse listener class

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default implement mouse listener class

    I can't figure out how to implement this class:

    public class ViewController extends CircleView {
    public ViewController() {
    // Register mouse listener
    addMouseListener(new java.awt.event.MouseAdapter() {
    public void mousePressed(java.awt.event.MouseEvent e) {
    CircleModel model = getModel(); // Get model

    if (model == null) return;

    if (e.isMetaDown())
    model.setRadius(model.getRadius() - 5); // Right button
    else
    model.setRadius(model.getRadius() + 5); // Left button
    }
    });
    }
    }


    ************

    To this program:



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

    public class CircleController extends JPanel {
    private CircleModel model;
    private JTextField jtfRadius = new JTextField();
    private JButton jbtSend = new JButton("SEND");
    private JComboBox jcboFilled = new JComboBox(new Boolean[]{
    new Boolean(false), new Boolean(true)});

    /** Creates new form CircleController */
    public CircleController() {
    // Panel to group labels
    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(3, 1));
    panel1.add(new JLabel("Radius"));
    panel1.add(new JLabel(""));
    panel1.add(new JLabel("Filled"));

    // Panel to group text field, combo box, and another panel
    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(3, 1));
    panel2.add(jtfRadius);
    panel2.add(jbtSend);
    panel2.add(jcboFilled);

    setLayout(new BorderLayout());
    add(panel1, BorderLayout.WEST);
    add(panel2, BorderLayout.CENTER);

    // Register listeners

    jbtSend.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (model == null) return; // No model associated yet. Do nothing
    model.setRadius(new Double(jtfRadius.getText()).doubleValue());
    }
    });

    jcboFilled.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (model == null) return; // No model associated yet. Do nothing
    model.setFilled(
    ((Boolean)jcboFilled.getSelectedItem()).booleanVal ue());
    }
    });
    }

    public void setModel(CircleModel newModel) {
    model = newModel;
    }

    public CircleModel getModel() {
    return model;
    }
    }


    ******************


    import java.awt.event.*;
    import java.util.*;

    public class CircleModel {
    /** Property radius. */
    private double radius = 20;

    /** Property filled. */
    private boolean filled;

    /** Property color. */
    private java.awt.Color color;

    /** Utility field used by event firing mechanism. */
    private ArrayList<ActionListener> actionListenerList;

    public double getRadius() {
    return radius;
    }

    public void setRadius(double radius) {
    this.radius = radius;

    // Notify the listener for the change on radius
    processEvent(
    new ActionEvent(this, ActionEvent.ACTION_PERFORMED,"radius"));
    }

    public boolean isFilled() {
    return filled;
    }

    public void setFilled(boolean filled) {
    this.filled = filled;

    // Notify the listener for the change on filled
    processEvent(
    new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "filled"));
    }

    public java.awt.Color getColor() {
    return color;
    }

    public void setColor(java.awt.Color color) {
    this.color = color;

    // Notify the listener for the change on color
    processEvent(
    new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "color"));
    }

    /** Register an action event listener */
    public synchronized void addActionListener(ActionListener l) {
    if (actionListenerList == null)
    actionListenerList = new ArrayList<ActionListener>();

    actionListenerList.add(l);
    }

    /** Remove an action event listener */
    public synchronized void removeActionListener(ActionListener l) {
    if (actionListenerList != null && actionListenerList.contains(l))
    actionListenerList.remove(l);
    }

    /** Fire TickEvent */
    private void processEvent(ActionEvent e) {
    ArrayList list;

    synchronized (this) {
    if (actionListenerList == null) return;
    list = (ArrayList)actionListenerList.clone();
    }

    for (int i = 0; i < list.size(); i++) {
    ActionListener listener = (ActionListener)list.get(i);
    listener.actionPerformed(e);
    }
    }
    }


    *********************

    import java.awt.*;
    import java.awt.event.*;

    public class CircleView extends javax.swing.JPanel
    implements ActionListener {
    private CircleModel model;

    public void actionPerformed(ActionEvent actionEvent) {
    repaint();
    }

    /** Set a model */
    public void setModel(CircleModel newModel) {
    model = newModel;

    if (model != null)
    // Register the view as listener for the model
    model.addActionListener(this);

    repaint();
    }

    public CircleModel getModel() {
    return model;
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (model == null) return;

    g.setColor(model.getColor());

    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    int radius = (int)model.getRadius();

    if (model.isFilled()) {
    g.fillOval(xCenter - radius, yCenter - radius,
    2 * radius, 2 * radius);
    }
    else {
    g.drawOval(xCenter - radius, yCenter - radius,
    2 * radius, 2 * radius);
    }
    }
    }

    **********

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

    public class MVCDemo extends JApplet {
    private JButton jbtController = new JButton("Show Controller");
    private JButton jbtView = new JButton("Show View");
    private CircleModel model = new CircleModel();

    public MVCDemo() {
    setLayout(new FlowLayout());
    add(jbtController);
    add(jbtView);

    jbtController.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFrame frame = new JFrame("Controller");
    CircleController controller = new CircleController();
    controller.setModel(model);
    frame.add(controller);
    frame.setSize(200, 200);
    frame.setLocation(200, 200);
    frame.setVisible(true);
    }
    });

    jbtView.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFrame frame = new JFrame("View");
    CircleView view = new CircleView();
    view.setModel(model);
    frame.add(view);
    frame.setSize(500, 200);
    frame.setLocation(200, 200);
    frame.setVisible(true);
    }
    });
    }

    public static void main(String[] args) {
    MVCDemo applet = new MVCDemo();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setTitle("MVCDemo");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    frame.setSize(400, 320);
    frame.setVisible(true);
    }
    }

    ***************

    Thanks for helping!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: implement mouse listener class

    I recommend reading the link in my signature entitled 'getting help'...in particular the sections on using the highlight tags, asking a specific question, and providing an SSCCE

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  2. [SOLVED] Is it possible for a mouse listener to exist without a GUI interface?
    By techwiz24 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 3rd, 2011, 02:32 PM
  3. Replies: 0
    Last Post: February 12th, 2011, 06:44 PM
  4. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM
  5. Java program to contol computer mouse using the awt Robot and events
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: February 11th, 2009, 03:17 PM