I've added a AncestorListener to my component and found the following problem:
Everytime when the component is shown, the method ancestorAdded(AncestorEvent event) is called twice. Once the event is fired from the method AncestorNofifier.componentShown and once from the method AncestorNotifier.propertyChange.
addAncestorListener(new AncestorListener() {
#Override
public void ancestorAdded(AncestorEvent event) {
//do sth.
}
#Override
public void ancestorRemoved(AncestorEvent event) {
//do sth.
}
#Override
public void ancestorMoved(AncestorEvent event) {
//not used
}
});
Does anyone know how to prevent one of these envents?
Thanks.
Related
I'm trying to add a SelectionListener<TreeItem> on my Tree using the addSelectionHadler() method.
For my proof on onSelection(SelectionEvent<TreeIterm> event) I put a simple Windows.alert() but it don't do anything: when I select a treeItem that color change but doesn't open the window.
I write the Handler but if you want more code tell me.
Thank you.
class SelHand implements SelectionHandler<TreeItem> {
#Override
public void onSelection(SelectionEvent<TreeItem> event) {
Window.alert(event.getSelectedItem().getText());
}
}
SelHand selezionatore = new SelHand();
tree.addSelectionHandler(selezionatore);
Use the straightforward:
tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
#Override
public void onSelection(SelectionEvent<TreeItem> event) {
Window.alert(event.getSelectedItem().getText());
}
});
I have JTextField. I need to save the changes, if user writes something in it and then lost the focus(like click some where else)
mMaxLabelLength = new JTextField();
mMaxLabelLength.addActionListener(this);
public void focusGained(FocusEvent fe)
{
System.out.println("4");
mMaxLabelLength.addActionListener(this);
}
#Override
public void focusLost(FocusEvent fe)
{
System.out.println("5");
mMaxLabelLength.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Do something
}
The problem is I am not able to call "actionPerformed" from "focusLost/focusGain". I need to keep the "actionPerformed" as separate method as I am calling it from another places also.
So, you want to do exactly the same thing when the focus is lost as what you're already doing in actionPerformed(), right, right. So, do just that:
#Override
public void focusLost(FocusEvent fe) {
doSomething();
}
public void actionPerformed(ActionEvent e){
doSomething();
}
private void doSomething() {
// ...
}
Why at first works chartMouseClicked (JFreeChart library), and already then mouseClicked?
boolean isDoubleClicked = false;
chartPanel.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent me) {
if (me.getClickCount() == 2 &&) {
isDoubleClicked = true;
}
}
#Override
public void mousePressed(MouseEvent me) {}
#Override
public void mouseReleased(MouseEvent me) {}
#Override
public void mouseEntered(MouseEvent me) {}
#Override
public void mouseExited(MouseEvent me) {}
});
chartPanel.addChartMouseListener(new ChartMouseListener() {
#Override
public void chartMouseClicked(ChartMouseEvent cme) {
if (isDoubleClicked)
System.out.println("Double clicked!");
}
#Override
public void chartMouseMoved(ChartMouseEvent cme) {}
});
So, System.out.println("Double clicked!"); not works. How to correct it?
You have two different listener objects here, one is a MouseListener instance (that listens to mouse events on the panel) and the other is a ChartMouseListener instance (that listens to mouse events on the chart in the panel). They are registered in separate listener lists, and the isDoubleClicked field from one object isn't visible to the other object.
The reason that ChartMouseListener is separate from MouseListener is that JFreeChart creates its own events that contain additional information about the entity in a chart that is "underneath" the mouse pointer.
To add up on #DavidGilbert, you can also use ChartMouseEvent.getTrigger().getClickCount() to detect double-click in the chart.
I have a JFace dialog which contains SWT Text and a button . Initially when the dialog is opened the button should be disabled, and when I click on the Text and as long as the caret position of the Text is visible button should be enabled.
These are the listeners i am using :
text.addMouseListener(new MouseListener()
{
#Override
public void mouseDoubleClick(MouseEvent arg0)
{
}
#Override
public void mouseDown(MouseEvent arg0)
{
}
#Override
public void mouseUp(MouseEvent arg0)
{
testButton.setEnabled(true);
}
});
text.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent arg0)
{
testButton.setEnabled(false);
}
#Override
public void focusGained(FocusEvent arg0)
{
}
});
Am I using the appropriate listeners? Please suggest
If I understood you correctly, this should be what you want:
button.setEnabled(false);
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
button.setEnabled(false);
}
});
text.addListener(SWT.FocusIn, new Listener()
{
#Override
public void handleEvent(Event e)
{
button.setEnabled(true);
}
});
Initially, the Button is disabled. It will be enabled once the Text gaines focus. The Button will be disabled again after it was pressed.
cmbCategory.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//Do things
}
});
The above code works just fine when I make selection using the control itself, but does not fire when I try changing the index programmatically.
Can anyone help me?
Maybe this could work:
cmbCategory.addModifyListener(new ModifyListener(){
#Override
public void modifyText(ModifyEvent event) {
//Do things
}
});