Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MouseListener for a JPanel painted on another JPanel

Posted on 2004-09-02
13
Medium Priority
?
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

705 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