there are three edit field on my mainscreen i want to know which edit field is on focous.
thanks alot
Alternatively, you can have your Mainscreen class implement FocusChangeListener.
If you instantiate your Edit controls as Focusable, and implement the listener:
public void focusChanged(Field field, int eventType)
{
if(field == YourEditField1)
{
}
else if (field == YourEditField2)
{
}
}
isFocus
public boolean isFocus()
Determines if this field currently has the focus.
Returns:
True if this field has the focus; otherwise, false.
Since: JDE 4.2.0
Related
I use Vaadin23.
Is there a legal way to prevent tab switching?
As i understand documentation method
public Registration addSelectedChangeListener(
ComponentEventListener<SelectedChangeEvent> listener) {
return addListener(SelectedChangeEvent.class, listener);
}
register listener which is triggered after tab change event.
What are the options?
Thanks!
If your tab contents are navigation targets (Routes), you can use the BeforeLeaveObserver interface in your form view:
public class SignupForm extends Div
implements BeforeLeaveObserver {
#Override
public void beforeLeave(BeforeLeaveEvent event) {
if (hasChanges()) {
ContinueNavigationAction action =
event.postpone();
ConfirmDialog confirmDialog = new ConfirmDialog();
confirmDialog.setText("Your form has changes! Are you sure you want to leave?");
confirmDialog.setCancelable(true);
confirmDialog.addConfirmListener(__ -> action.proceed());
confirmDialog.open();
}
}
private boolean hasChanges() {
// TODO: implement your logic to check if there are unsaved changes
}
}
How do I get a textbox to give me an error if anything other than numbers and/or decimals are submitted?
You need to add a validator to the text field.
textField.addValidator(new Validator());
There is not a validator that checks if the value is a number or not in Vaadin, so you need to create a custom one:
private class MyValidator implements Validator {
#Override
public void validate(Object value)
throws InvalidValueException {
if (!(value instanceof Integer &&
(value instanceof Double)) {
throw new InvalidValueException("The value is not a number");
}
}
}
Now you can add your custom validator to your field:
textField.addValidator(new MyValidator());
More info about validators here.
private class MyValidator implements Validator {
#Override
public void validate(Object value) throws InvalidValueException {
try {
// new Integer(text);
new Double(value.toString());
} catch (NumberFormatException e) {
throw new InvalidValueException("The value is not a number");
}
}
}
I am creating a BlackBerry application which contains two ObjectChoiceFields. I want to add listeners for each of them. I searched for that but did not find any of useful code.
It is like a country-state selector on websites. I am looking for simple logic, and I have no need of any database oriented solution. Can anyone describe how to add listeners for those two ObjectChoiceField?
I have have attached my code below, but it is only working for country choice field. Nothing happens when I change state choice.
public class MyApplication extends MainScreen implements FieldChangeListener
{
ObjectChouceField choice_c,choice_s;
public MyApplication ()
{
this.setTitle("hai");
choice_c = new MyChoiceField("Select a Country", countryArray);
choice_c.setChangeListener(this);
this.add(choice_c);
choice_s = new MyChoiceField("Select a State", stateArray);
choice_s.setChangeListener(this);
this.add(choice_s);
..................
..................
}
public void fieldChanged(Field field, int context)
{
if(field == choice_category)
{
Dialog.alert("choice country has been pressed");
}
else if(field == choice_round)
{
Dialog.alert("choice state has been pressed");
}
}
}
Have you tried overriding the navigationClick method of the ObjectChoiceField?
Here is an example:
ObjectChoiceField selectionField = new ObjectChoiceField("Select the Country",countryArray)
{
protected boolean navigationClick(int arg0, int arg1)
{
return super.navigationClick(arg0, arg1);
}
};
cf is the ChoiceField. Use the getSelectedIndex to and trigger events accordingly.
cf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
switch (cf.getSelectedIndex()) {
case 0:
//code here //
break;
default:
break;
}
}
});
I create a custom menu in new class extends popupScreen. I went when the user clock on the menu button the menu display. I use this code
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
return true;
}
else
return super.keyDown(keycode, time);
}
but I obtain error on this line UiApplication.getUiApplication().pushScreen(popup); causes by pushScreen. How can I change this code or there is another method to display this menu.
try this
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
}
return true;
}
else
return super.keyDown(keycode, time);
}
In Blackberry, creating, displaying and hiding of Menu is handled by the system; you will be provided with callbacks at appropriate time. The callbacks you will need to code are:
public boolean onMenu( int instance )
{
return true; // Menu is created
}
public void makeMenu( Menu menu, int instance )
{
MenuItem _desired_menu_item = new MenuItem()
{
public void run()
{
}
}
menu.add( _desired_menu_item );
super.makeMenu( menu, instance)
}
That's it! It would work. Please check Blackberry "UI and Navigation guide" for more details
I'm trying to set the text on a button when it is clicked. I'm initialising a BigVector which will update the text of the button with its value. I'm using a counter value to determine wihcih BigVector value should be selected. The problem is, the below code is expecting the counter value to be final.
A better methodology for updating the text on a Field when it is clicked is most welcome.
Here is my code -
final BigVector bigStringVectorA = new BigVector();
bigStringVectorA.addElement("A Test answer 1");
bigStringVectorA.addElement("A Test answer 2");
bigStringVectorA.addElement("A Test answer 3");
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});
Thanks
You can make the counter an instance variable, either in the outer class or in the anonymous FieldChangeListener:
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
private int counter = 0;
public void fieldChanged(Field field, int context) {
counter++;
if (counter > bigStringVectorA.size()) {
counter = 0;
}
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});
You can try to call the invalidate() method of that field and that should force the redraw of that button.
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
aAnswerOptionButton.invalidate();
}
});