4

I am a Java student and just ended the basic functionality of a little command line card game. The game is a simplified version of Magic-type trading card game. There is no AI, you play against yourself or another human player.

At this point, i am trying to add some GUI to it using MVC, but i'm finding problems adding a MouseListener to a button.

This is a brief explanation of what's going on:

  • I have a Model class, that extends Observable by inheritance of a superclass
  • A View class, that implements Observer.
  • And a Controller class that extends MouseAdapter

Then i put everything together:

....

View view = new View();
Model model = new Model();

model.addObserver( view );

Controller controller = new Controller();
// associate Controller's Model and View objects
controller.addModel(model);
controller.addView(view);

view.addController(controller); // i try to add the MouseListener

....

The addController() method of View is:

public void addController(Controller controller){
            this.myButton.addMouseListener( controller )
}

I already checked that addController() method is being called (println something inside it), but the Listener is not being set for some reason: mouseReleased() is never called when i click the button.

Any thoughts or any step that i may have overlooked? Appreciate.

Edit (Controller code):

public class Controller extends MouseAdapter {

    Model model;
    View view;

    public void addModel(Model m){
            this.model = m;
    }

    public void addView(View ui){
            this.view = ui;
    }

    // All @Overrides

    @Override
    public void mouseReleased(MouseEvent me) {
        System.out.println("oh, it arrived");
    }
}
Thorn
  • 4,015
  • 4
  • 23
  • 42
TMichel
  • 4,336
  • 9
  • 44
  • 67
  • 2
    1. [maybe will help you with one of possible logics](http://stackoverflow.com/questions/8169964/is-mvc-in-swing-thread-safe), 2. notice output to the Swing GUI should be wrapped in invokeLater, 3. again for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, 4. because we can't see rest of (importannt) code, 5. those code lines talking about ... – mKorbel Nov 04 '12 at 10:33
  • Can you show the code from Controller? Maybe you are not correctly overriding the method from MouseAdapter. This is the signature of the method "public void mouseReleased(MouseEvent e)" – Dan Iliescu Nov 04 '12 at 10:34
  • 2
    The mouse events may be begin consumed by the button and never reaching you for further processing. Better to use an action listener on a button, which will also be fired if the user presses space or enter when the button is focused – MadProgrammer Nov 04 '12 at 10:45
  • See also this [example](http://stackoverflow.com/a/3072979/230513). – trashgod Nov 04 '12 at 12:34

1 Answers1

4

You must be doing something wrong, but I can not say what without the code. Here is some simple code that works (maybe it can help you understand what you are doing wrong):

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

public class Test1 extends JFrame {

    public Test1() {
        initUI();
    }

    private void initUI() {
        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
        container.setBackground(Color.black);

        JButton b = new JButton("test");
        b.addMouseListener(new Controller());
        container.add(b);

        add(container);
        pack();

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Test1 c = new Test1();
                c.setVisible(true);
            }
        });
    }

    class Controller extends MouseAdapter {
        @Override
        public void mouseReleased(MouseEvent me) {
            System.out.println("oh, it arrived");
        }
    }

}
Dan Iliescu
  • 435
  • 2
  • 7
  • Provide "one that works" is a really nice way to tackle a question with insufficient information to identify the problem yourself... you're most kind ;-) – corlettk Nov 04 '12 at 11:07
  • 1
    The code already provided seams OK. Without the rest there isn't much help we can give. – Dan Iliescu Nov 04 '12 at 11:20
  • you are right, my excuses on overlook the JFrame part, it was indeed the origin of the conflict. Just modified the way i was initializing the swing part as Dan illustrates, and now the Listener works. Many many thanks :) – TMichel Nov 04 '12 at 11:47