Editable Label/Div losing focus on click - gwt

I have an editable GWT Label which shows a strange behavior. That is if I click the text “Add note…” the cursor does not appear until I click a second time. But if I click on the label outside the text the cursor appears on first click. How do I solve that? My guess is that replacing the text also removes the cursor when the cursor is in the text. So how can I get the cursor back on first click?
public class EditableLabel extends Label implements FocusHandler {
public EditableLabel() {
super();
getElement().setAttribute("contenteditable", "true");
getElement().setAttribute("tabindex", "1");
this.sinkEvents(Event.ONBLUR);
this.sinkEvents(Event.ONFOCUS);
addHandler(this, FocusEvent.getType());
setText("Add note...");
}
#Override
public void onFocus(FocusEvent event) {
setText("");
}
}

I think your problem depends on the browser. On FF it works fine for me.
I assume you want to write something, if so try to change Label for TextBox, it should work.

Related

Gwt DragStartHandler does not work in IE and Firefox

I have a FocusPanel in which contains a Label.
And then I add the DragStartHandler to FocusPanel like the code below:
focusPanel.addDomHandler(new DragStartHandler() {
#Override
public void onDragStart(DragStartEvent event)
{
focusPanel.getElement().getStyle().setBackgroundColor("yellow");
event.getDataTransfer().setDragImage(focusPanel.getElement(), 10, 10);
dragSourceIndex = getFocusPanelIndex(focusPanel);
}
}, DragStartEvent.getType());
I can drag this in Chrome, but cannot do this in IE and Firefox. Moreover, if I highlight the focuspanel's text first, then I can drag when it's highlighted.
Does anyone know what is wrong?
Thanks.
I make a go-around by adding highlight on MouseDownEvent.
focusPanel.addMouseDownHandler(new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event)
{
markText(focusPanel.getWidget().getElement());
}
});
The markText method is learn from here:
Set selected text in GWT (in order to make copy paste easier)
This is a hacking but at least works.
Now the issue is, the "drop" events are not happening in IE. I use IE 11.

Cursor position to end of string in listgird textbox?

Is there any way by which, i can set the cursor position to end of string in listgird textbox.
Example :
Enter website name, once user click on cell of listgird its will display 'http://' now i want that my cursor position to the end of string.>
it is quite easy:
TextBox yourBox = new TextBox();
root.add(yourBox);
yourBox.setValue("AMSTERDAM");
yourBox.setFocus(true);
yourBox.setCursorPos(yourBox.getValue().length());
sometimes you lose anyway the focus while rendering the complete component. If this happens you can try to put the setFocus in a scheduleDeferred-Command, it means, after the rendering process of the browser is done, the code in execute() will be executed. So you can be sure, that no other component will get also a focus, and your component lose it again.
final TextBox yourBox = new TextBox();
root.add(yourBox);
yourBox.setValue("AMSTERDAM");
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
#Override
public void execute()
{
yourBox.setFocus(true);
yourBox.setCursorPos(yourBox.getValue().length());
}
});

Radio button change handler refreshing the page

I'm using UploadItem, RadioGroupItem and some other widgets. RadioButton is having onChangeHandler which will decide what all other components need to be displayed. I've uploaded some file using UploadItem. Then I changed the radio button selection. On changing the radio button, required widgets are getting displayed properly but whatever file I'd selected using UploadItem is going away. Fresh UploadItem widget is getting displayed. In other words page is getting refreshed.
My requirement is whenever I change radio button option, required widget should displayed along with that whatever file I had selected using UploadItem should remain same.
My Code is something like this:
UploadItem upload = new UploadItem();
RadioGroupItem radioGroup = new RadioGroupItem();
HashMap map = new HashMap();
map.put("option1","option1");
map.put("option2","option2");
radioGroup.setValueMap(map);
TextItem textbox = new TextItem();
radioGroup.addChangeHandler(new ChangeHandler(){
public void onChanged(ChangedEvent event) {
String radioValue =((String)event.getValue());
if(radioValue.equalsIgnoreCase("option2")){
textbox.show();
}else{
textbox.hide();
}
}
});
Add all created widgets to DynamicForm object using dynamicForm.setFields(all created widgets)
Changing the radio button should hide and show the textBox. But while doing that page is getting refreshed and whatever file we had selected using UploadItem is lost.
As per the documentation for hide() and show() of FormItem class, invocation of any of these methods, will cause the DynamicForm to be redrawn.
So it may cause the problem you're getting.
To overcome this issue, I would suggest you to put UploadItem in a separate DynamicForm.
fire an event on radio Selection change as
radioButton.addListener(Events.Change, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
if(radioButton.getValue()){
//fire an event here for ur widget
}
}
});

Select text on click in EditorGridPanel (gwt-ext)

I have a gwt-ext EditorGridPanel, by clicking onto a cell, you can edit its value. The cursor is placed at the beginnig of the cell, but I want to select the whole text in this cell if the user clicks on it. Any idea how I can handle this?
I tried some Listeners etc. but none worked for me yet.
Sorry for my english, with Ext-GWT(GXT) it's posible doing this:
final TextField<String> text = new TextField<String>();
text.setAllowBlank(false);
CellEditor textEditor = new CellEditor(text);
textEditor.addListener(Events.StartEdit, new Listener<EditorEvent>() {
public void handleEvent(EditorEvent be) {
text.setSelectOnFocus(true);
}
});
column.setEditor(textEditor);
configs.add(column);
Just find the way in GWT-Ext, I think will something like this.....I hope this help....

focus & key board listner problem in cell table in gwt

I have one small problem i.e. one textbox is their when click on the text box the popup is shows below the text box.The popup contains celltable
i write keypresslisner for textbox when i press Down And UP arrow the focus is set to be cellTable And also we still press down and up arrows its highlites the rows in celltable
anyone please tell me how to solve it...it's my request
When I wrote code like this:
box.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == 38) {
celltable.setFocus(true);
} else if (event.getNativeEvent().getKeyCode() == 40) {
celltable.setFocus(true);
}
}
});
only the focus is goes to the cell table
The question is a little bit unclear, but I think you may be helped by FocusPanel:
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/FocusPanel.html
Wrap your CellTable in this FocusPanel and you can do setFocus() on that instead. The FocusPanel has addKeyPressHandler(), so you can capture further keypress events there, also when your textbox has lost the focus.