Why the suggestion list remains visible in GWT - java

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

Related

GWT Tree: selection Listener

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

How to remove object (including components) from arraylist?

I'm working on a task planning app. I have a 'new task' button to add a task. When clicked, this button makes a new instance of the TaskRowToDo class and adds this to the toDoList arraylist. This class contains a row with a text field and some buttons.
This is the 'new task' button code:
private void drawNewBtn(){
JButton btnNew = new JButton("Nieuwe taak");
btnNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("New task added");
toDoList.add(new TaskRowToDo(toDoIndex+7, false, "new task", 2));
toDoList.get(toDoIndex).draw();
toDoIndex++;
frmPlanner.revalidate();
}
});
frmPlanner.getContentPane().add(btnNew, "cell 3 12");
}
At the end of the TaskRowToDo there is a 'remove' button. This button should remove the row from the toDoList and remove this row from the screen.
Below is the 'remove' button code:
btnRemoveToDo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("To Do removed");
toDoIndex--;
toDoList.remove(toDoIndex);
frmPlanner.revalidate();
}
});
The button removes the instance of TaskRowToDo from the toDoList, but it still shows up on screen and the components even work. So it's not really removed. I've tried using revalidate() and repaint() but to no avail. As a test I tried frmPlanner.removeAll() and even this doesn't clear the screen (however the components won't work anymore).
How do I remove this one row, including its components and clear this space on the screen?
toDoList is JList?
give same more code, on this time I suggest to setModel on JList.
I always prepare method setModel when I create JList and it works:
private void setModelForJList() {
toDoList.setModel(new ListModel<TaskRowToDo>() {
#Override
public int getSize() {
return toDoListEntityList.size();
}
#Override
public TaskRowToDogetElementAt(int index) {
return toDoListEntityList.get(index);
}
#Override
public void removeListDataListener(ListDataListener l) {
}
#Override
public void addListDataListener(ListDataListener l) {
}
});
toDoList.repaint();
}
when you delete object from JList, call this method, toDoListEntityList is list of object which you put in Jlist.
About JList some advices. Good practice is declare generic type of JList (in your case is JList<TaskRowToDo> toDoList= new JList<TaskRowToDo>

JavaFX: How to make enter key submit TextArea

Sorry if this seems a little too easy, I'm brand new to JavaFX, this is my first little app built with it.
I am trying to make a bare bones chat client. I am using the JavaFX Scene builder to make the client UI, and a controller class connected to the FXML.
How can I make is so that the current text of in the text area is submitted to the server and the text area is cleared upon the enter key press, instead of using some kind of "send" button?
EDIT: Here is the code that is not working:
//...
public class FXMLDocumentController
{
//...
#FXML private TextArea messageBox;
//...
messageBox.setOnKeyPressed(new EventHandler<KeyEvent>()
{
#Override
public void handle(KeyEvent keyEvent)
{
if(keyEvent.getCode() == KeyCode.ENTER)
{
//sendMessage();
}
}
});
//...
This should get you what you want:
TextArea area;
//... (initialize all your JavaFX objects here...)
// wherever you assign event handlers...
area.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
String text = area.getText();
// do your thing...
// clear text
area.setText("");
}
}
});
I might add, that if you are so inclined to provide both a button and an enter key event, you could tie the event handler functions of both controls to a single common function in a way such as this:
Button sendButton;
TextArea area;
// init...
// set handlers
sendButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
sendFunction();
}
});
area.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
sendFunction();
}
}
});
// define send function
public void sendFunction() {
String text = this.area.getText();
// do the send stuff
// clear text (you may or may not want to do this here)
this.area.setText("");
}
Either way works, good luck.
You can use lambda expressions also ... I think it is more elegant and simply
textArea.setOnKeyPressed(event -> {
if(event.getCode() == KeyCode.ENTER){
//type here what you want
}
});
In addition to the other answers, I think it might be useful in some applications to not actually invoke the send function if the user pressed SHIFT+ENTER. In that case he/she maybe actually wanted a new line.
textArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume(); // otherwise a new line will be added to the textArea after the sendFunction() call
if (event.isShiftDown()) {
textArea.appendText(System.getProperty("line.separator"));
} else {
sendFunction();
}
}
});
If you don't want to send empty messages you can do something like this:
textArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume();
if (event.isShiftDown()) {
textArea.appendText(System.getProperty("line.separator"));
} else {
if(!textArea.getText().isEmpty()){
sendFunction();
}
}
}
});

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