Solved

MouseListener for a JPanel painted on another JPanel

Posted on 2004-09-02
13
612 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 110 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 115 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 55
topping1 challenge 7 53
Systems talking to each other 5 110
maven project error 5 24
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now