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
Solved

Forwarding an event in java

Posted on 2006-06-14
12
411 Views
Last Modified: 2011-04-14
Hello Experts,

I need some help with event handling in Java.

button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
      QMessageButton userButton =  (QMessageButton)e.getSource();
      String messageNo = userButton.getMessageNo();
      System.out.println("Userbutton klicked!" + messageNo);
                // i want to forward the event to the JTextPane in which the QMessageButton is
}
});


--> how do I simply forward all events from the Button to its parent after handling them in Button?
0
Comment
Question by:qasinformatik
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900583
>>how do I simply forward all events from the Button to its parent after handling them in Button?

Forward? What do you want to do with it? You can just pass 'e' to another method
0
 
LVL 92

Expert Comment

by:objects
ID: 16900584
you can't, you'd instead add your parent also as a listener.
0
 

Author Comment

by:qasinformatik
ID: 16900609
My parent (the JTextPane) has a MouseListener.

It is called everywhere - but not if I click the Button.
I want that the ActionPerformed of the Button is called and then the mouseClicked of the JTextPane is called.

The reason why I wanted to do that is that I need to set the caretPosition in the JTextPane to where the Button is placed in the JTextPane.
But when I try to find out the position of the Button in the JTextPane I always get (0/0) for the x- and y-coordinate.

so I created a mouseListener for the JTextPane where I get the position in the JTextPane where the user clicked --> I can find out the Position and set the CaretPosition
--> thats why I need to forward the event to the JTextPane even when the Button is clicked
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16900628
If you want to get the coordinates on the button in terms of the JTextPane, add a MouseListener to the button and use

SwingUtilities.convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16900637
>> after handling them in Button

So the parent also needs to handle the event? In that case, it should perhaps also register itself as an action-listener for the button.
0
 
LVL 92

Expert Comment

by:objects
ID: 16900649
> The reason why I wanted to do that is that I need to set the caretPosition in the JTextPane to where the Button is placed in the JTextPane.

you can just use button.getLocation() in that case.
why have you get a button inside your text pane?

0
 

Author Comment

by:qasinformatik
ID: 16900680
@ mayankeagle
how can it register itself as an action-listener for the button?
can u give a code-example?

@objects:
button.getLocation() returns 0/0 --> no idea why.. perhaps a special layout

--> its a chat application --> i need a button inside the text pane to delete the specific message (works already)

@CEHJ
SwingUtilities.convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
--> didn't work
button.addMouseListener(new MouseListener(){
      public void mouseClicked(MouseEvent e){
            
      QMessageButton userButton =  (QMessageButton)e.getSource();
      String messageNo = userButton.getMessageNo();
      System.out.println("Userbutton klicked!" + messageNo);
      SwingUtilities.convertMouseEvent(userButton, e, MessageMask.getInstance().getMessagesJTP());
      Point p = e.getPoint();
      System.out.println("DelButton-Location: " + p.getX() + ", " + p.getY());
--> I get the coordinates inside the button instead of the coordinates inside the messagesJTP
0
 
LVL 92

Expert Comment

by:objects
ID: 16900724
> button.getLocation() returns 0/0 --> no idea why.. perhaps a special layout

because thats where it is located I'd say :)
the corrds are relative to its immediate parent

to get the coords relative to another component then use

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html#convertPoint(java.awt.Component,%20java.awt.Point,%20java.awt.Component)

> --> its a chat application --> i need a button inside the text pane to delete the specific message (works already)

Doesn't need to be inside the text pane to achiev e that, it can be anywhere in your hierarchy.


0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 16900730
You need to do

MouseEvent converted = SwingUtilities.convertMouseEvent(userButton, e, MessageMask.getInstance().getMessagesJTP());

, assuming the third parameter is correct. It should be the parent component of your button
0
 

Author Comment

by:qasinformatik
ID: 16900781
CEHJ

Thanks for your solution. It worked fine.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900789
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16900896
easier to just add the listener to the parent, then no conversion is necesary
and also less coupling.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Weekend adv creator 3 51
Spring Framework HTTPSession management 1 45
use lov values 2 61
How to determine if a string is a valid SHA value 7 35
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

840 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