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 9 of 9

Thread: problem with the MouseListener

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

    Default problem with the MouseListener

    I built a game that built an matrix of labels i add the labels an mouselistener and The problem is that when I press event on a
    particular label, say there is map[0][0] It create an event no matter what I click .... How do I create a matrix of labels when I do certain conditions on each label was only then that event will happen
    sorry about my english
    this is my code:


    public class Board 
    {
    	private int x;
    	private int y;
    	private Point location;
    	private ImageIcon picture;
    	private JLabel label;
    	private Piece[][] map;
     
    	public Board()
    	{
    		this.x = 47;
    		this.y = 45;
    		this.location = new Point();
    		this.map = new Piece[9][12];
     
    		for (int i = 0; i < 9; i++)
    		{
    			for (int j = 0; j < 12; j++)
    			{
    				this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
    			}
    		}
    	}
     
    	public Piece[][] getMap()
    	{
    		return this.map;
    	}
     
    	public void setMap(Piece[][] map)
    	{
    		this.map = map;
    	}
     
    	public void draw(Panelmenu panel)
    	{
    		for (int i = 0; i < 9; i++) 
    		{
    			for (int j = 0; j < 12; j++) 
    			{
    				this.location.setX(this.x);
    				this.location.setY(this.y);
    				System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
    				this.map[i][j].setLocation(this.location);
    				System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
     
    				this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
    				this.map[i][j].setPicture(this.picture);
     
    				this.label = this.map[i][j].getLabel();
    				this.label.setText("");
    				this.label.setIcon(this.map[i][j].getPicture());
    				this.label.setHorizontalAlignment(JLabel.CENTER);
    				this.label.setSize(58, 58);
    				this.label.setLocation(this.x, this.y);
    				this.map[i][j].setLabel(this.label);
     
    				this.map[i][j].setType(0);
    				this.map[i][j].setStatus(true);
     
    				this.x += 62;
     
    				if (j == 11)
    				{
    					this.x = 47; 
    					this.y += 59;
    				}
     
    				this.map[i][j].draw(panel);
    			}
    		}
    	}
    }



    this is the Piece class:




    public class Piece implements MouseListener
    {
    	private Point location;
    	private ImageIcon picture;
    	private JLabel label;
    	private int type;
    	private boolean status;
     
    	public Piece(Point location, ImageIcon picture, JLabel label, int type, boolean status)
    	{
    		this.location = location;
    		this.picture = picture;
    		this.label = label;
    		this.type = type;
    		this.status = status;
    		this.label.addMouseListener(this);
    	}
     
    	public Point getLocation() 
    	{
    		return this.location;
    	}
     
    	public void setLocation(Point location)
    	{
    		this.location = location;
    		System.out.println("Piece = " + this.location);
    	}
     
    	public ImageIcon getPicture()
    	{
    		return this.picture;
    	}
     
    	public void setPicture(ImageIcon picture)
    	{
    		this.picture = picture;
    	}
     
    	public JLabel getLabel() 
    	{
    		return this.label;
    	}
     
    	public void setLabel(JLabel label) 
    	{
    		this.label = label;
    	}
     
    	public int getType()
    	{
    		return this.type;
    	}
     
    	public void setType(int type) 
    	{
    		this.type = type;
    	}
     
    	public boolean getStatus()
    	{
    		return this.status;
    	}
     
    	public void setStatus(boolean status)
    	{
    		this.status = status;
    	}
     
    	public void draw(JPanel panel)
    	{
    		panel.add(this.label);
    	}
     
    	@Override
    	public void mouseClicked(MouseEvent e)
    	{
     
    		System.out.println("[" + this.location.getX() + ", " + this.location.getY() + "]");
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with the MouseListener

    Your English is fine, but what exactly is the problem?

    You're adding a MouseListener to every JLabel, so it's going to print out the location no matter what. If you only want certain JLabels to respond to the MouseListener, either don't add the MouseListener to them or add some logic to the mouseClicked() method.

    Also, since each JLabel's MouseListener is doing the same thing, why do you create a new instance for each JLabel? Why not just use a single instance of your MouseListener?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with the MouseListener

    my game is like the game exagon know that game?
    so i want that the board will be a matrix... i added printers because i want to know what is the pointers in evry piece and piece...
    but i write me all the time the last x and y that changed...
    a add an "if " that say if i`m clicking on map[0][0].jlabel write "bobo" ... but he does not refer to that "if"
    if i am clicking on the label in map[0][1].jlabel he will also print "bobo"
    how can i do that it will consider in the metrix and the clicks will happend depend the map[][]

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with the MouseListener

    i added print because i want to print the points in evry piece... but he doesnt print the point... he prints me all the time the last x and y
    of the last piece (map[8][11])

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with the MouseListener

    Where do you get the x,y values you are printing?

    Make sure you get a new Point object for each Piece. Do not continue to use only one Point object.
    Last edited by Norm; August 22nd, 2011 at 05:37 PM.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with the MouseListener

    i get the same value in evry labels

    public class Board implements MouseListener
    {
    	private int x;
    	private int y;
    	private Point location;
    	private ImageIcon picture;
    	private JLabel label;
    	private Piece[][] map;
     
    	public Board()
    	{
    		this.x = 47;
    		this.y = 45;
    		this.location = new Point();
    		this.map = new Piece[9][12];
     
    		for (int i = 0; i < 9; i++)
    		{
    			for (int j = 0; j < 12; j++)
    			{
    				this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
     
    			}
    		}
    	}
     
    	public Piece[][] getMap()
    	{
    		return this.map;
    	}
     
    	public void setMap(Piece[][] map)
    	{
    		this.map = map;
    	}
     
    	public void draw(Panelmenu panel)
    	{
    		for (int i = 0; i < 9; i++) 
    		{
    			for (int j = 0; j < 12; j++) 
    			{
    				this.location.setX(this.x);
    				this.location.setY(this.y);
    				System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
    				this.map[i][j].setLocation(this.location);
    				System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
     
    				this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
    				this.map[i][j].setPicture(this.picture);
     
    				this.label = this.map[i][j].getLabel();
    				this.label.setText("");
    				this.label.setIcon(this.map[i][j].getPicture());
    				this.label.setHorizontalAlignment(JLabel.CENTER);
    				this.label.setSize(58, 58);
    				this.label.setLocation(this.x, this.y);
    				this.label.addMouseListener(this);
    				this.map[i][j].setLabel(this.label);
     
    				this.map[i][j].setType(0);
    				this.map[i][j].setStatus(true);
     
    				this.x += 62;
     
    				if (j == 11)
    				{
    					this.x = 47; 
    					this.y += 59;
    				}
     
    				this.map[i][j].draw(panel);
    			}
    		}
    	}
     
    	@Override
    	public void mouseClicked(MouseEvent arg0) 
    	{
    		System.out.println("[" + map[0][0].getLocation().getX() + ", " +map[0][0].getLocation().getY()+ "]");
     
    	}

    the first point is in map[0][0].getloction().getx() is x=47

    but when i clicking on im i print that the x value in map[0][0]= 729
    the last value x gets....
    he does not give me the corect x
    map[0][0].getLocation().getX() need to print 47 not 729
    evry x and y on the map array is different so when i clicking on label i want that he will print the value that i set to the label.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with the MouseListener

    i get the same value in evry labels
    That sounds like all the Piece objects are using the SAME Point object.
    Check your code to be sure that each Piece has it own UNIQUE Point object.
    One way to check is to print out the reference to the Point object EVERY time it is set and changed.
    The output should look something like: Point@e43541
    The output for all of them should be different. If they are all the same, then all your Piece objects are sharing ONE Point object.
    One easy way to test for this case, is to add a method to the Piece class that prints out the Point object. Write a loop after all the Piece objects have been created and filled with data that calls that method for all the Piece objects and look at what is printed. If all of them print the same value, them you know that you need to check why your code is giving the SAME Point object to each Piece object.

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with the MouseListener

    i check`t the points and you right! all the points are the same...

    where is the problem in may code because i adding the pieces to the panel ... way the draw is on the same point?
    this is my code:

    public class Board implements MouseListener
    {
    	private int x;
    	private int y;
    	private Point location;
    	private ImageIcon picture;
    	private JLabel label;
    	private Piece[][] map;
     
    	public Board()
    	{
    		this.x = 47;
    		this.y = 45;
    		this.location = new Point();
    		this.map = new Piece[9][12];
     
    		for (int i = 0; i < 9; i++)
    		{
    			for (int j = 0; j < 12; j++)
    			{
    				this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
     
    			}
    		}
    	}
     
    	public Piece[][] getMap()
    	{
    		return this.map;
    	}
     
    	public void setMap(Piece[][] map)
    	{
    		this.map = map;
    	}
     
    	public void draw(Panelmenu panel)
    	{
    		for (int i = 0; i < 9; i++) 
    		{
    			for (int j = 0; j < 12; j++) 
    			{
    				this.location.setX(this.x);
    				this.location.setY(this.y);
    		//		System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
    				this.map[i][j].setLocation(this.location);
    		//		System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
     
    				this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
    				this.map[i][j].setPicture(this.picture);
     
    				this.label = this.map[i][j].getLabel();
    				this.label.setText("");
    				this.label.setIcon(this.map[i][j].getPicture());
    				this.label.setHorizontalAlignment(JLabel.CENTER);
    				this.label.setSize(58, 58);
    				this.label.setLocation(this.x, this.y);
    				this.label.addMouseListener(this);
    				this.map[i][j].setLabel(this.label);
     
    				this.map[i][j].setType(0);
    				this.map[i][j].setStatus(true);
     
    				this.x += 62;
     
    				if (j == 11)
    				{
    					this.x = 47; 
    					this.y += 59;
    				}
     
    				this.map[i][j].draw(panel);
    			}
     
    		}
     
    		check();
    	}
    	public void check()
    	{
     
    	for (int i = 0; i < 9; i++)
    	{
    		for (int j = 0; j < 12; j++)
    		{
    			System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
     
    		}
    	}
     
    	}

    the metod check is prints the points
    this is the results:

    0, 0] = [Point = Point [x=729, y=517]]
    [0, 1] = [Point = Point [x=729, y=517]]
    [0, 2] = [Point = Point [x=729, y=517]]
    [0, 3] = [Point = Point [x=729, y=517]]
    [0, 4] = [Point = Point [x=729, y=517]]
    [0, 5] = [Point = Point [x=729, y=517]]
    [0, 6] = [Point = Point [x=729, y=517]]
    [0, 7] = [Point = Point [x=729, y=517]]
    [0, 8] = [Point = Point [x=729, y=517]]
    [0, 9] = [Point = Point [x=729, y=517]]
    [0, 10] = [Point = Point [x=729, y=517]]
    [0, 11] = [Point = Point [x=729, y=517]]
    [1, 0] = [Point = Point [x=729, y=517]]
    [1, 1] = [Point = Point [x=729, y=517]]
    [1, 2] = [Point = Point [x=729, y=517]]
    [1, 3] = [Point = Point [x=729, y=517]]
    [1, 4] = [Point = Point [x=729, y=517]]
    [1, 5] = [Point = Point [x=729, y=517]]
    [1, 6] = [Point = Point [x=729, y=517]]
    [1, 7] = [Point = Point [x=729, y=517]]
    [1, 8] = [Point = Point [x=729, y=517]]
    [1, 9] = [Point = Point [x=729, y=517]]
    [1, 10] = [Point = Point [x=729, y=517]]
    [1, 11] = [Point = Point [x=729, y=517]]
    [2, 0] = [Point = Point [x=729, y=517]]
    [2, 1] = [Point = Point [x=729, y=517]]
    [2, 2] = [Point = Point [x=729, y=517]]
    [2, 3] = [Point = Point [x=729, y=517]]
    [2, 4] = [Point = Point [x=729, y=517]]
    [2, 5] = [Point = Point [x=729, y=517]]
    [2, 6] = [Point = Point [x=729, y=517]]
    [2, 7] = [Point = Point [x=729, y=517]]
    [2, 8] = [Point = Point [x=729, y=517]]
    [2, 9] = [Point = Point [x=729, y=517]]
    [2, 10] = [Point = Point [x=729, y=517]]
    [2, 11] = [Point = Point [x=729, y=517]]
    [3, 0] = [Point = Point [x=729, y=517]]
    [3, 1] = [Point = Point [x=729, y=517]]
    [3, 2] = [Point = Point [x=729, y=517]]
    [3, 3] = [Point = Point [x=729, y=517]]
    [3, 4] = [Point = Point [x=729, y=517]]
    [3, 5] = [Point = Point [x=729, y=517]]
    [3, 6] = [Point = Point [x=729, y=517]]
    [3, 7] = [Point = Point [x=729, y=517]]
    [3, 8] = [Point = Point [x=729, y=517]]
    [3, 9] = [Point = Point [x=729, y=517]]
    [3, 10] = [Point = Point [x=729, y=517]]
    [3, 11] = [Point = Point [x=729, y=517]]
    [4, 0] = [Point = Point [x=729, y=517]]
    [4, 1] = [Point = Point [x=729, y=517]]
    [4, 2] = [Point = Point [x=729, y=517]]
    [4, 3] = [Point = Point [x=729, y=517]]
    [4, 4] = [Point = Point [x=729, y=517]]
    [4, 5] = [Point = Point [x=729, y=517]]
    [4, 6] = [Point = Point [x=729, y=517]]
    [4, 7] = [Point = Point [x=729, y=517]]
    [4, 8] = [Point = Point [x=729, y=517]]
    [4, 9] = [Point = Point [x=729, y=517]]
    [4, 10] = [Point = Point [x=729, y=517]]
    [4, 11] = [Point = Point [x=729, y=517]]
    [5, 0] = [Point = Point [x=729, y=517]]
    [5, 1] = [Point = Point [x=729, y=517]]
    [5, 2] = [Point = Point [x=729, y=517]]
    [5, 3] = [Point = Point [x=729, y=517]]
    [5, 4] = [Point = Point [x=729, y=517]]
    [5, 5] = [Point = Point [x=729, y=517]]
    [5, 6] = [Point = Point [x=729, y=517]]
    [5, 7] = [Point = Point [x=729, y=517]]
    [5, 8] = [Point = Point [x=729, y=517]]
    [5, 9] = [Point = Point [x=729, y=517]]
    [5, 10] = [Point = Point [x=729, y=517]]
    [5, 11] = [Point = Point [x=729, y=517]]
    [6, 0] = [Point = Point [x=729, y=517]]
    [6, 1] = [Point = Point [x=729, y=517]]
    [6, 2] = [Point = Point [x=729, y=517]]
    [6, 3] = [Point = Point [x=729, y=517]]
    [6, 4] = [Point = Point [x=729, y=517]]
    [6, 5] = [Point = Point [x=729, y=517]]
    [6, 6] = [Point = Point [x=729, y=517]]
    [6, 7] = [Point = Point [x=729, y=517]]
    [6, 8] = [Point = Point [x=729, y=517]]
    [6, 9] = [Point = Point [x=729, y=517]]
    [6, 10] = [Point = Point [x=729, y=517]]
    [6, 11] = [Point = Point [x=729, y=517]]
    [7, 0] = [Point = Point [x=729, y=517]]
    [7, 1] = [Point = Point [x=729, y=517]]
    [7, 2] = [Point = Point [x=729, y=517]]
    [7, 3] = [Point = Point [x=729, y=517]]
    [7, 4] = [Point = Point [x=729, y=517]]
    [7, 5] = [Point = Point [x=729, y=517]]
    [7, 6] = [Point = Point [x=729, y=517]]
    [7, 7] = [Point = Point [x=729, y=517]]
    [7, 8] = [Point = Point [x=729, y=517]]
    [7, 9] = [Point = Point [x=729, y=517]]
    [7, 10] = [Point = Point [x=729, y=517]]
    [7, 11] = [Point = Point [x=729, y=517]]
    [8, 0] = [Point = Point [x=729, y=517]]
    [8, 1] = [Point = Point [x=729, y=517]]
    [8, 2] = [Point = Point [x=729, y=517]]
    [8, 3] = [Point = Point [x=729, y=517]]
    [8, 4] = [Point = Point [x=729, y=517]]
    [8, 5] = [Point = Point [x=729, y=517]]
    [8, 6] = [Point = Point [x=729, y=517]]
    [8, 7] = [Point = Point [x=729, y=517]]
    [8, 8] = [Point = Point [x=729, y=517]]
    [8, 9] = [Point = Point [x=729, y=517]]
    [8, 10] = [Point = Point [x=729, y=517]]
    [8, 11] = [Point = Point [x=729, y=517]]


    way? i dont know way
    need help....
    this is the piece class:

    public class Piece 
    {
    	private Point location;
    	private ImageIcon picture;
    	private JLabel label;
    	private int type;
    	private boolean status;
     
    	public Piece(Point location, ImageIcon picture, JLabel label, int type, boolean status)
    	{
    		this.location = location;
    		this.picture = picture;
    		this.label = label;
    		this.type = type;
    		this.status = status;
    		//this.label.addMouseListener(this);
    	}
     
    	public Point getLocation() 
    	{
    		return this.location;
    	}
     
    	public void setLocation(Point location)
    	{
    		this.location = location;
    	//	System.out.println("Piece = " + this.location);
    	}
     
    	public ImageIcon getPicture()
    	{
    		return this.picture;
    	}
     
    	public void setPicture(ImageIcon picture)
    	{
    		this.picture = picture;
    	}
     
    	public JLabel getLabel() 
    	{
    		return this.label;
    	}
     
    	public void setLabel(JLabel label) 
    	{
    		this.label = label;
    	}
     
    	public int getType()
    	{
    		return this.type;
    	}
     
    	public void setType(int type) 
    	{
    		this.type = type;
    	}
     
    	public boolean getStatus()
    	{
    		return this.status;
    	}
    	//System.out.println("[" + this.location.getX() + ", " + this.location.getY() + "]");
    	public void setStatus(boolean status)
    	{
    		this.status = status;
    	}
     
    	public void draw(JPanel panel)
    	{
    		panel.add(this.label);
    	}
     
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with the MouseListener

    Where do you change the value of the Piece's Point? Look at those places. At one of them you are setting them to be the same. Remember that assigning the value of a reference to a object to another reference does not create a new object. It makes the two references point to the SAME object. Where in your code are you NOT creating a new Point object before using it to set a reference?

Similar Threads

  1. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  2. Help with mouselistener and mousemotionlistener pls..
    By rentaw02 in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 05:45 PM
  3. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  4. [SOLVED] implements MouseListener doesn't work why
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 04:30 PM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM

Tags for this Thread