Results 1 to 7 of 7

Thread: mouseListeners

  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default mouseListeners

    I made a program that displays a window with some stuff on it. Now I need to add a mouseListener and a methode that is called when a mouse event is generated. I found most of this information on oracle.com, except how to add mouse listeners.

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    You need to need import:
    javax.swing.event.*;
    Then, let your class implement MouseListener. Attach a listener to each JComponent:
    myJComponent.addMouseListener(this);
    Finally, add the unimplemented methods, which are all in the javadocs. Cheers,
    Crikey

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by crikey View Post
    You need to need import:
    javax.swing.event.*;
    Then, let your class implement MouseListener. Attach a listener to each JComponent:
    myJComponent.addMouseListener(this);
    Finally, add the unimplemented methods, which are all in the javadocs. Cheers,
    Crikey
    Did everything except the JComponent. This is just me being a noob but, how does a JComponent work? This is the code for my constructor:

    Java Code:
    public class editor extends JFrame implements MouseListener
    {
    	public editor()
    	{
    		setTitle("tower defence");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(608,634);
    		setLocation(0,0);
    		getContentPane().setBackground(Color.white);
    		setVisible(true);
    	}
    }
    Eclipse automaticly added the unimplemented methodes, but I don't have a JComponent I can use as far as I know.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your JFrame's contentPane is a JPanel which extends from JComponent (most all Swing components inherit from JComponent either directly or indirectly). I would either get the contentPane via getContentPane() and add the MouseListener to that. But having said that, does your class create JPanels and populate it with components? If so, it's often better to add the mouse listener to one of the JPanels.... hard to say though without knowing more of your program's logic.

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your JFrame's contentPane is a JPanel which extends from JComponent (most all Swing components inherit from JComponent either directly or indirectly). I would either get the contentPane via getContentPane() and add the MouseListener to that. But having said that, does your class create JPanels and populate it with components? If so, it's often better to add the mouse listener to one of the JPanels.... hard to say though without knowing more of your program's logic.
    Well, Im making a tower defence game. The game is going to consist of a play area and some information printed below that. The play area consists of squares (which are instances of the class square) which are drawn as rectangle polygons (I later found out I could have used the drawRect methode) when the paint methode is called. This class editor is going to be the map editor (I make this one first because the "game" class is going to take in a pre-made field of squares). The user has to decide where to put the castle (the destination for the creeps) and square by square where the path to the castle is going to be. They have to do this by clicking on the screen. Using the coordinates I calculate if, and if so which, square they pressed and if that square can be turned into the castle or path and when that is possible, repaint the square.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •