MouseEvent getPoint() problem

I have two JFrame windows in an application each of which has a JPanel listening to mouse events. In one the events's getPoint returns the click location relative to the clicked component, but in the other, it returns the click location relative to the top-left corner of the parent JFrame (e.g., a click at the top of the component does not have y=0 and the location shifts down if a menu bar is added to the window). I think they both used to be relative to the clicked component, but the result has recently changed. I cannot find a change I made to cause the change.
How is this possible?

Torgil wrote:
John_A_Nairn wrote:
I was able to solve it myself. It turns out I had recently added a getParent() method to my JPanel subclass not realizing that this was overriding a Component method. It must be that the mouse event workings use this method to set the click location. Renaming my method solved the problem.This is the third post (that I've seen) in just the last month where a program has not worked because a Swing component was extended and a method from a superclass was overridden by mistake.
When will people learn not to always automatically create a subclass of JPanel, JDialog, JFrame, etc as soon as they need a JPanel, JDialog, JFrame, etc?!? :-)I make that point all the time: don't subclass when you don't need to, and you only need to if you are overriding a method (and make sure you absolutely need to do that override!). Then people respond: I absolutely need to subclass JPanel (JFrame/JDialog/JInternalFrame) because it has-a JButton/JTextField/JLabel/... sigh So that's what "has-a" means these days?

Similar Messages

  • MouseEvent.CLICK Problem i think...

    I am making a game that wil be included in a school project. its a basic shoot the alien type game. My problem is that it will not respond to a mouse click at all. The weired thing is using the same exact code just changing the event to MOUSE_OVER instead of CLICK it works fine. But I want it to work on the click. Any help would be greatly appreciated. Heres my code for the game_alien class:
    package  {
         import flash.display.MovieClip;
         import flash.events.MouseEvent;
         public class game_alien extends MovieClip{
              private var health = 2;
              public function game_alien() {
                   this.addEventListener(MouseEvent.CLICK, hit); //here is the problem
              private function hit(event:MouseEvent) {
                   trace(health);
                   health--;
                   if (health == 1){
                      this.gotoAndPlay("first_hit");
                   if (health == 0){
                        this.gotoAndPlay("end");
                        parent.removeChild(this);
    and i dont know if this would help anything but here is the code for the game class which encapsulates the game_alien object:
    package  {
         import flash.display.MovieClip;
         import flash.events.MouseEvent;
         import flash.ui.Mouse;
         public class game extends MovieClip{
              var new_cursor:Cursor = new Cursor();
              var alien:game_alien = new game_alien();
              public function game() {
                   changeCursor();
                   addAlien();
              private function addAlien(){
                   addChild(alien);
              public function changeCursor(){
                   Mouse.hide();
                   stage.addChild(new_cursor);
                   new_cursor.startDrag(true);
    Also it isnt throwing any errors or message to the output box (besides my trace statement ). Its kinda like due in two days two so any response on the idea is realllyyyyy apreciated thanks

    you're welcome.
    p.s.  in the future, mark helpful/correct answers, if there are any.

  • MouseEvent listening problem

    I have following problem to solve:
    I need to develop a custom dropdown component (forced using older jvm -on PPC) that has only java choice which is inadequate for purpose needed. I'm almost finished with it but i have problem with with this thing: i want for my popupmenu (custom made also) to catch all the mouse events for owner component (and its children) in order to hide the menu when they are clicked i.e. when you click on the dropdown a popupmenu is shown they you click somewhere on the panel (where dropdown resided) or some of it children, the popupmenu should disappear. So far so good, this is working nicely, I'm listening to the mouse events of these components in my popup but the problem is that if one of the children is a button it recives the clicked event too and performs the action for that button, while i would like for it to ignore that event if popup is visible. this is solved nicely in normal dropdown component (in swing or in awt choice) but i dont know how to do it without coding ugly stuff in all of the children components.
    Any tips??

    Torgil wrote:
    John_A_Nairn wrote:
    I was able to solve it myself. It turns out I had recently added a getParent() method to my JPanel subclass not realizing that this was overriding a Component method. It must be that the mouse event workings use this method to set the click location. Renaming my method solved the problem.This is the third post (that I've seen) in just the last month where a program has not worked because a Swing component was extended and a method from a superclass was overridden by mistake.
    When will people learn not to always automatically create a subclass of JPanel, JDialog, JFrame, etc as soon as they need a JPanel, JDialog, JFrame, etc?!? :-)I make that point all the time: don't subclass when you don't need to, and you only need to if you are overriding a method (and make sure you absolutely need to do that override!). Then people respond: I absolutely need to subclass JPanel (JFrame/JDialog/JInternalFrame) because it has-a JButton/JTextField/JLabel/... sigh So that's what "has-a" means these days?

  • Problem with MouseEvent

    this.addMouseListener(new MouseAdapter(){
               private void showIfPopupTrigger(MouseEvent mouseEvent) {
                    try {
                        l.write("22222222222");
                   } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                       if (popupMenu.isPopupTrigger(mouseEvent))
                            try {
                                  l.write("3333333");
                             } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                             String name = getValueAt(getSelectedRow(), 1).toString();
                             if(getValueAt(getSelectedRow(), 2).toString().equals("FOLDER"))
                             popupMenu.init(false,name);
                             else
                                  popupMenu.init(true,name);     
                             popupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
                     public void mousePressed(MouseEvent mouseEvent) {
                          try {
                                  l.write("000000000000000000000");
                             } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                             if (mouseEvent.getButton() == MouseEvent.BUTTON3) // BUTTON3 is the right mouse button
                                  try {
                                       l.write("11111111111111111111");
                                  } catch (IOException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                                  showIfPopupTrigger(mouseEvent);
                     public void mouseReleased(MouseEvent mouseEvent) {
                             showIfPopupTrigger(mouseEvent);
              });In the above given code, when I right click the mouse button, control reaches till l.write("22222222222");, but, it is not getting inside if (popupMenu.isPopupTrigger(mouseEvent)) , instead of that, control again goes to public void mousePressed(MouseEvent mouseEvent), what can be the reason?
    Rony

    I got the output with this.
    if (popupMenu.isPopupTrigger(mouseEvent))
                            int indRow = rowAtPoint(mouseEvent.getPoint());
                             getSelectionModel().addSelectionInterval(indRow, indRow);

  • Re: How to go about a problem with JFrame

    first before anything, I suggest you read the Sun Java Swing graphics and 2D graphics tutorials. They'll give you the answers you need including using JPanels, overriding the paintComponent method, etc...
    Next, try to separate your logic from your graphics. It will make updating and debugging a whole lot easier.
    Good luck.

    This is the approach I would (and did) take:
    Extend JPanel
    Have an ArrayList of Shape or any class implementing Shape
    Override paintComponent and iterate over the ArrayList to draw the shapes
    AddMouseListener which iterates over the ArrayList to detect whether any element contains() MouseEvent#getPoint()
    If one is found, take action accordingly. As per your OP, you probably want to perform two different actions alternately, could use a boolean flag for that.
    If (as was the case for me) the shapes may overlap, iterate upwards in the paintComponent and downwards in the MouseListener so that the first shape detected is the last drawn (the topmost). Break out of the iteration if you want only one shape to be detected.
    db

  • JTextArea problem: mouseClicked event irregular

    My application allows user to add several CalcArea objects (a subclass of JPanel) in a big JPanel. Highlight of ClassArea class is given below.
    Strangely, I find that once in a while, mouseClicked event is not fired. It is very critical for my user to select a particular CalcArea and modify/update the currently selected CalcArea. It is very much annoying to find that the mouseClicked event occurs irregularly. Please advise me how I can make sure mouseClicked event occurs when user clicks on the desired CalcArea. Thanks.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.math.*;
    import javax.swing.border.*;
    import java.awt.print.*;
    public class CalcArea extends JPanel implements MouseListener, FocusListener
       JTextArea ta;
       DrawPanel dp; //subclass of JPanel
       static int iCount=0;
       static int selectedMemberID=-1;
       int memberID;     //unique ID (0-based)
       //constructor
       public CalcArea(String strID....)
       }  //constructor ends
       public void mousePressed(MouseEvent e)
       } //method mousePressed ends
       public void mouseClicked(MouseEvent e)  //***Problem here
         selectedMemberID= memberID;
         System.out.print("mseClicked***");
       public void mouseReleased(MouseEvent e) {}
       public void mouseEntered(MouseEvent e) {}
       public void mouseExited(MouseEvent e) {}

    Is there any way to solve it? I don't really understand what you are trying to accomplish, but maybe somthing like the following will help:
    e.getComponent().getGraphics();
    e.getComponent().getParent().getGraphics();

  • Newbie help needed with Applet

    I am having a problem with the following Applet. I have highlighted it in bold.
    How can I ensure that all of the conditions are met regardless of the order they are inserted on the graph,within the program?
    At the moment I have to do them in the exact order they are in the code in order to get the "Winner" message.
    Also if I insert an image at any time at the point if(x >= 290 && x <= 350 && y >= 290 && y <= 350) I also receive the "Winner" message. I realise this happens because it is the last line of code in this statement.
    However I am very new to this and dont know how to fix it. Indeed I dont even know if this is the correct way to do this.
    Hope this makes sense.
    Any help would be very much appreciated
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class MyDraughts extends Applet implements MouseListener,ActionListener,
                   MouseMotionListener{
                        public MyDraughts(){
                             points = new ArrayList();
                             pawncount = 0;
                             pawncount1 = 14;
                             message = "";
                             message1 = "";
                             title = new Font ("Arial", Font.BOLD, 18);
         private Point square1, square2, square3, square4, square5, square6, square7,square8,square9,
         square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,
         square20,square21,square22,square23,square24,square25,square26,square27,square28,square29,
         square30,square31,square32,mouse;
         private int select;
         private Image pawn;
         private Point icon;
         private ArrayList points;
         private int x;
         private int y;
         private int pawncount;
         private int pawncount1,pawncount2;
         private Button undoButton;
         private String message,message1;
         private Font title;
         public void init(){
              setBackground(new Color(210,255,210));
              this.addMouseMotionListener(this);
              this.addMouseListener(this);
              select = 0;
              square1=new Point(50,10);
              square2=new Point(130,10);
              square3=new Point(210,10);
              square4=new Point(290,10);
              square5=new Point(10,50);
              square6=new Point(90,50);
              square7=new Point(170,50);
              square8=new Point(250,50);
              square9=new Point(50,90);
              square10=new Point(130,90);
              square11=new Point(210,90);
              square12=new Point(290,90);
              square13=new Point(10,130);
              square14=new Point(90,130);
              square15=new Point(170,130);
              square16=new Point(250,130);
              square17=new Point(50,170);
              square18=new Point(130,170);
              square19=new Point(210,170);
              square20=new Point(290,170);
              square21=new Point(10,210);
              square22=new Point(90,210);
              square23=new Point(170,210);
              square24=new Point(250,210);
              square25=new Point(50,250);
              square26=new Point(130,250);
              square27=new Point(210,250);
              square28=new Point(290,250);
              square29=new Point(10,290);
              square30=new Point(90,290);
              square31=new Point(170,290);
              square32=new Point(250,290);
              undoButton = new Button("Undo");
    add(undoButton);
    undoButton.addActionListener(this);
              mouse= new Point();
              icon = new Point();
              pawn = getImage(getCodeBase(), "pawn.gif");
    public void actionPerformed(ActionEvent actionevent){
    if(actionevent.getSource() == undoButton){
    int a = points.size();
    int b = a - 1;
    points.remove(b);
    a = points.size();
    repaint();
    public void paint (Graphics g){
         drawBox(g);
         g.fillRect(square1.x, square1.y,40,40);
         g.fillRect(square2.x, square2.y,40,40);
         g.fillRect(square3.x, square3.y,40,40);
         g.fillRect(square4.x, square4.y,40,40);
         g.fillRect(square5.x, square5.y,40,40);
         g.fillRect(square6.x, square6.y,40,40);
         g.fillRect(square7.x, square7.y,40,40);
         g.fillRect(square8.x, square8.y,40,40);
         g.fillRect(square9.x, square9.y,40,40);
         g.fillRect(square10.x, square10.y,40,40);
         g.fillRect(square11.x, square11.y,40,40);
         g.fillRect(square12.x, square12.y,40,40);
         g.fillRect(square13.x, square13.y,40,40);
         g.fillRect(square14.x, square14.y,40,40);
         g.fillRect(square15.x, square15.y,40,40);
         g.fillRect(square16.x, square16.y,40,40);
         g.fillRect(square17.x, square17.y,40,40);
         g.fillRect(square18.x, square18.y,40,40);
         g.fillRect(square19.x, square19.y,40,40);
         g.fillRect(square20.x, square20.y,40,40);
         g.fillRect(square21.x, square21.y,40,40);
         g.fillRect(square22.x, square22.y,40,40);
         g.fillRect(square23.x, square23.y,40,40);
         g.fillRect(square24.x, square24.y,40,40);
         g.fillRect(square25.x, square25.y,40,40);
         g.fillRect(square26.x, square26.y,40,40);
         g.fillRect(square27.x, square27.y,40,40);
         g.fillRect(square28.x, square28.y,40,40);
         g.fillRect(square29.x, square29.y,40,40);
         g.fillRect(square30.x, square30.y,40,40);
         g.fillRect(square31.x, square31.y,40,40);
         g.fillRect(square32.x, square32.y,40,40);
         g.setFont(title);
         g.drawString(message, 350, 300);
         g.drawString(message1, 350, 320);
         for(int i = 0; i < points.size(); i++){
    icon = (Point)points.get(i);
    g.drawImage(pawn, icon.x, icon.y, 25, 25, this);
         g.drawImage(pawn,175,135,25,25,this);
         g.drawImage(pawn,135,175,25,25,this);
         g.setFont(title);
         g.drawString("Click A Square To Place A Pawn", 350, 50);
    public void mouseDragged(MouseEvent e){}
    public void mouseMoved(MouseEvent e){}
    //require for the interface
    public void mousePressed(MouseEvent mouseevent){
    setBackground(new Color(210,255,210));
    x = mouseevent.getX();
    y = mouseevent.getY();
    if(x > 19 && x < 312 && y > 19 && y < 312){
    pawncount = pawncount + 1;
    points.add(mouseevent.getPoint());
    if(pawncount > pawncount1){
    setBackground(Color.red);
    message = "Too Many Pawns!You can only place 14";
    message1 = "Hit restart to play again";
    pawncount1 = 14;
    if(x >= 10 && x <= 50 && y >= 290 && y <= 330){
    setBackground(Color.red);
    message = "A Pawn Can't be placed here";
    pawncount = 15;
    if(x >= 50 && x <= 90 && y >= 250 && y <= 290){
    setBackground(Color.red);
    message = "A Pawn Can't be placed here";
    pawncount = 15;
    if(x >= 90 && x <= 130 && y >= 210 && y <= 250){
    setBackground(Color.red);
    message = "A Pawn Can't be placed here";
    pawncount = 15;
    //This is the solution for the puzzle
    if(x >= 90 && x <= 130 && y >= 10 && y <= 50);
    if(x >= 250 && x <= 290 && y >= 10 && y <= 50);
    if(x >= 90 && x <= 130 && y >= 50 && y <= 90);
    if(x >= 170 && x <= 210 && y >= 50 && y <= 90);
    if(x >= 10 && x <= 50 && y >= 90 && y <= 130);
    if(x >= 50 && x <= 90 && y >= 90 && y <= 130);
    if(x >= 250 && x <= 290 && y >= 130 && y <= 170);
    if(x >= 50 && x <= 90 && y >= 170 && y <= 210);
    if(x >= 210 && x <= 250 && y >= 210 && y <= 250);
    if(x >= 290 && x <= 350 && y >= 210 && y <= 250);
    if(x >= 10 && x <= 50 && y >= 250 && y <= 290);
    if(x >= 130 && x <= 170 && y >= 250 && y <= 290);
    if(x >= 210 && x <= 250 && y >= 290 && y <= 350);
    if(x >= 290 && x <= 350 && y >= 290 && y <= 350){
    setBackground(Color.green);
    message = "Winner";
    pawncount = 14;
    repaint();
    //required for the interface
    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void drawBox(Graphics g){
         for(int i=10;i<=350;i+=40){
    g.drawLine(i,10,i,330);
    g.drawLine(10,i,330,i);
    }

    Since your code is heavilly dependant on which square is clicked on I'd define a class extending JComponent to represent a square. You can make this an inner class, which would simplify access to other squares etc. Then lay these out in your JPanel, and each will have it's own paint method which will paint it with or without a pawn.
    At the same time you add all these as a two dimensional array for logical access. Each square has (or is) it's own mouseListener so that you can let awt figure out which you clicked on.
    private static final int CELL_SIZE=50;
    private static final int BOARD_SIZE= 8;
    private class Square extends JComponent implements MouseListener {
        public boolean hasPawn;
        private Square(int row, int col) {
             Rectangle where = new Rectangle(col * CELL_SIZE, row * CELL_SIZE,
    CELL_SIZE, CELL_SIZE);
            setBounds(where);
           addMousListener(this);
       public void addPawn() {
         if(!hasPawn) {
           hasPawn = true;
           repaint();
      public void paint(Graphics g) {
    } // end of inner class
    Square[][]board = new Square[BOARD_SIZE][BOARD_SIZE];}

  • Override the ToolTipText of the JTable with JButton ToolTipTex(pls see msg)

    Hi All,
    Could you please me to solve this problem?
    I have two Icons display on 2 JButtons (2 different ToolTipText for buttons). The Buttons are display on a JPanel. The Panel is display on one of the cell inside JTable. Is it possible that when I move the mouse over each Icon it will displays the ToolTipText of each of the JButton (I am try to replace the ToolTipText of the JTable with the ToolTipText of the JButton when mouse over and when I move the mouse to the next JButton I get a different ToolTipText). The reason I am using the JButton is that in the future I will add actionListener() to do something when the user click on it.
    I have try working with glassPane, addMouseMotionListener, MouseEvent.getPoint(), SwingUtilities.convertPoint, SwingUtilities.getDeepestComponentAt.
    Thank you in advance.
    Note: I am getting the Icons from Object [].

    Hello, the following is one ugly piece of code but I guess that's pretty much what you were after. Just test the tooltip and you'll see that it depends both on the cell and on which of the two buttons is hovered.import javax.swing.*;
    import javax.swing.table.TableCellRenderer;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    public class TestTooltipTable {
         private static class CustomTableCellRenderer implements TableCellRenderer {
              private JButton theLeftButton;
              private JButton theRightButton;
              private JPanel thePanel;
              public CustomTableCellRenderer() {
                   theLeftButton = new JButton("left action");
                   theRightButton = new JButton("right action");
                   thePanel = new JPanel(new GridLayout(1, 2)) {
                        public String getToolTipText(MouseEvent e) {
                             Point p = e.getPoint();
                             Rectangle leftRect = theLeftButton.getBounds();
                             Rectangle rightRect = theRightButton.getBounds();
                             if (leftRect.contains(p)) {
                                  return theLeftButton.getToolTipText();
                             } else if (rightRect.contains(p)) {
                                  return theRightButton.getToolTipText();
                             return " ";
                   thePanel.add(theLeftButton);
                   thePanel.add(theRightButton);
              public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                                        boolean hasFocus, int row, int column) {
                   String text = (String)value;
                   theLeftButton.setToolTipText("left action " + text);
                   theRightButton.setToolTipText("right action " + text);
                   return thePanel;
         public static void main(String[] args) {
              final JFrame frame = new JFrame(TestTooltipTable.class.getName());
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Object[][] data = {{"1", "A"}, {"2", "B"}, {"3", "C"}};
              String[] columnNames = {"Number", "Letter"};
              JTable table = new JTable(data, columnNames) {
                   public String getToolTipText(MouseEvent event) {
                        return super.getToolTipText(event);
              table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
              ToolTipManager.sharedInstance().registerComponent(table);
              frame.getContentPane().add(new JScrollPane(table));
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        frame.pack();
                        frame.show();
    }

  • Trouble dragging a window to parts of the screen

    Hi!
    I have a window which I would like to drag to different parts of the screen. I have tried doing so by using mouse dragg and redrawing it according to the coordinates obtained from the mouse drag event. The problem is that the window rarely moves to where I want to dragg it. Most of the time it goes to different locations on the screen. Can somebody suggest an alternative?
    Could there be something wrong with the way I am capturing the coordinates?Is there a better way of doing it?
    java.awt.event.MouseEvent event
    //this would be the code in the Dragg function
    Window.setLocation(event.getX(),event.getY())
    Thank you

    The event co-ordinates are relative to the top left of the window. They are not screen co-ordinates.
    try using :-
    Point p = mouseevent.getPoint();
    p.translate(this.getX(), this.GetY())
    Window.setLocation(p)I know other people have had niggles with Window.setLocation(...), and use Window.setBounds(...) instead.

  • Mouse click position on Glass Pane

    Hi,
    I'm working on a program following this example about Glass Panes:
    http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
    I'm having a problem whith code to detect the component I click on:
    Point glassPanePoint = e.getPoint();
    Container container = contentPane;
    Point containerPoint = SwingUtilities.convertPoint(
                                    glassPane,
                                    glassPanePoint,
                                    contentPane);
    if (containerPoint.y < 0) { //we're not in the content pane
        //Could have special code to handle mouse events over
        //the menu bar or non-system window decorations, such as
        //the ones provided by the Java look and feel.
    } else {
        //The mouse event is probably over the content pane.
        //Find out exactly which component it's over.
        Component component =
            SwingUtilities.getDeepestComponentAt(
                                    container,
                                    containerPoint.x,
                                    containerPoint.y);
    }I don't know if is e.getPoint(), SwingUtilities.convertPoint or SwingUtilities.getDeepestComponentAt, but
    getDeepestComponentAt doesn't return the component I click on. It returns the component which is a little bit down
    on screen.
    Anyone has the same problem and knows how to avoit it?
    Thanks

    The problem was frame borders. With this adjustments works properly:
    final Window window = SwingUtilities.getWindowAncestor(this);
    final Container container = getRootPane().getContentPane();
    final Point pWindow = window.getLocationOnScreen();
    final Point pContainer = container.getLocationOnScreen();
    final int iAdjustmentX = (int)pContainer.getX() - (int)pWindow.getX();
    final int iAdjustmentY = (int)pContainer.getY() - (int)pWindow.getY();
    final MouseEvent mouseEvent = (MouseEvent)event;
    final Point pMouse = mouseEvent.getPoint();
    final Point glassPanePoint = new Point((int)pMouse.getX() - iAdjustmentX, (int)pMouse.getY() - iAdjustmentY);

  • How to get the value of a specific field in JTable

    hi every one,,,
    i'm intersting to get a field of JTable
    how can i decide the row and the column of JTable???
    and in which type will be the field ??

    If you need the row number of a selected row
    table.getSelectedRow() should work.
    If you are using a mouse then
    Point p = MouseEvent.getPoint();
    int row = table.getRowAtPoint(p);
    int column = table.getColumnAtPoint(p);
    You can set the type of the field in
    TableModel.getColumnClass(int column) method.

  • How to handle an event on images

    precisely i have an image of hand , when i click a finger i want display an image from database

    You must add a MouseListener to the component the image is displayed on. In the mouseClicked handler you use MouseEvent.getPoint and check if it is within the boundaries of a finger.

  • How to get Mouse Screen Coordinates

    Hi
    I have code:
    JButton btn  = new JButton("button");
    btn.addActionListener(new ActionListener()
        public void actionPerformed(ActionEvent e)
              //  HEAR I WANT TO GET MOUSE
              //  SCREEN COORDS!!!
    }I want to get Coordinates!!!

    If you have a MouseEvent you can convert it as follows:
    Point point = mouseEvent.getPoint();
    SwingUtilities.convertPointToScreen((Component)mouseEvent.getSource(), point);However, that won't work from your actionPerformed method. If possible, use a MouseListener and intercept the mousePressed/mouseClicked events.
    If you want to capture the mouse position when someone presses a button (e.g, by using an keyboard shortcut) then you'll need a MouseMotionListener to track the mouse pointer as it moves over your component.
    Hope this helps.

  • About draging something with mouse

    when I press mouse and drag mouse ,I want to create a icon at the side of mouse cursor. When I drag the mouse cusor above a JTable or release mouse which cell or row would fire events.
    How to do it?

    When I drag the
    mouse cusor above a JTable or release mouse which
    cell or row would fire events.table.columnAtPoint(mouseEvent.getPoint())
    table.rowAtPoint(mouseEvent.getPoint())
    http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html

  • Need code help, please

    I am making a program where there are 3 ellipses. For two of the ellipses I want they to be able to be dragged across the screen when the mouse is pressed on them, they will also change color. For the third ellipse I want all three ellipses to move and change color when I click on it. I have the two ellipses doing what I want but I cant get a third move them all. Thanks.
    public interface Draggable
       public void setColor      (java.awt.Color aColor);
       public void setLocation   (int x, int y);
       public void mousePressed  (java.awt.event.MouseEvent XX);
       public void mouseDragged  (java.awt.event.MouseEvent XY);
       public void mouseReleased (java.awt.event.MouseEvent XZ);
    import wheels.users.*;
    public class DraggableSun extends Ellipse implements Draggable
       private java.awt.Point _lastMousePosition;
       public DraggableSun (int x, int y)
          super (java.awt.Color.YELLOW);
          _lastMousePosition = new java.awt.Point ();
          this.setLocation (x, y);
       public void mousePressed  (java.awt.event.MouseEvent mouseEvent)
          _lastMousePosition = mouseEvent.getPoint ();
          this.setColor (java.awt.Color.BLUE);
       public void mouseDragged  (java.awt.event.MouseEvent me)
          java.awt.Point currentPoint;
          int diffX, diffY;
          currentPoint = me.getPoint ();
          diffX = currentPoint.x - _lastMousePosition.x;
          diffY = currentPoint.y - _lastMousePosition.y;
          this.setLocation
             this.getXLocation() + diffX,
             this.getYLocation() + diffY
          _lastMousePosition = currentPoint;
       public void mouseReleased (java.awt.event.MouseEvent me)
          this.setColor (java.awt.Color.YELLOW);
    import wheels.users.*;
    public class Leader extends DraggableSun
       private MovableSun follower1;
       private MovableSun follower2;
       public Leader(int x, int y, MovableSun follower1, MovableSun follower2)
         super(x,y);
         this.follower1 = follower1;
         this.follower2 = follower2;
       public void mousePressed (java.awt.event.MouseEvent e)
         super.mousePressed (e);
         this.setFillColor (java.awt.Color.BLUE);
         //follower1.setFillColor (java.awt.Color.BLUE);
         //follower2.setFillColor (java.awt.Color.BLUE);
      // public void mouseDragged (java.awt.event.MouseEvent e)
         //super.mouseDragged(e);
       }Not sure if mouseDragged method in last class should be the same as the 2nd class, last class isnt finished, got lost.

    sorry, but I have gotten this far in the Leader class. It complies but all three don't move when drag the one. Any help would and I would be grateful.
    import wheels.users.*;
    public class Leader extends DraggableSun
       private DraggableSun _sun;
       private DraggableSun _sun1;
       private java.awt.Point _lastMousePosition;
       public Leader(int x, int y, MovableSun follower1, MovableSun follower2)
         super(x,y);
         this._sun = _sun;
         this._sun1 = _sun1;
       public void mousePressed (java.awt.event.MouseEvent e)
         super.mousePressed (e);
         this.setFillColor (java.awt.Color.BLUE);
         _sun.setFillColor (java.awt.Color.BLUE);
         _sun1.setFillColor (java.awt.Color.BLUE);
       public void mouseDragged (java.awt.event.MouseEvent e)
         super.mouseDragged(e);
         java.awt.Point currentPoint;
            int diffX, diffY;
            currentPoint = e.getPoint ();
            diffX = currentPoint.x - _lastMousePosition.x;
            diffY = currentPoint.y - _lastMousePosition.y;
            this.setLocation
             this.getXLocation() + diffX,
             this.getYLocation() + diffY
         _sun.setLocation
               _sun.getXLocation() + diffX,
            _sun.getYLocation() + diffY
            _sun1.setLocation
               _sun1.getXLocation() + diffX,
               _sun1.getYLocation() + diffY
         _lastMousePosition = currentPoint;
       }

Maybe you are looking for

  • Tiger Upgrade

    I am looking to purchase a full disk set Tiger upgrade...I have a PowerMac G5 Panther 10.3.9...from what I gather...I have to purchase a disk set designed for "PowerMac"...as opposed to a set that is designed for "eMac"..."Mac Mini"...etc...is that t

  • Solution Manager DB was installed in 'Simple mode'

    We are just a couple months into our first SAP installation.  I just received this message from my hardware/server guy: "Please be aware that the Solution Manager DB was installed in “Simple mode.”  This means that we don’t have transaction logs, and

  • Dreamweaver doesn't see my template anymore

    I honestly searched this forum for my issue but not having any luck. I need to edit a Dreamweaver site. I created it using a template (.dwt) and it has never given me any trouble until now. For some reason when I edit the template and save the file,

  • Import music from another program

    I have some music that I purchased from Sony to use with the crummy MP3 player that my grandma last year. Is it possible to transfer the music that I purchased from their online store into my iTunes library? The songs are all listed as OpenMG Audio F

  • Does CS3 support Canon 7D?

    I upgraded my camera to Canon 7D, now CS3 and Lightroom 1 will not open 7D images. Camera Raw 4.3 won't support the 7D raw images. Must I upgrade Photoshop and Lightroom?