aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with mouse listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with mouse listener" Watch "Problem with mouse listener" New topic
Author

Problem with mouse listener

Cristiano Sousa
Greenhorn

Joined: Nov 30, 2012
Posts: 5
Hi everyone.
I am making a game of air traffic control and I am having problems in making the airplane change place. It is based in a JLabel array and I want it to select the airplane with the left mouse click and then, with right click I want the airplane to go to the selected index. This is an early draft, but the current code that I have for the listener is this:

The left click is working fine, it changes the imageicon of the selected JLabel the the image that I am using to show a selected airplane. But when I right click a JLabel nothing happens. Anyone can help me?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
mouseClicked() is *always* a poor choice, as it requires there to be no x or y movement between pressed() and released()

try it using mousePressed() instead
Cristiano Sousa
Greenhorn

Joined: Nov 30, 2012
Posts: 5
I tried it, it doesn't solve my problem.
I think the problem must be here:

since "grelha" is my JLabels array and "aviaos" is a JLabel it is correct to check if they're equal by using "==" right?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I tried it, it doesn't solve my problem.

worked OK for me.

you posted "But when I right click a JLabel nothing happens."
so I added a System.out.println(...) and it printed out fine.

if the problem is something else, you would need to post a sample program
that we can copy/paste/compile/run/observe

a sample program is one with nothing unrelated to the problem (menus etc) included in the code.
Cristiano Sousa
Greenhorn

Joined: Nov 30, 2012
Posts: 5
So I changed my code and I did it like this:


Now both buttons work, but when I use right click it doesn't change the previous location of the airplane ( JLabel with ImageIcon) to an empty cell ( ImageIcon set to null).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Michael Dunn wrote:mouseClicked() is *always* a poor choice, . . .
If you are using buttons, a mouse listener is a bad choice. You should, as a general rule, use the highest‑level Listener you can lay your hands on. In the case of buttons, that means ActionListener is what you go for as a default choice. It’s all in the Java Tutorials, where they call high level listeners listeners for “semantic events”, and give a good reason why an ActionListener is better for buttons.
I do not like the combined actionPerformed() method approach. Get rid of the getSource() calls and use a separate Listener for each button. More details in older posts.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> it doesn't change the previous location of the airplane ( JLabel with ImageIcon) to an empty cell

if this is the line you refer to
janela.grelha[a][b].setIcon(null);

either duplicate references (unlikely, if the other label changes it's icon to 'aviao'),
or [a][b] is not what you think it is
Cristiano Sousa
Greenhorn

Joined: Nov 30, 2012
Posts: 5

the "a" and "b" is supposed to be the coordinates of the first click, but now I found out that this works only for grelha[0][0]. That makes me think that a and b are not being changed or that every time I click a button they are created again. Anyone knows how I can solve this? I am not using buttons, the listener is on the jlabels
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the "a" and "b" is supposed to be the coordinates of the first click,

coordinates of what, in relation to what?

if you're trying to ID the label via cooordinates, you're better off using getSource()
Cristiano Sousa
Greenhorn

Joined: Nov 30, 2012
Posts: 5
I am trying to use "a" and "b" to identify the index of the left click in the array of jlabels(grelha), because when I use right click I need to know the index where the first click occurred to set the ImageIcon to null. It is as if I wanted to change it's position. I need to know where it is now and where it is going to.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with mouse listener