Dynamic set text in GWT Label - gwt

I want to set text of GWT Label dynamically.
For example I am using below code to set text in GWT Label :
Label statusLabel = new Label();
for (int i = 0; i < numX; ++i) {
for (int j = 0; j < numY; ++j) {
statusLabel.setText("Processing " + "(" + (i + 1) + "/" + (j + 1) + " of " + numX + "/" + numY + ") ...");
}
}
And I add this Label in RootPanel one time like this : RootPanel.get().add(statusLabel);
But problem is that Label text is unchnage.
What I missed? Or How can set dynamic text into GWT Lable.

Your loop is just to fast. It does not allow the browser to render the changed text. If your processing is running just inside that loop, the generated JavaScript code will be executed as a single block and will not allow the UI to refresh in-between.
To allow the UI to refresh, you need to use delayed logic.
For example using Scheduler.get().scheduleDeferred.
Deferred means here the JavaScript code returns control to the browser, which refreshes the text, and afterwards the ScheduledCommand gets executed.
To make it short:
Split up your processing in different parts, where each of them changes the text and gets scheduled using a ScheduledCommand.
As an alternative you could keep a counter variable in your class (like in the loop) and just re-schedule ScheduledCommands until your processing is finished.
Just keep in mind that the browser needs some time to refresh the text and that it won't do that until your JavaScript code returns.

Related

Displaying a single text in bold in textview in android

How to show each word in bold for particular seconds in textview in android
You can use this....
But adding a sleep will block your UI so you better use handlers instead of sleep
textView.setTypeface(null, Typeface.BOLD);
Thread.Sleep(1000);
textView.setTypeface(null, Typeface.NORMAL);
Yes you can do that too...
textView.setText(Html.fromHtml("<b>" + Hello + "</b>" + World));
Thread.Sleep(1000);
textView.setText(Html.fromHtml(Hello + "<b>" + World + "</b>"));
You might need other styles supported by TextView, here you go.
I hope i helped ;)

gwt firefox getAbsoluteLeft()

I'm seeing a truly weird behavior while trying to set a popup relative to another element in GWT. It seems like setting the popup position (an independent, floating element) changes the answer I get from calls like getAbsoluteRight() or getAbsoluteLeft() for a completely different element, which is static on the page and does not move visually.
I added some print statements to check what was going on, so here is the code:
System.out.println(item.td);
int position = item.td.getAbsoluteRight()-offsetWidth;
System.out.println("left/right:" + item.td.getAbsoluteLeft() + "/" + item.td.getAbsoluteRight() + ". sent:" + (item.td.getAbsoluteRight() - offsetWidth) + "=" + position);
popup.setPopupPosition(position, item.td.getAbsoluteBottom());
System.out.println("left/right:" + item.td.getAbsoluteLeft() + "/" + item.td.getAbsoluteRight() + ". sent:" + (item.td.getAbsoluteRight() - offsetWidth) + "=" + position);
popup.addStyleName("bigger");
System.out.println("left/right:" + item.td.getAbsoluteLeft() + "/" + item.td.getAbsoluteRight() + ". sent:" + (item.td.getAbsoluteRight() - offsetWidth) + "=" + position);
System.out.println(item.td);
Here is the result on Chrome
Menu displayed, widths: 81/340=340
<td class="hover">Daniel?</td>
left/right:1104/1185. sent:845=845
left/right:1121/1202. sent:862=845
left/right:1121/1202. sent:862=845
<td class="hover">Daniel?</td>
Here is the result on Firefox
Menu displayed, widths: 81/340=340
<td class="hover">Daniel?</td>
left/right:1254/1335. sent:995=995
left/right:1273/1354. sent:1014=995
left/right:1273/1354. sent:1014=995
<td class="hover">Daniel?</td>
so the left/right coordinates of the fixed element suddenly change (X coordinate goes from 1254 to 1273) after calling setPopupPosition(), while the relevant element actually stays in the same place (visually). I really have no idea how it happens as the popup doesn't even know of the existence of that element. Even more, while I can reproduce the error consistently, it does not happen if I switch the popup content...
... incidentally, I compared the coordinates given by firefox with a screenshot of the page, and the return values are not only wrong, but impossible given my screen size (1366x768) and no scrolling.
I could probably try setting the position twice, as the second value is actually the correct one, but I would really like to understand what is going on here...
Many thanks!
The differ is exactly 150 pixels. (May be total of 75 pix in left side)
Have you checked page against: http://validator.w3.org/
There's often differences within the margin/padding (also borders in IE).
As i get out of your getAbsoluteRight()-offsetWidth code, you using the td to get absolute right. But setting the position on the popup. This should mean that you have some borders/margins/padding between the popup and the td content.
The getAbsoluteLeft() and getAbsoluteRight() (as well as Top and Bottom) are all calculated based on their parental element's scroll and offset positions.
Also an empty object can often end up in a default width. But as soon as you enter a content, the size adapts to its content.

MS Access - Close form without save design changes, acSaveNo doesnt work

I have a Front End/Back End app in MS Access and I'm having some problems with performance when I close forms.
These forms are always saving before close because in Form_Open I hide some columns and edit the caption. Then, when I close the form, it saves the hidden columns and wastes much time!
I have already done a lot of settings modifications in Access for optimize for FE/BE. But I'm still have problems with forms that save before close. On save event it really wastes a lot of time. With the hide/edit columns routine it takes 20s to save. Without the routine it takes 1s or less, but my data is not saved.
How could I hide/edit these columns without the need to save the form afterwards? Or how could I close the form without saving structure changes?
Unfortunately Access doesn't have a BeforeClose event, and in a Close or Unload event it saves before going to this sub, so I can't cancel it to close later in code.
Hide Code:
'show all columns
For i = 1 To 8
Form_Y_SubF_LP.Controls("Item_00" & i).ColumnHidden = False
Form_Y_SubF_LP.Controls("Quantity_00" & i).ColumnHidden = False
Form_Y_SubF_LP.Controls("DistributionEQ_00" & i).ColumnHidden = False
Next
'Hide unnecessary columns
For i = 8 To ProtQuant + 1 Step -1
Form_Y_SubF_LP.Controls("Item_00" & i).ColumnHidden = True
Form_Y_SubF_LP.Controls("Quantity_00" & i).ColumnHidden = True
Form_Y_SubF_LP.Controls("DistributionEQ_00" & i).ColumnHidden = True
Next
'Change the caption of columns to the real name of each prototype
prot = DLookup("[Prototype]", "Y_Configurações", "[Program]= '" & ProgramName & "'")
For i = 0 To UBound(Split(prot, ";"), 1)
Form_Y_SubF_LP.Controls("Item_00" & i).Properties("Caption") = "Item_" & Split(prot, ";")(i)
Form_Y_SubF_LP.Controls("Quantity_00" & i).Properties("Caption") = "Quantity_" & Split(prot, ";")(i)
Form_Y_SubF_LP.Controls("DistributionEQ_00" & i).Properties("Caption") = "DistributionEQ_" & Split(prot, ";")(i)
Next
A simple solution is to set Close Button = No on the form's property sheet, then add a command button, cmdCloseMe, with this for its On Click event:
Private Sub cmdCloseMe_Click()
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
If you need your form displayed in Datasheet View, you would need to embed it in a subform control of another form and add the cmdCloseMe button to that form.

Setting up some properties for a combobox (scroll, edit, jump)

There are 3 properties that I want to set for some VBA form comboboxes and I don't know if it's possible.
I don't want to let the combobox editable. Right now if the user types something in it that it submits the form it will send that value... I want to let him choose only from the values I added in the Combobox.
I want to make the list of items in the combobox scroll-able. Right now I'm able to scroll through the list if I use the scroll-bar but I don't know why I can't scroll with the mouse scroll.
And I want to jump to some item if I start typing. Let's say I have the months of the year in one combobox... if I start to type mar I want it to jump to march. I know that for the html forms this properties is by default but I don't know about VBA forms...
Thanks a lot
Of the behaviours you want, some are possible with settings on the Combo, others you will need to code
List of Months: Put a list of entries on a (hidden) sheet and name the range. Set .RowSource to that range
Match as you type: Set properties .MatchEntry = fmMatchEntryComplete and .MatchRequired = True
Reject non list entries: A Combo with these settings will allow you to type an invalid entry, but will reject it with an error message popup when you commit. If you want to silently reject invalid data as you type, you will need to code it.
If you want the selected value returned to a sheet, set .ControlSource to a cell address (preferable a named range)
By "...scroll with the mouse scroll..." I assume you mean the mouse wheel. Unfortunatley Forms don't support mouse wheel scroll. You will have to code it yourself. There is a Microsoft patch for this at here (not tried it myself yet)
Sample code to silently reject invalid entries
Private Sub cmbMonth_Change()
Static idx As Long
Dim Match As Boolean
Dim i As Long
If cmbMonth.Value = "" Then Exit Sub
If idx = 0 Then idx = 1
i = idx
Match = False
For i = 0 To cmbMonth.ListCount
If cmbMonth.List((i + idx - 1) Mod cmbMonth.ListCount) Like cmbMonth.Value & "*" Then
cmbMonth.ListIndex = (i + idx - 1) Mod cmbMonth.ListCount
Match = True
Exit For
End If
Next
If Not Match Then
cmbMonth.Value = Left(cmbMonth.Value, Len(cmbMonth.Value) - 1)
End If
End Sub
Set the propertie MatchEntry of combobox to 1 (fmMatchEntryComplete) and MatchRequired to true for example
combobox1.MatchEntry=1
combobox1.MatchRequired=True
[]'s

How to scroll a GWT TextArea?

I am calling com.google.gwt.user.client.ui.TextArea.setText(myText) to set the content. After that I call setCursorPosition(myText.length()) to get the cursor to the end. This works well.
When myText has more lines then the text area can display at once, it shows a scroll bar. But it does not scroll to the cursor position. Even worse - it scrolls to the top.
How can I scroll the GWT TextArea to the cursor position? I realy need the cursor position, not the bottom of the TextArea. A JSNI workaround would be ok too.
Try adding this after setting the cursor position:
textAreaToScroll.getElement().setScrollTop(textAreaToScroll.getElement().getScrollHeight());
This will scroll the element to the bottom.
EDIT:
To scroll to the any cursor position there is (as far as I know) no easy way to do so. I don't think there is any way to ask the browser wich line the cursor is on.
I just got an idea for something that may work (haven't actually tested it) to guess a rough estimate of how long to scroll.
int cursorPos = textAreaToScroll.getCursorPos();
long offsetRatio = cursorPos / textAreaToScroll.getText().length();
//gives 0.0 to 1.0
offsetRatio += someMagicNumber; // -0.1 maybe?
// Depending on the font you may need to adjust the magic number
// to adjust the ratio if it scrolls to far or to short.
offsetRatio = offsetRatio>0.0 ? offsetRatio : 0; //make sure
//we don't get negative ratios
//(negative values may crash some browsers while others ignore it)
textAreaToScroll.getElement().setScrollTop(
textAreaToScroll.getElement().getScrollHeight() * offsetRatio );
This may scroll the roughly the desired distance. Note, this assumes each line is filled about the same amount since it uses the cursor position divided by the length of the text and not the number of lines (wich is hard to calculate). Manual newlines will skew this estimate, and proportional fonts will allso make it less accurate.
You'll probably need to adjust the ratio so that it scrolls sligtly too short rather than too far since the cursor will still be visible if it's slightly below the top of the text area.
As I said I haven't actually tested this, I may have inverted the logic and other subtle bugs.
I had a scenario where the textarea will already have something and when a new command is submitted it will add the data to it and scroll to the start of the newly added data. this is what I did
// Hold the previous height to set the scroll.
final int prevHeight = document.get().getElementById(textareadid).getScrollHeight();
// Hold the prev position if output area already has some data.
final int prevPos = this.textArea.getValue() != null ?
this.textArea.getValue().length() : 0;
after processing and setting the new data
int posCount = 0;
if (previousResponse != null && !previousResponse.isEmpty())
{
final String dataStr = "new data from process";
// add 15 lines for the cursor position
posCount = getRelativeCount(dataStr);
}
this.textArea.getElement().setScrollTop(prevHeight);
this.textArea.setCursorPos(prevPos + posCount);
private int getRelativeCount(final String str)
{
int charCount = 0;
if (str != null)
{
int NUM_ROWS = 15;
if (getUserAgent().contains("msie"))
{
NUM_ROWS = 16;
}
final String[] splitArr = str.split("\n"); // split on the new line
// char
for (int index = 0; index < splitArr.length && index < NUM_ROWS; index++)
{
charCount += splitArr[index].length();
}
}
return charCount;
}
To improve on Stein's answer, you can count the number of lines in the text, and then set the top position based on the desired line over the total lines, rather than using characters.
While you are counting lines, you will also have to determine which line the cursor is in.
String text = textArea.getText();
int lines = 1;
int pos = 0;
int cursorPos = ...;
int cursorLine = -1;
while((pos = 1+text.indexOf("\n", pos)) > 0)
{
if (cursorLine == -1 && pos > cursorPos)
cursorLine = lines;
lines++;
}
if (lines > 0 && cursorLine > 0 && cursorLine < lines)
{
int scroll = textArea.getElement().getScrollHeight();
scroll *= cursorLine;
scroll /= lines;
scroll -= 30; // Back up a bit so it's not right at the top
if (scroll < 0)
scroll = 0;
textArea.getElement().setScrollTop(scroll);
}
this worked for me:
GWT textArea Scroll issue
http://www.gwtplayground.com/2012/08/gwt-textarea-scroll-issue_21.html