qasinformatik
asked on
Forwarding an event in java
Hello Experts,
I need some help with event handling in Java.
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEven t e) {
QMessageButton userButton = (QMessageButton)e.getSourc e();
String messageNo = userButton.getMessageNo();
System.out.println("Userbu tton 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?
I need some help with event handling in Java.
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEven
QMessageButton userButton = (QMessageButton)e.getSourc
String messageNo = userButton.getMessageNo();
System.out.println("Userbu
// 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?
you can't, you'd instead add your parent also as a listener.
ASKER
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
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
If you want to get the coordinates on the button in terms of the JTextPane, add a MouseListener to the button and use
SwingUtilities.convertMous eEvent(Com ponent source, MouseEvent sourceEvent, Component destination)
SwingUtilities.convertMous
>> 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.
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.
> 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?
you can just use button.getLocation() in that case.
why have you get a button inside your text pane?
ASKER
@ 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.convertMous eEvent(Com ponent source, MouseEvent sourceEvent, Component destination)
--> didn't work
button.addMouseListener(ne w MouseListener(){
public void mouseClicked(MouseEvent e){
QMessageButton userButton = (QMessageButton)e.getSourc e();
String messageNo = userButton.getMessageNo();
System.out.println("Userbu tton klicked!" + messageNo);
SwingUtilities.convertMous eEvent(use rButton, e, MessageMask.getInstance(). getMessage sJTP());
Point p = e.getPoint();
System.out.println("DelBut ton-Locati on: " + p.getX() + ", " + p.getY());
--> I get the coordinates inside the button instead of the coordinates inside the messagesJTP
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.convertMous
--> didn't work
button.addMouseListener(ne
public void mouseClicked(MouseEvent e){
QMessageButton userButton = (QMessageButton)e.getSourc
String messageNo = userButton.getMessageNo();
System.out.println("Userbu
SwingUtilities.convertMous
Point p = e.getPoint();
System.out.println("DelBut
--> I get the coordinates inside the button instead of the coordinates inside the messagesJTP
> 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.
>
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.
>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CEHJ
Thanks for your solution. It worked fine.
Thanks for your solution. It worked fine.
:-)
easier to just add the listener to the parent, then no conversion is necesary
and also less coupling.
and also less coupling.
Forward? What do you want to do with it? You can just pass 'e' to another method