GWT Tree: selection Listener - java

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());
}
});

Related

JavaFX button not responding on first click

public void handle(){
submit.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
LoginConnection login = new LoginConnection();
boolean pass = login.login(usernameField.getText(), passwordField.getText());
if(pass)
flip(SceneNames.Main);
else
invalLoginMessage.setOpacity(1.00);
}
});
register.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
flip(SceneNames.Register);
}
});
}
When i click on submit or register, it takes two click for it to do anything. How do i fix this?
What happens is that on first click it adds the handlers specified in the method and on second and consecutive clicks, it uses the handlers. To fix it just create separate methods to add through fxml or scene builder.

Why the suggestion list remains visible in GWT

I am working with suggestion box in gwt and added SelectionHandler and onFocusHandler to the suggestion box but after selection the suggestion list remains visible.. Which should not be the case.
What should I do to hide the suggestion list after selecting one of the suggestion.?
suggestBox.getValueBox().addFocusHandler(new FocusHandler() {
#Override
public void onFocus(FocusEvent event) {
if(suggestBox.getText().equals("")){
suggestBox.setText(" ");
suggestBox.showSuggestionList();
suggestBox.setText("");
}
}
});
suggestBox.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
#Override
public void onSelection(SelectionEvent<Suggestion> arg0) {
if (arg0.getSelectedItem() instanceof Suggestion) {
//code to take actions after selection
}
}
});
What should I do to hide the suggestion list after selecting one of
the suggestion.?
Why don't you hide the list in your selection handler?
#Override
public void onSelection(SelectionEvent<Suggestion> arg0) {
if (arg0.getSelectedItem() instanceof Suggestion) {
//code to take actions after selection
}
DefaultSuggestionDisplay display = (DefaultSuggestionDisplay) suggestBox.getSuggestionDisplay();
display.hideSuggestions();
}

Adding click event to JavaFX TableCell

I have TableView and I want my program to doSomething() when user clicks on a cell. After searching on Internet (stackoverflow included), I found this.
Tried that method, but I got a compile error on these code :
EventHandler click = new EventHandler() {
public void handle(MouseEvent t) {
System.out.println("CLICKED");
}
};
NetBeans asked me to override all abscract method, so I did it.
EventHandler click = new EventHandler() {
#Override
public void handle(MouseEvent t) {
System.out.println("CLICKED");
}
};
Still got same error :
error: method does not override or implement a method from a supertype
If I remove #Override annotation, I got :
error: <anonymous pengamatan.penginderaan.FXMLDocumentController$4> is not abstract and does not override abstract method handle(Event) in EventHandler
Any help? Thank you.
You can try:
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("cell clicked!");
}
});
found here
Found the solution! Here is the code :
....
import javafx.scene.input.MouseEvent;
....
....
EventHandler click = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if(t.getClickCount()>1) {
System.out.println("DOUBLE CLICK");
}
}
};
....
Hope it helps. Thank you.

Problem with SWT Combobox addSelectionListener()

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
}
});

Special method should be called after any AbstractAction-execution

Please excuse the vague question title, but usually I don't do such kind of stuff. So I have the following problem:
I'm designing a popupmenu for a specific app where each menu item is associated with a certain action:
public class CanvasMenu extends JPopupMenu {
public CanvasMenu(){
this.add(new AbstractAction("Do some operation") {
#Override
public void actionPerformed(ActionEvent arg0) {
doSomeStuff1();
cleanup(); // has to be done after every menu operation
}
});
this.add(new AbstractAction("Other operation") {
#Override
public void actionPerformed(ActionEvent e) {
doSomeOtherStuff();
cleanup(); // has to be done after every menu operation
}
});
}
}
I read somewhere that AbstractAction is used for such tasks where you want to add menu items and associate them with some action. In reality, I want not only two such actions, but some dozen of them.
The problem is the cleanup thing. cleanup should be after any of these actions has been chosen. This means, if I continue in the abovely described manner, I will have to write cleanup() for each AbstractAction.
Is there any (easy/elegant/nice) way or pattern to avoid writing cleanup() over and over again? I.e. is it possible to desing something that will only get the action and after executing it automatically call cleanup?
This is one of the patterns:
abstract class ActionWithCleanup extend AbstractAction {
#Override
public final void actionPerformed(ActionEvent arg0) {
myAction();
cleanup(); // has to be done after every menu operation
}
public abstract void myAction();
}
...
this.add(new ActionWithCleanup("Do some operation") {
#Override
public void myAction() {
doSomeStuff1();
}
});

Categories