Solved

MouseListener for a JPanel painted on another JPanel

Posted on 2004-09-02
13
633 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 70
What's wrong with this code? 4 34
Github api to fetch the number of collaborators in a git repo 1 27
How to fix  socket closed error 11 26
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

762 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