Skip to main content

Some help with Swing and Mouse event listeners

4 replies [Last post]
etabeta
Offline
Joined: 2006-02-17

Hi, is there someone can help me?

Background:
1) i wrote a class that extends MouseListener and MouseMotionListener.
2) I added that class to a JPanel with addMouseListener() AND addMouseMotionListener().
Plateform: jdk 1.4.2_05-b04, Linux Fedora os .

Troubles:
a) if mousePressed() gives a value to a class property, in mouseReleased() I can read the right property value, but in mouseDragged() i can't !!!
b) when in mouseReleased() i call jpanel.removeMouseListener(this) AND jpanel.removeMouseMotionListener(this) , the class continues to listen the MouseMotionEvents of jpanel.

Why?
Thanks for aid ...
Bye

<br />
class Op_Adapter implements MouseListener, MouseMotionListener{<br />
  /** Invoked when the mouse button has been clicked (pressed and released) on a component.<br />
  */<br />
  public void mouseClicked(MouseEvent e){}<br />
  // Invoked when the mouse enters a component.<br />
  public void mouseEntered(MouseEvent e){}<br />
  // Invoked when the mouse exits a component.<br />
  public void mouseExited(MouseEvent e){}<br />
  // Invoked when a mouse button has been pressed on a component.<br />
  public void mousePressed(MouseEvent e){}<br />
  // Invoked when a mouse button has been released on a component.<br />
  public void mouseReleased(MouseEvent e) {}<br />
  // Invoked when a mouse button is pressed on a component and then dragged.<br />
  public void mouseDragged(MouseEvent e){}<br />
  // Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.<br />
  public void mouseMoved(MouseEvent e){}<br />
}<br />
/** Ascoltatore del QPanel per il comando erase<br />
 */<br />
class Op_Erase extends Op_Adapter{<br />
  TexturePaint cross;<br />
  BufferedImage bi;<br />
  JComponent qpanel;<br />
  int stx, sty, last_x, last_y, prova;<br />
  //* Costruttore<br />
  public Op_Erase(JComponent qpanel,BufferedImage bim){<br />
    this.qpanel = qpanel;<br />
    this.bi = bim;<br />
  }<br />
  public void mousePressed(MouseEvent e){<br />
    //    qpanel.setDoubleBuffered( false );<br />
    Graphics2D g2 = this.bi.createGraphics();<br />
    this.stx = e.getX();<br />
    this.sty = e.getY();<br />
    last_x = this.stx;<br />
    last_y = this.sty;prova=11;<br />
    System.out.println("Premuto"+stx+sty+"-y"+prova);<br />
  }<br />
  public void mouseDragged(MouseEvent e){<br />
    update(e);<br />
  }<br />
  private void update(MouseEvent e){<br />
    Graphics2D g2 = this.bi.createGraphics();<br />
    g2.setXORMode(qpanel.getBackground());<br />
    g2.setColor(Color.BLACK);<br />
    if (last_x!=this.stx) {<br />
      g2.drawRect(this.stx,this.sty,last_x-this.stx,last_y-this.sty);<br />
      System.out.println("+"+stx+sty+"-"+last_x+last_y+"-"+prova);<br />
    } // end of if ()<br />
    last_x = e.getX();<br />
    last_y = e.getY();<br />
    g2.drawRect(stx,sty,last_x-stx,last_y-sty);<br />
    //    g2.dispose();<br />
    qpanel.repaint();<br />
  }<br />
  public void mouseReleased(MouseEvent e) {<br />
    Graphics2D g2 = this.bi.createGraphics();<br />
    g2.setPaint(Color.white);<br />
    g2.fillRect(stx, sty, e.getX()-last_x, e.getY()-last_y);<br />
    g2.dispose();<br />
    qpanel.removeMouseMotionListener(this);<br />
    qpanel.removeMouseListener(this);<br />
    qpanel.repaint();<br />
  }<br />
}<br />
public class QPanel extends JScrollPane implements MouseListener, MouseMotionListener{<br />
  Rectangle rect = new Rectangle(0, 0, 100, 50);<br />
  BorderMappedRectangle bmapRec;<br />
  BufferedImage bi;<br />
..........................<br />
// to generate the menus<br />
  public Object[] getMenu(){<br />
    if (jmenus == null) {<br />
      //** Listener of command menu. It has to register temporary Mouse event listener to this QPanel.<br />
      ActionListener menuListener = new ActionListener(){<br />
	  public void actionPerformed(ActionEvent e){<br />
	    if (e.getActionCommand().equals(commandLabels[ERASE])) {<br />
	      QPanel.this.addMouseListener(new Op_Erase(QPanel.this,QPanel.this.bi));<br />
	      QPanel.this.addMouseMotionListener(new Op_Erase(QPanel.this,QPanel.this.bi));<br />
	    }<br />
	  }<br />
	};<br />
.........<br />

Message was edited by: shan-man (formatted code)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
thetan
Offline
Joined: 2004-08-19

Hi etabeta,
I believe the answer is in that lines:
QPanel.this.addMouseListener(new Op_Erase(QPanel.this,QPanel.this.bi));
QPanel.this.addMouseMotionListener(new Op_Erase(QPanel.this,QPanel.this.bi));
Seems you are using two different Op_Erase instances and there is nothing strange that you are able to operate with class properties within mouseClicked/mouseReleased and can't in mouseDragged as they belongs to a different objects, right? It's about a) question.
It also likely that the reason of problem b) is somewhere there too.
Good luck,
--Andrei

Crowgol
Offline
Joined: 2006-02-14

I have the same problem, when I use DragMouseAdapter that I extend it from MouseAdapter, the mouseDragged even does not work, other events such as mouseclicked and mousePrssed are working but the mouseDragged is not working. who can tell me why?

thetan
Offline
Joined: 2004-08-19

Hi Crowqol,
this is because MouseAdapter doesn deal with
clicked, entered, released, pressed and exited events:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseAdapter.html
To catch drag events use MouseMotionAdapter as well.

shan-man
Offline
Joined: 2006-02-17

Hi,

Just wanted to let you know that I've moved this post from the "Forum Feedback & Suggestions" forum to the appropriate "Swing & AWT" forum.

Regards,
Shannon