I'm trying the following code but it doesn't compile:
SimpleIntegerProperty startPageProperty = new SimpleIntegerProperty();
TextField startPageField = new TextField();
Bindings.bindBidirectional(
startPageField.textProperty(), startPageProperty, new IntegerStringConverter()
);
The last static method call does not accept these parameters.
Bindings#bindBidirectional expects a StringConverter[Number], you are providing a StringConverter[Integer]. Though it may not be intuitive, you'll have to use a NumberStringConverter instead.
Bindings.bindBidirectional(startPageField.textProperty(),
startPageProperty,
new NumberStringConverter());
While the previous answer is correct, there is another way to solve this, which works better if you want to format numbers in a specific way (e.g. with thousands separators):
var formatter = new TextFormatter<>(new NumberStringConverter("#,###"));
formatter.valueProperty().bindBidirectional(startPageProperty);
startPageField.setTextFormatter(formatter);
The advantage of using a TextFormatter is that it will reformat any number entered by the user when the text field loses focus.
Related
My issue is I have a simple List of Strings say
List<String> names = List.of("Frank","Joe","Eva");
All I want is display it on the UI. With some simple code like
ListComponent lc = new ListComponent.setItems(names);
I have tried it with Table which seems to work but code behind it is a bit boilerplate for this simple task(7-8 line of code).
I have tried also the Grid component and it works well when I want to bind a POJO to it , but with String.class type its a nightmare.
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
it doesnt work because I have to provide getters for the column, which String.class doesnt have for the value. So I did this:
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
listGrid.addColumn(String::toString).setCaption("name");
It works! However unspecified columns also appear in the grid, so now I have 3 columns Byte,Empty,name. And I dont know why. Where are these comes from?
What are the requirements for displaying them? Just to get them on the screen? Is Label enough?
for(String name: names) {
mylayout.addComponent(new Label(name))
}
If you need selection, then maybe ListSelect or ComboBox are the go-to’s.
If you want to avoid the additional columns, one way is to do as was pointed out in a comment, i.e. do removeAllColumns() before you go on creating your own columns.
Another approach would be to do new Grid<>() instead of new Grid<>(String.class). The main difference is that the second constructor uses reflection on the provided class and automatically configures columns for anything that looks like regular Java bean properties.
I would highly prefer to use grid.removeColumnByKey rather than removeAllColumns()
You can also use grid.setColumns to specify order of columns.
I will add link to vaadin documentantion for grid with java examples which is realy helpfull. enter link description here
I did something similar to adding the Strings in TextAreas. Because I needed some formatting, I added the text using StringBuilder.
List<String> details = getDetails();
StringBuilder builder = new StringBuilder();
for (String detail : details) {
TextArea ta = new TextArea();
ta.setSizeFull();
ta.setMaxHeight("100px");
ta.setValue(builder.append(detail).toString());
((Span) content).add(ta);
((Span) content).add(new Hr());
}
The result is like this:
Try out this
final Grid<String> grid = new Grid<>();
grid.setItems(new ArrayList<String>());
grid.addColumn(item -> item).setHeader("Value");
I have a ComboBox in my Vaadin 8 code.
allAtts here is a Set of Attendant-s, and the field theAtt in there is of type String:
private ComboBox<Attendant> theCB = new ComboBox<Attendant>(null,allAtts);
theCB.setEmptySelectionAllowed(false);
Binder<SearchArgs> binder = new Binder<SearchArgs>(SearchArgs.class);
binder.setBean(sas);
binder.forField(theCB).bind("theAtt");
..
VerticalLayout vl = new VerticalLayout(theCB, rb, deleteBtn);
What's more - this exact flow of theCB is a copy-paste from another class i wrote, on the very same type Attendant.
The problem here is the drop-down click on theCB is not working. That is, clicking that little chevron-down icon on the ComboBox is producing no effect. What could this be!?
I tried removing the other items, rb, deleteBtn in vl. Still didn't work.
Nothing else happening to theCB anywhere else.
TIA.
EDIT:
theCB is otherwise functional. typing in the field is allowed, brings the choices and returns the selection accurately.
EDIT-2
https://github.com/vaadin/vaadin-combo-box/issues/680 is not the issue. nothing changing when i pass a non-null label value to the constructor. Besides, it's working fine with null label in that other place in the code.
EDIT-3
Attendant contains several fields, one of which is String att.
SearchArgs composes Attendant in a its field theAtt. Attendant.toString() returns att only.
Once again - this exact same logic is working, as i'm typing these, in another part in the code.
Also note: i since also tried the following without success:
binding theCB on a separate binder, i.e. declare a second binder on sas and use that one for binding theCB
cloning the Attendant objects that go into theCB as data-provider.
I have create a sign up scene in my javafx application. The sign up scene contains several textfields like name, username and password. I am wandering how can I set the textfield of the password to be encrypted in order no one can see the pass. My code is the following:
class MySignUpTextField extends TextField {
public MySignUpTextField() {
setPrefWidth(400.0);
setPrefHeight(60.0);
setStyle("-fx-font: 30 cornerstone; -fx-text-fill: black; -fx-base: #17499F;");
}}
passSignupSLabel = new MySignUpLabel("Password:");
SsignupButtonPane.add(passSignupSLabel , 1, 2);
passSignupSTextField = new MySignUpTextField();
SsignupButtonPane.add(passSignupSTextField , 2, 2);
How can I turn the the passSignupSTextField to be encrypted?
Did you consider using a PasswordField instead of a TextField?
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/PasswordField.html
Ok, it depends on what you mean by 'encrypted'. If you mean 'encrypted' as in scrambling the input before you send information to a database or server, then your question is way too broad. If you want to mask the input of the TextField, there is a really easy solution. Create a new class, MySignUpPasswordField, and make that class extends PasswordField instead of TextField. PasswordField already masks the input for you, so you don't have to do it manually.
Here is where you can learn more about PasswordFields, http://docs.oracle.com/javafx/2/ui_controls/password-field.htm
Use PasswordField instead of TextField, which is subclass of TextField and masks entered characters.
I am using the org.eclipse.swt.widgets.Combo class and I am doing the following
Combo myCombo = new Combo(container, SWT.READ_ONLY);
myCombo.add("1");
myCombo.add("2");
//later on
myCombo.setText(""); //will not work because READ_ONLY
The user will choose an element of the combo, and I am providing a reset button where I want the value to be set to null. However, according to the javadoc, the setText method is ignored when the receiver is READ_ONLY. I like the Combo being read only because I only want the user to select what I provide.. But I want to set the value back to null or "" if possible. Can I do this with a the read only receiver? Or what is another good way of doing this?
Thanks!
Use combo.deselectAll() to reset the selection.
Alternatively you could use:
combo.deselect(combo.getSelectionIndex());
In both cases getSelectionIndex() will return -1 afterwards.
These methods appear as if the Combo supports multi-selection, which it doesn't. However strange they may appear, they do reset the selection.
The best way is to use ComboViewer
List<String> input = new ArrayList<String>();
input.add("1");
input.add("2");
combo = new ComboViewer(container, SWT.READ_ONLY);
combo.setLabelProvider(new LabelProvider());
combo.setContentProvider(ArrayContentProvider.getInstance());
combo.setInput(input);
And to clear it
combo.setSelection(StructuredSelection.EMPTY);
Regards
I've a Form where an property of source Item need to be formatter by an custom format.
Source property (of my own bean) is a Integer, but need to be formatted as a Currency-like format.
I tried to implement my own PropertyFormatter, and setup it inside my FieldFactory.createField for this form as
TextField tf = new TextField("Price");
tf.setPropertyDataSource(new MyPriceFormatter());
return tf;
But as I see from the logs, only format() method is called. But parse() method is never used, and setValue is never called
What's wrong with my code? How to use custom PropertyFomatter for forms? Or how to add custom format for form's field?
After some investigation i found that there is something just replaces my formatter, with an new MethodProperty data source. So i'd implemented my own PriceField, with overrided setPropertyDataSource, that fix this situation. btw, it seems to bee hacky, and i'm still looking for an other way
I have also experienced this problem and solved it another way. Actually I also had to make a textfield formated with a currency :-)
The problem is that the datasource in the PropertyFormatter is null at the time you are creating the fields in the FormFieldFactory. You can instead set the datasource on your field after the FormFieldFactory has been called:
addCountryRatesForm.setFormFieldFactory(new MyFormFieldFactory());
Field internationalRate = addCountryRatesForm.getField("internationalRate");
internationalRate.setPropertyDataSource(new CurrencyFormatter("#0.00 ", currency, internationalRate.getPropertyDataSource()));
So unfortunately with Vaadin you cannot create a TextField that sets its own formatter.