Codename One - Correct use of the Picker - codenameone

I'm trying to do a clear question about the use of the Picker, because my previous one in not enough clear: Codename One - addActionListener of a Picker
The purpose of the Picker is to select one element from a set, such as a string of a set of strings, is it right? So, I want to give the user the opportunity to select a string from a set of strings, each of one corresponds to a language.
The problem is that the use of an ActionListener added to the Picker is not correct for this purpose, because it's triggered by both "Ok" and "Cancel" buttons.
I need to execute some code (to change the language) only and only if the user press "Ok", because executing that code if the user press "Cancel" should be considered an unexpected behavior and a bug (from the point of view of the user).
So my question is how to use correctly the Picker in this use case.

From the CN1 documentation, one way to do this would be :
String [] list = {"one" , "two" , "three"};
Picker picker = new Picker ();
picker.setType(Display.PICKER_TYPE_STRINGS);
picker.setStrings(list);
picker.setSelectedString("test");
picker.addActionListener(l -> System.out.println(picker.getSelectedString()));
Oddly, and as Francesco has pointed out in a prior moment, when you run the application IN THE SIMULATOR, and press the Cancel-Button inside the picker, it prints out the selected string. Same when you press the OK-Button. (Is this intended?)
On installed devices, the results seem to be mixed, as to on some devices the cancel operation does not incur in any action.

I had to do this to avoid triggering another listener on the string (I was using a Property with a change listener) if Cancel was pressed, and I did this by checking the current picker string against the previous selected string to see if it changed. I only updated the field or local variable (Property in my case) if the value changed. Modifying the previous answer's code:
...
private String currentSelection;
...
String [] list = {"one" , "two" , "three"};
Picker picker = new Picker ();
picker.setType(Display.PICKER_TYPE_STRINGS);
picker.setStrings(list);
picker.setSelectedString("test");
picker.addActionListener(l -> {
if (!picker.getSelectedString.equals(currentSelection) {
currentSelection = picker.getSelectedString;
System.out.println(currentSelection);
}
});

Related

TextField keyboard is lost when clearing text from within DataChangedListener

I have a TextField on which I listen key presses
TextField msgField = new TextField();
msgField.setSingleLineTextArea(false);
msgField.addDataChangedListener((i1, i2) -> { ... });
I want to clear() the field's text when a certain key is pressed (enter key)
msgField.addDataChangedListener((i1, i2) -> {
if(!msgField.getText().isEmpty())
{
String lastChar = msgField.getText().substring(msgField.getText().length()-1);
if(lastChar.equals("\n"))
{
msgField.clear();
}
}
});
When enter is pressed, the keyboard is closed on this field, which I am trying to prevent. How can I prevent this? setText("") (instead of clear()) produces a random number of further calls to DataChanged which messes things up (I tried wrapping with sync and control vars, no success there either). I have tested this on Android
DataChangedListener is verbose so you shouldn't rely on it's low level behavior too much. In other words I'm not sure why clear() doesn't send events but it's possible it should... You need to guard against that in your DataChangedListener .
If you want to re-open the virtual keyboard just use startEditingAsync on the text field which will re-launch the text field. Notice that clear explicitly stops editing as invoking setText while editing can produce an inconsistent result as the user might be typing something on the native UI thread and you're trying to override him from the EDT. Stopping to make a change is essential to synchronize the native/lightweight components.

Codename One - get selected text from AutoComplete

How can I get the complete selected text from an AutoComplete TextField?
If I use getText(), I only get the few letters the user has input so far.
Example: I write "flo" and then select "Flowers" from the list, but getText() gives me "flo"
AutoCompleteTextField auto = new AutoCompleteTextField(arrayWithNames);
auto.setMinimumLength(4);
auto.addListListener((ActionEvent evt1) -> {
String lookedFor = auto.getText();
Hashtable<String,Object> match[] = findMatch(lookedFor);
if(hMatch.length>0){
contElements.removeAll();
for (Hashtable<String, Object> Match1 : match) {
...
...//fill the Container with the names found
...
}
}
});
How it works
I am using the AutoComplete TF as a search button.
I have an array with all the names in my list.
Then I populate the Auto with the array.
The user selects a name from the Auto and then I search the value that is being "lookedFor" using the findMatch(). It returns a new array with the found entries.
I need the complete name from the list so I can use the findMatch() method, but when I use getText() from the Auto, it only returns the letters the user entered, and not the whole name, so my method does not work, since I am comparing whole Strings.
(I am using the Auto because it is very convenient if people remember only a part of the name they are looking for)
If you subclass AutoCompleteTextField you can access the selected text internally via getSuggestionModel().getItemAt(getSuggestionModel().getSelectedIndex()). Now you can define a public getter method getSelectedText() or something on your derived class.
I am not sure you are using the AutoCompleteTextBox correctly.
The entire purpose of the AutoCompleteText box is to help you assist the user in selecting from a list of valid requests,
You should not be getting the value of getText() until the user is ready to submit the form where the AutoCompleteTB is located.
This WILL help if you haven't already looked here:
https://www.codenameone.com/javadoc/com/codename1/ui/AutoCompleteTextField.html#getPropertyTypes--
Good luck!

How to access to a Combo box value in Item Line on NetSuite?

In NETSUITE
is there any way to access to a value inside of a combo-box at the item line level?
I need to access to a value after inserting an item but all functions get me null value.
I have tried
nlapiGetCurrentLineItemValue
and
nlapiGetFieldValue
Both functions are getting me null values.
Thanks,
Pablo.
In general (for user event and client script) below code should work
nlapiGetLineItemValue(LINE_ITEM_TYPE, YOUR_FIELD_ID, LINE_NUMBER);
eg on SO to get the line item Id:
nlapiGetLineItemValue('item', 'item', 1);
PS: Syntax is independent of data type or field type
If you mean combo box as a mulitselect, and if you're trying to access via User Event Script, use:
nlapiGetLineItemValues(type, fldname, linenum);
Note the 's' in nlapiGetLineItemValues
If its just a standard field, nlapiGetLineItemValue(type, fldname, linenum) should work.
Which call to use depends on what event you are capturing.
For instance if you are trying to access the value in a post sourcing, field changed or line validate event of a client script you would use nlapiGetCurrentLineItemValue('item', 'fieldname');

In HaxeFlixel, What is Box object and how to use it?

I'm trying to use FlxUICheckBox. In the official documentation, the constructor looks like this:
new(X:Float = 0, Y:Float = 0, ?Box:Dynamic, ..)
What is the Box object?
How should I send a Box object as a parameter to this constructor?
I should probably change it to BoxAsset. It's the image asset you want to use for the box part of the checkbox.
A simple checkbox has three components, and looks a bit like this:
[X] Checkbox
Box means the box part, "[ ]"
Check means the check part, "X"
Label means the text that goes in the textfield next to the checkbox
If you don't provide Box or Check, it will use default FlxUIAssets automatically to skin your checkbox. If you provide your own asset (such as "assets/mybox.png" for example), it will use that instead. It is expecting the same sort of thing you would pass into FlxSprite.loadGraphic() -- a String, a BitmapData, or a FlxGraphic.
I should probably also update the type from :Dynamic to :FlxGraphicAsset, I originally wrote this code a long time ago before they added that new helper type.

Ext JS 4 TextArea - how to set the insertion point?

Is it possible / how do you set the insertion point for an ExtJS 4 Textarea?
I want to insert some text (which I have working), then I want to set the insertion point at a specific length from the beginning of the field:
I am getting the current contents, inserting some text in front. Now I want to move the insertion point to right after the "-":
//field = my ExtJS text area
var ins = "some text I inserted - \r";
var value = ins + field.getValue();
field.setValue(value);
There's no out-of-the-box method that allows this.
Javascript security doesn't allow you to fire a keypress event, so you can't focus the textarea and then fire the "CTRL+END" key combo, or even the "END" key, for example.
I played around with the focus() and select() methods on the <textarea> element, with no success being able to get the cursor to appear at the end.
So my answer is that you shouldn't attempt this until it's officially supported, because even if you get a hack to work, it might only function properly in some browsers.
qaScriptForm is a normal form
Script is a normal TextArea
var insertIndex = qaScriptForm.Script.selectionStart;
var value = qaScriptForm.Script.value;
value=value.substr(0,insertIndex)+ " DATEADD(DAY,-7,GETDATE()) "+value.substr(insertIndex);
qaScriptForm.Script.value=value;

Resources