Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding mouselistener to jframe

843807Apr 21 2010 — edited Apr 21 2010
I'm sure I'm just forgetting something simple but I've looked up several examples that use code similar to mine but can't get the mouse listeners to work. I haven't done GUI stuff in awhile.
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class GraphViewer extends JFrame implements MouseMotionListener,MouseListener
{
    private String myImageFile;
    
    public GraphViewer(String image)
    {
        myImageFile = image;
        initBackground();
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        this.pack();
        this.setVisible(true);
    }
    
    private void initBackground()
    {
        JPanel background = new JPanel();
        ImageIcon imgi = new ImageIcon(myImageFile);
        JLabel imgl = new JLabel(imgi);
        background.add(imgl);
        JScrollPane scroll = new JScrollPane(background,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.getContentPane().add(scroll);
    }

    @Override
    public void mouseDragged(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseMoved(MouseEvent arg0)
    {
        System.out.println(arg0.getX()+","+arg0.getY());
        
    }

    @Override
    public void mouseClicked(MouseEvent arg0)
    {
        System.out.println("Clicked at "+arg0.getX()+","+arg0.getY());
        
    }

    @Override
    public void mouseEntered(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mousePressed(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2010
Added on Apr 21 2010
4 comments
1,643 views