Toggle TextField required/not required when checkbox checked - java

I have a text field which I want to toggle as a required field or not based on if a checkbox is checked. I can't get the ValueChangeEventListener to play nice.
This is what I have so far:
private TextField myTextField = new TextField("Name");
myTextField.setRequired(true);
private CheckBox myCheckBox;
myCheckBox = createCheckBox();
private CheckBox createCheckbox() {
CheckBox checkBox = new CheckBox("My checkbox");
checkBox.setImmediate(true);
checkBox.addValueChangeListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
toggleRequired();
}
});
return checkBox;
}
private void toggleRequired() {
if (myCheckBox.getValue() != true) {
myTextField.setRequired(false);
} else {
myTextField.setRequired(true)
}
}
The valueChangeListener gets fired way too many times going from true to false back to true. Not sure what I'm doing wrong here and would really appreciate some help.
Thank you

Ok I figured it out! It was a very easy solution all I had to change was
checkBox.addValueChangeListener(new CheckBox.ValueChangeListener() {});
I was referencing some the following properties class instead:
properties.ValueChangeListener();

Related

Is there a way to detect button click in another method?

Like
#FXML
void start() {
// for game loop
while(!winning) {
if (attack.clicked()) attack();
else if (defend.clicked()) defend();
}
}
Can it possible to do this? Thanks.
You can assisgn true or false to a flag when when button click(at the button's click handler method). Your sintax may be different at click handler, but your code like this.
private boolean isAttackButtonClicked = false;
public void attackButtonclickHandler(ClickEvent e){
...
isAttackButtonClicked = true;
...
}

Can we change GWT Bootstrap 3 ButtonCell Icon Dynamically?

I have a GWT bootstrap 3 button as a ButtonCell created with IconType and ButtonType:
public abstract class ButtonColumn<T> extends Column<T, String> {
public ButtonColumn(IconType iconType, ButtonType buttonType) {
this(new ButtonCell(buttonType, iconType));
}
}
So when I create the button, I do
new ButtonColumn<Object>(IconType.PLAY, ButtonType.SUCCESS) {
#Override
public void onClick(Object obj) {
doStuff(obj);
}
};
I want to change my button IconType onClick. Is it possible to achieve it?
And can I create a custom IconType extending the GWT IconType Enum? I wanted to put an animated icon (like a loading icon).
Well, you can not change the button's icon in a row, especially when you create the whole column with an icon already specified. But you can redraw() a row and this could be a way to achieve what you want.
I use AbstractCell to render a button and onBrowserEvent:
first create an AbstractCell with ClickEvent in consumedEvents parameter
in the render() method render a button based on the clicked state
in the onBrowserEvent() method change the clicked state and re-render the row
The clicked state is best to be kept in the table's underlying data type so it is available for each row.
Here is a complete working example code:
final CellTable<TableType> table = new CellTable<TableType>();
AbstractCell<TableType> buttonCell = new AbstractCell<ButtonCellTest.TableType>(ClickEvent.getType().getName()) {
#Override
public void render(Context context, TableType value, SafeHtmlBuilder sb) {
Button button = new Button();
button.setType(ButtonType.SUCCESS);
button.setSize(ButtonSize.SMALL);
button.add(new Icon(value.isClicked() ? IconType.CHECK : IconType.TIMES));
sb.append(SafeHtmlUtils.fromTrustedString(button.toString()));
}
#Override
public void onBrowserEvent(Context context, Element parent, TableType value, NativeEvent event, ValueUpdater<TableType> valueUpdater) {
value.setClicked(!value.isClicked());
// ... do stuff...
table.redrawRow(context.getIndex());
}
};
table.addColumn(new Column<TableType, TableType>(buttonCell) {
#Override
public TableType getValue(TableType object) {
return object;
}
});
ArrayList<TableType> rowData = new ArrayList<TableType>();
rowData.add(new TableType("row 1"));
rowData.add(new TableType("row 2"));
...
table.setRowData(rowData);
And example table's data type keeping the clicked state:
public class TableType {
String text;
boolean clicked = false;
public TableType(String text) {
this.text = text;
}
public String getText() {
return text;
}
public boolean isClicked() {
return clicked;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
As for extending the IconType enum - no, you can not extend an enum in Java. See this question for example: Can enums be subclassed to add new elements?.
You could try to add your own CSS class but this should be asked as another question to get precise answers.

How to know if a ComboBox ValueChangeListener is triggered from select or from the user

Suposse that I define a ComboBox like this:
ComboBox myCombo = new ComboBox();
myCombo.addValueChangeListener(event -> {
//Some code
});
And then, after adding items to the ComboBox I select one:
myCombo.select(someItem);
Is there any way from witch I can know if the code inside the value change listener is executed because of a calling to myCombo.select(someItem) or because the user changes the value of the ComboBox?
We simply use a flag such as _internalChange and we check it in the listener function. Not cool enough but it works :) A reusable solution would be to create a new component.
boolean _internalChange = false;
void init(){
ComboBox myCombo = new ComboBox();
myCombo.addValueChangeListener(event -> {
if(_internalChange) {
// do something
} else {
// do something else
}
});
}
void selectMyCombo(Object value){
_internalChange = true;
myCombo.select(stuff)
_internalChange = false;
}
I think all you need to do is this. If you haven't added the ValueChangeListener when you select the initial value the listener won't get triggered.
ComboBox myCombo = new ComboBox();
// add items to the ComboBo
myCombo.select(someItem);
myCombo.addValueChangeListener(event -> {
//Some code
});

GWT/ Textbox- single AND double click handler options? possible?

I have created a popup box that extends DialogBox and uses a cellTable that contains a list of values, one of which will be selected and inserted into a textBox.
-I have an onSelectionChange handler which is fired when one of the rows is clicked.
-I have an onDoubleClick handler which is fired when the same rows are double clicked.
both work when the other is commented out. But when they are both in the live code, whichever one is written first gets overwritten by the other one and no longer gets called.
Any way around this?
Code snipbit:
final SingleSelectionModel<popUpBoxContent> selectionModel= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
public void onSelectionChange (selectionChangeEvent event){
//Do something
}});
final SingleSelectionModel<popUpBoxContent> selectionModel2= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionMode2);
cellTable.addDomHandler(new DoubleClickHandler(){
public void onDoubleClick(final DoubleClickEvent event){
//Do something else
}},
DoubleClickEvent.getType());
Thank you!
Yes they get overwritten from what I can see in the snippet. Assuming "popUpBoxContent" is the data type with which the CellTable (I presume cellTable is a CellTable) is being populated you could try this and see if it works:
final SingleSelectionModel<PopUpBoxContent> selectionModel = new SingleSelectionModel<PopUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(final DoubleClickEvent event) {
PopUpBoxContent selected = selectionModel.getSelectedObject();
if (selected != null) {
System.out.println("double clicked");
}
}
},
DoubleClickEvent.getType());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
System.out.println("clicked");
}
});

TextField selectAll works half the time

I'm using Vaadin and have a set of textfields in a form. The textfields has focusListeners that triggers a method that focuses all the text in the texfield if there is any.
My problem though is that the auto selection works literally half the time. If I paste some text in a textfield, clicks outside the textfield and then clicks inside it the text will be selected. However, if I click outside again and then in the textfield the text will for a fraction of a second be selected to then only having the input marker where ever it was I clicked in the text.
Here's the code:
class FormTextField extends FormLayout {
private static final long serialVersionUID = -2738069810605965508L;
String caption;
final STextField textField = Cf.formTextField(caption, "", 22);
public FormTextField(String textField) {
addStyleName("panelform");
setWidth(formWidth, UNITS_EM);
this.textField.setCaption(textField);
this.textField.setImmediate(true);
this.textField.addListener(new FieldEvents.FocusListener() {
#Override
public void focus(FocusEvent event) {
textFieldSelectAll();
}
});
addComponent(this.textField);
}
private void textFieldSelectAll() {
this.textField.selectAll();
}
public STextField getTextField() {
return textField;
}
}
}
I'm very interested to know if someone of you are familiar with this problem and have been able to sort it out?
If you want any more information from me then please ask!
I think you need to declare your FormTextField/STextField immediate too.
Hope this helps.

Categories