Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MouseListener for a JPanel painted on another JPanel

Posted on 2004-09-02
13
Medium Priority
?
652 Views
Last Modified: 2013-11-23
I have a JPanel painted on another JPanel and I can't get the mouseevents to work for the first JPanel.  Essentially, what I have is:

public class class1 extends JPanel implements MouseListener {
      Color c;

     public class1() {
          c=Color.RED;
          addMouseListener(this);
     }

     public void paintComponent(Graphics g) {
          g.setColor(c);
          g.drawLine(0,0,100,100);
     }

     public void mouseClicked(MouseEvent me) {
         System.out.println("Mouse Clicked");
         c=Color.blue;
         repaint();
      }

     //... other mouse events, etc.

}  // end class1

public class2 extends JPanel {
     class1 c1;

     public class2() {
         c1=new c1();
     }

     public void paintComponent(Graphics g) {
        super.paintComponent(g);
        c1.paint(g);
     }

     public static void main(String args[]) {
        JFrame f = new JFrame();
        f.getContentPane().add(new class1());
        f.pack();
        f.setVisible(true);
     }
}


When this is run, it would draw a red line from the upperleft corner.  When that line is clicked (or eventually otherwise interacted with), I want to be able to manipulate it - in this example simple change the color.  The mouse events aren't being fired.  Any help with this would be appreciated.
0
Comment
Question by:pittbb
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11965785
Try removing

>>f.pack();
0
 
LVL 35

Expert Comment

by:girionis
ID: 11965804
I just ran it and it is working fine (the line becomes blue). I am on windows 2000 with jdk 1.4.1._05
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 440 total points
ID: 11965826
Actually your code:

>>
        JFrame f = new JFrame();
        f.getContentPane().add(new class1());
        f.pack();
        f.setVisible(true);
>>

does in fact work as is
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:pittbb
ID: 11965847
Removing f.pack() had the same result for me (i.e. no result).  I am on winXP with jdk1.4.2_01.  I am running it in JBuilderX.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11965971
Can you try running it on a different os?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11966063
Are you sure you're running the code you *think* you're running?
0
 
LVL 2

Author Comment

by:pittbb
ID: 11966187
I tried this on Win2000 with no luck.  I'm not running the exact code I posted, I tried to simplify it to just the parts relevant to my question, but it may be something else that I haven't posted that is causing the problem.  I'll try running just the code I posted and see if that works.  Thanks for your patience.
0
 
LVL 2

Author Comment

by:pittbb
ID: 11966266
Well, I got the code I posted to "work" in the sense that it turns blue, etc.  I still have to figure out what in my code is causing it not to work.  But it turns blue anywhere you click within the frame, is there a way to have it only turn blue if you click on the line itself?
0
 
LVL 35

Accepted Solution

by:
girionis earned 460 total points
ID: 11966383
You have to be checking for the coordinates of the line. If the mouse position matches them then paint it blue.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11967248
As for the line not turning blue in your actual code: Are you sure the panel with the line is updating? Try popping in a JOptionPane.showMessageDialog after the line's color is changed to see if it updates then. The issue may be that you have two threads running and your "gui" thread is waiting for your "main" thread. I had a similar issue using JProgress bars... (Credit to objects and girionis for teaching me that ;) ).

If your program updates when the dialog is displayed, it means you'll need to run the the "main" process in another thread. You'll need to create a new Thread object:
Thread t = new Thread(new Runnable(){
public void run(){
//Call any methods you'll want to run in here
}
});
t.start();
0
 
LVL 2

Author Comment

by:pittbb
ID: 11967373
Well, I solved it.  I don't know why the code I posed above works, and my actual program doesn't.  To fix it, I added c1 (instance of class1) as a mouseListener for class2.


     public class2() {
         c1=new c1();
         addMouseListener(c1);
     }


I tried the message dialog, but it just confirmed that the click event wasn't firing.  To follow up on girionis's comment about checking the coordinates, in case anyone is interested compare the slopes of the line with the slope of line formed by the mouse position and a point on the line to see if they're equal.  Also restrict the point to be between the min/max x/y coordinates of the endpoints if you want it to happen only for the line segment.

I'll go ahead and do the points, but if anyone has any ideas why I had to add a mouse listener to class2 in my code and not in the simplified code I posted, I'd be interested.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11968527
>>but if anyone has any ideas why I had to add a mouse listener to class2

I can only think that the reason is that you were actually clicking on class2 (which needed a listener) when you thought you were clicking on class1
0
 
LVL 35

Expert Comment

by:girionis
ID: 11970579
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

885 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