Reputation: 3578
Let me describe little bit whats my situation,
I have SWT Text
in my Java SE SWT application:
let say USER will write something (String
) to the created Text
while app running. I would like to introduce some MouseListener
which would do exactly this:
When USER would click into Text
object/widget, this Text
would clear(setText("");
) itselves(if there was previously written some String).
MouseListener
has 3 methods: mouseDown(...)
, mouseUp(..)
, mouseDoubleClick(...)
-> I should use onlymouseDown(...)
in this case - nothing more isnt nescesary.
In mouseDown(..)
method I would need to call method of actual Text object reference : "XY".setText("");
Text which was clicked-into by mouse. -> This I somehow could not obtain.
I would like to have it somehow like general MouseListener onMouseClickText
which could be applied to any SWT Text
I will use in my app.
Does anybody know how to do such MouseListener
or better : how to obtain this reference to existing clicked SWT Text
, inside MouseListener
?
here is example code:
package sk.tokra.example;
//imports here
.
.
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.
.
public class Application {
//class fields
Text text;
private static final Logger logger = LoggerFactory.getLogger(Application.class);
.
.
.
// main
public static void main(String[] args) {
logger.debug("main(), Starting app!");
try {
Application window = new Application();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
logger.debug("main(), Exiting app!");
return;
}
/**
* Open the window.
*/
public void open() {
logger.debug("open()");
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
text = new Text(shell, SWT.BORDER);
text.addMouseListener(onMouseClickText);
.
.
.
}
//other stuff/methods/listener part
private MouseListener onMouseClickText = new MouseListener() {
@Override
public void mouseUp(MouseEvent arg0) {
logger.debug("onMouseClickFind, mouseUp()");
}
@Override
public void mouseDown(MouseEvent arg0) {
logger.debug("onMouseClickFind, mouseDown()");
// HERE I WOULD NEED to obtain refence of Text
// then call .setText("");
}
@Override
public void mouseDoubleClick(MouseEvent arg0) {
logger.debug("onMouseClickFind, mouseDoubleClick()");
}
};
}
Upvotes: 0
Views: 3348
Reputation: 3241
As Naveen suggested it is much better to you use a focus listener because the same text widget can receive focus through numerous different ways (like tabbing on keyboard). You might want to handle that case as well.
In any case, whether you use a FocusListener
or MouseListener
on the Text
, you can fetch the widget which actually caused the event. In case of the mouse listener you receive an instance of MouseEvent
and you can use MouseEvent#widget
to access the Text control. Your code would look something like below in the listener implementation:
//other stuff/methods/listener part
private MouseListener onMouseClickText = new MouseListener() {
@Override
public void mouseUp(MouseEvent arg0) {
((Text) arg0.widget).setText("");
}
...
}
You may add the same listener instance to multiple Text
widgets, it should work.
Upvotes: 1