I've got an issue with a focus listener on a TextBox in a TreeItem. I
want to take action when the textbox loses focus. The code I have
works perfectly in hosted mode on Linux, but does not work in Firefox
after compile/browse. Here's the code:
private void newTreeTextBox(final DynamicTreeItem item, final
FieldGroup group) {
final String originalText = item.getText();
final TextBox box = new TextBox();
box.setText(originalText);
item.setWidget(box);
box.selectAll();
box.setFocus(true);
box.addKeyboardListener(new KeyboardListenerAdapter() {
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if (keyCode == KEY_ENTER) {
setItemText(item, originalText, box.getText(), group);
} else if (keyCode == KEY_ESCAPE) {
setItemText(item, originalText, null, group);
}
}
});
box.addFocusListener(new FocusListenerAdapter() {
public void onLostFocus(Widget sender) {
Window.alert("lost focus");
setItemText(item, originalText, box.getText(), group);
}
});
}
I've got a debug alert in my onLostFocus method. What happens is I
click on an item, it's replaced with a textbox with text selected. I
hit press any key, and the lostFocus event is fired. Any ideas?
Thanks.
> I've got a debug alert in my onLostFocus method. What happens is I
> click on an item, it's replaced with a textbox with text selected. I
> hit press any key, and the lostFocus event is fired. Any ideas?
> Thanks.
The same code works perfectly in konqueror (although a bit clunkier).
-Dave
I tried using a deferred command (actually right after you posted
this), but it didn't work. I think it has to do with Tree and
TreeItems. Interestingly if I have a Tree with only a single root
TreeItem, I have no issues with setting focus to the text within a
textbox within a TreeItem that was just added to a Tree. At this
point, I'm just going to write my own Tree widget.
Thanks for your suggestion, I'm using it in other places.
-Dave
Yeah, maybe it would. If anyone knows for sure, please say so.
Otherwise, I've moved on to writing my own.
-Dave
package litty.test.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FocusListenerAdapter;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.Widget;
public class Home implements EntryPoint, TreeListener {
Tree homeTree;
public void onModuleLoad() {
init();
}
private void init() {
homeTree = new Tree();
TreeItem root1 = new TreeItem("ABC");
root1.addItem("ABC1");
root1.addItem("ABC2");
root1.addItem("ABC3");
TreeItem root2 = new TreeItem("DEF");
root2.addItem("DEF1");
root2.addItem("DEF2");
TreeItem root3 = new TreeItem("EFG");
root3.addItem("EFG1");
root3.addItem("EFG2");
root3.addItem("EFG3");
homeTree.addItem(root1);
homeTree.addItem(root2);
homeTree.addItem(root3);
homeTree.addTreeListener(this);
RootPanel.get().add(homeTree);
}
public void onTreeItemSelected(final TreeItem item) {
if (!(item.getWidget() != null && item.getWidget() instanceof TextBox)) {
final String originalText = item.getText();
final TextBox box = new TextBox();
box.setText(originalText);
item.setWidget(box);
item.setSelected(true);
box.selectAll();
box.setFocus(true);
box.addKeyboardListener(new KeyboardListenerAdapter() {
public void onKeyPress(Widget sender, char keyCode,
int modifiers) {
if (keyCode == KEY_ENTER) {
item.setText(box.getText());
} else if (keyCode == KEY_ESCAPE) {
item.setText(originalText);
}
}
});
box.addFocusListener(new FocusListenerAdapter() {
public void onLostFocus(Widget sender) {
Window.alert("Focus lost");
item.setText(box.getText());
}
});
}
}
public void onTreeItemStateChanged(TreeItem item) {
// TODO Auto-generated method stub
}
}
Hope this helps.
Cool. I already wrote a different tree that more closely fits my
use-cases, but this is good info.
Thanks,
-Dave