Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  mouse event not triggering  (Read 213 times)
0 Members and 1 Guest are viewing this topic.
Offline dav0012

Senior Newbie





« Posted 2014-04-19 11:57:20 »

Hi all, small question Quick response is required plzz. pardon if its a basic question, am new to programming and want to stick to good programming rule as much as possible.

Question:
i have a JFrame which contain a JScrollPane which contain a JPanel as viewportView.
i have implemented the JPanel with MouseListener and MouseMotionListener with the code shown below to chk if the event Fires but no response. (i also tried it on the JScrollPane)


where should i have put the MouseListener and MouseMotionListener Huh??
plz help, its urgent.

code to fire mouse action :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
@Override
public void mouseClicked(MouseEvent event){
   System.out.println(event.getPoint());
   
   }//close mouseClicked  

@Override
   public void mousePressed(MouseEvent event){
     
      System.out.println(event.getPoint());
         }//close mouse pressed

@Override
public void mouseMoved(MouseEvent event){}//close mouseMoved

@Override
public void mouseDragged(MouseEvent event){}//close mouseDragged

@Override
public void mouseExited(MouseEvent event){}//close mouseDragged

@Override
public void mouseReleased(MouseEvent event){}//close mouseDragged

@Override
public void mouseEntered(MouseEvent event){}//close mouseDragged


code for initialisation :
P.S:
PuzzlePanel is a class which extends JPanel
PuzzleScrollPane is a class which extends JScrollPane

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public class PuzzlePane extends JFrame{

   
   ImageData puzzleImageData;
   Dimension gameSetting = new Dimension();
   protected PuzzlePanel pp ;
   protected PuzzleScrollPane jsp ;
   protected Gallery gDialog ;
   protected Dimension dimDesktop ;
   public PuzzlePane(){
     
      initUI();
     
     
   }//close public declaration
 
   public void initUI(){
   
    dimDesktop = Toolkit.getDefaultToolkit().getScreenSize();
   this.setSize(dimDesktop);
   this.setPreferredSize(this.getSize());
   pp = new PuzzlePanel();
   jsp = new PuzzleScrollPane(pp);
   
   jsp.setBorder(new BevelBorder(1));
   jsp.setBackground(Color.cyan);
   jsp.setOpaque(false);
   jsp.getViewport().setOpaque(false);
   jsp.setSize(new Dimension(500,500));
   /*jsp.getVerticalScrollBar().setValue(0);
   jsp.getHorizontalScrollBar().setValue(0);*/

   this.add(jsp);
   this.pack();
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setLocationRelativeTo(null);
   this.setVisible(true);
   }//close initUI
 
Offline Herjan
« Reply #1 - Posted 2014-04-19 13:15:01 »

You should put the Mouse(Motion)Listener in the (constructor of) JPanel (PuzzlePanel in this case)

setMouse(Motion)Listener(Mouse(Motion)Listener listener);

Offline dav0012

Senior Newbie





« Reply #2 - Posted 2014-04-19 14:10:35 »

You should put the Mouse(Motion)Listener in the (constructor of) JPanel (PuzzlePanel in this case)

setMouse(Motion)Listener(Mouse(Motion)Listener listener);

u Rock  Grin Grin Grin
thanks , the 2nd line made my eye spin for a few mins but i understood what u meant.
it seemed that when i implement MouseListener to the JPanel it doesnt ADD the listener by itself.
so i added an inner class which extends MouseAdapter (with all the event code), then set the listener in the constructor of the jPanel
it did the wrk Smiley Smiley Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Herjan
« Reply #3 - Posted 2014-04-19 15:46:10 »

it seemed that when i implement MouseListener to the JPanel it doesnt ADD the listener by itself.
so i added an inner class which extends MouseAdapter (with all the event code), then set the listener in the constructor of the jPanel
it did the wrk Smiley Smiley Smiley

Since you implemented the MouseListener to the JPanel itself, you could just have done this:
addMouseListener(this);

But I'm glad you were able to fix it Smiley

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!