[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java get Coordinate of current mouse position

Posted on 2009-03-29
2
Medium Priority
?
1,298 Views
Last Modified: 2013-11-23
I have a double array [10][10] or JButtons.  I added the 100 buttons to a JPanel.  I wanted to know if there is a way to get the coordinate position  of the button where the mouse cursor is.  i.e. if the mouse is over the first button that would return 0,0.

Thanks.
0
Comment
Question by:ubuntuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 24013630
You could add a MouseMotionListener to your frame, which will tell you the new position of the cursor (x,y) every time it moves. But to work out which button it's on would require you knowing the dimensions of the buttons and their insets.

I expect a better method would be to add a MouseListener to each individual button.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24013690
Here's an example.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Example extends JFrame implements MouseListener
{
   JButton [] buttons=new JButton[100];
   
   public Example()
   {
      super("Example buttons");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      setLayout(new GridLayout(10,10));
      
      for(int i=0; i<buttons.length; i++)
      {
         buttons[i]=new JButton(""+i);
         buttons[i].setName(""+i); // this allows us to identify it later
         buttons[i].addMouseListener(this);
         add(buttons[i]);
      }
      
      pack();
      setLocationRelativeTo(null);
      setVisible(true);
   }
   
   public static void main(String[]a)
   {
      new Example();
   }
   
   
   public void mouseEntered(MouseEvent evt)
   {
      JButton buttonPressed=(JButton)evt.getSource();
      
      int xplusy=Integer.parseInt(buttonPressed.getName());
      int x=xplusy % 10;
      int y=(xplusy-x)/10;
      
      setTitle("Currently on button ("+x+", "+y+")");
   }
   
   public void mouseExited(MouseEvent evt)
   {
      setTitle("Example buttons");//back to normal
   }
   
   
   public void mousePressed(MouseEvent evt){}
   public void mouseReleased(MouseEvent evt){}
   public void mouseClicked(MouseEvent evt){}
}

Open in new window

0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question