tree item and focus

87 views
Skip to first unread message

David Durham

unread,
Jan 8, 2009, 10:08:57 AM1/8/09
to Google-We...@googlegroups.com
Hi all,

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.

David Durham

unread,
Jan 8, 2009, 10:16:14 AM1/8/09
to Google-We...@googlegroups.com
<snip>

> 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

todd....@gmail.com

unread,
Jan 8, 2009, 12:53:38 PM1/8/09
to Google Web Toolkit
Whenever I see problems like this I try deferring the focus and/or
listener registration from the event which creates a new widget like
this. The Tree implementation includes some fun internals which change
focus on things to ensure visibility and handle keyboard input.

Example:
public void onClick(Widget w) {
DeferredCommand.addCommand(new Command() {
public void execute() {
createMyWidget();
}
});

David Durham

unread,
Jan 19, 2009, 11:02:01 AM1/19/09
to Google-We...@googlegroups.com
On Thu, Jan 8, 2009 at 11:53 AM, todd....@gmail.com
<todd....@gmail.com> wrote:
>
> Whenever I see problems like this I try deferring the focus and/or
> listener registration from the event which creates a new widget like
> this. The Tree implementation includes some fun internals which change
> focus on things to ensure visibility and handle keyboard input.
>
> Example:
> public void onClick(Widget w) {
> DeferredCommand.addCommand(new Command() {
> public void execute() {
> createMyWidget();
> }
> });
> }

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

Litty Preeth

unread,
Jan 20, 2009, 12:04:57 AM1/20/09
to Google-We...@googlegroups.com
May be you should first select the treeitem and then change the String item into a TextBox item.

- Litty Preeth

David Durham

unread,
Jan 20, 2009, 11:16:26 AM1/20/09
to Google-We...@googlegroups.com
On Mon, Jan 19, 2009 at 11:04 PM, Litty Preeth <preet...@gmail.com> wrote:
> May be you should first select the treeitem and then change the String item
> into a TextBox item.

Yeah, maybe it would. If anyone knows for sure, please say so.
Otherwise, I've moved on to writing my own.


-Dave

Litty Preeth

unread,
Jan 21, 2009, 8:02:25 AM1/21/09
to Google-We...@googlegroups.com
Hi David,

Out of curiosity I tried that one and it works. Actually you shud first set the widget of the tree item as text box, then call the setSelected(true) for the tree item and then set the focus to the text box. This works in FF.

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.

- Litty Preeth

David Durham

unread,
Jan 23, 2009, 12:34:19 PM1/23/09
to Google-We...@googlegroups.com
On Wed, Jan 21, 2009 at 7:02 AM, Litty Preeth <preet...@gmail.com> wrote:
> Hi David,
>
> Out of curiosity I tried that one and it works. Actually you shud first set
> the widget of the tree item as text box, then call the setSelected(true) for
> the tree item and then set the focus to the text box. This works in FF.

Cool. I already wrote a different tree that more closely fits my
use-cases, but this is good info.

Thanks,

-Dave

Reply all
Reply to author
Forward
0 new messages