JavaFX Combox when something is selected property - javafx

I have this large BooleanBinding.
BooleanBinding uncompleteBinding = txtBarcode.textProperty().isEmpty()
.or(txtNombre.textProperty().isEmpty()
.or(txtPrecioContado.textProperty().isEmpty())
.or(txtPrecioCredito.textProperty().isEmpty())
.or(txtModelo.textProperty().isEmpty()
.or(txtSerie.textProperty().isEmpty()
.or(cboCategoria.selectionModelProperty().isNull()
))));
The purpouse of the BooleanBinding is to enable a save button. It was working fine until i added Combobox into the mix. It doesn't seem to work that way. I tried isNotNull() and itemsProperty() as well.
By defaul't Combobox displays a "-" and nothing is selected. Is requiered that the user selects something, and there is no default selected value allowed.

Use the value property to check the value chosen in the ComboBox and use "-" as prompt text.
Example:
ComboBox<String> cb = new ComboBox<>();
cb.getItems().setAll("A", "B", "C");
cb.setPromptText("-");
Button btn = new Button("Submit");
btn.disableProperty().bind(cb.valueProperty().isNull());

Try this idea:
comboBox.getSelectionModel().selectedIndexProperty().isEqualTo(0);
where your "-" is located under 0 index.

Related

ComboBox selected value not getting translated

My app should have several languages. English is by default. Problem is that if user will switch to different language, everything will be translated except for ComboBox selected value. This is how it looks:
Code behind ComboBox is:
ObservableList<Currency> currencyItem= CurrencyDA.getCurrencies();
currenciesComboBox.setItems(currencyItem);
Callback<ListView<Currency>, ListCell<Currency>> currencyFactory = lv -> new ListCell<Currency>(){
#Override
protected void updateItem(Currency currency, boolean empty){
super.updateItem(currency, empty);
setText(empty ? "" : interfaceBundle.getString("currency_"+currency.getName()));
}
};
currenciesComboBox.setCellFactory(currencyFactory);
currenciesComboBox.setButtonCell(currencyFactory.call(null));
currenciesComboBox.getSelectionModel().selectFirst();
How can I get selected value refreshed?
From the doc
As the ComboBox internally renders content with a ListView, API exists in the ComboBox class to allow for a custom cell factory to be set. For more information on cell factories, refer to the Cell and ListCell classes. It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the 'button' area of the ComboBox, you can set a custom ListCell instance in the button cell property. One way of doing this is with the following code :
//(note the use of setButtonCell):
Callback<ListView<String>, ListCell<String>> cellFactory = ...;
ComboBox comboBox = new ComboBox();
comboBox.setItems(items);
comboBox.setButtonCell(cellFactory.call(null));
comboBox.setCellFactory(cellFactory);
So the only thing you have to add is :
currenciesComboBox.setButtonCell(currencyFactory.call(null));

How do I get a new value from an editable combo box?

I am trying to add a value to an editable combo box called nameComboBox that I created in the Scene Builder.
I populate the combo box with this code:
private ObservableList<String> getNames()
{
return (FXCollections.observableArrayList("Freddy","Kerstin"));
}
..
nameComboBox.getSelectionModel().select(getNames());
I have a Save button defined form the Scene Builder. The code looks like this:
#FXML
private void handleSaveBtn()
{
System.out.println("The new name is " + nameComboBox.getValue());
}
When the scene is displayed, the combo boxes editable field is displayed empty with the two names hidden in the list underneath the empty field, which is what I want to happen.
If then type "Rusty" in the empty field and click a save button all that happens is that the println statement returns
"The new name is null".
If I wanted to do something with the new value, like validate it or store it in a database, how do I get the value that I entered in the editable field?
Try using this instead of .getValue() :
nameComboBox.getEditor().getText()
This returns the value of the textProperty of the TextField (.getEditor()) of the editable ComboBox.
try this
nameComboBox.setItems(getNames());
nameComboBox.setValue("Freddy");

QTableWidget with checkbox

I need to use QTableWidget with checkboxes instead of text in items. Checkbox must be in the center of item.
Examples which I tried work while checkbox is checked. If I uncheck checkbox it disapeares.
You can set the checkbox to be centered with this code:
QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
pMyTableWidget->setCellWidget(0,0,pWidget);
(I don't know if I understood you well here) And if you want to make your checkbox disappear when you uncheck it, you need to connect clicked signal of checkbox to a slot, that will make your checkbox invisible. Use connect method like this:
connect(checkbox,SIGNAL(clicked()),this,SLOT(checkboxClicked()));
You need to create slot checkboxClicked where you will be checking if the checkbox is checked or not. If not then you have to set it invisible. Example:
QCheckBox* Chb = qobject_cast<QCheckBox *>(QObject::sender());
if(!Chb->checked())
Chb->setVisible(false);

Accessing the contextMenu of a spark TextInput component

How can I add custom context menu items to the context menu of a spark TextInput component? I am using Flex 4.5.1.
Assuming I have a spark TextInput called sparktext:
trace(sparktext.contextMenu) // returns null
trace(sparktext.textDisplay.contextMenu) // returns null
Since those returned null, I though I would just create a contextMenu using:
sparktext.contextMenu = new ContextMenu()
//add entries
Or
sparktext.textDisplay.contextMenu = new ContextMenu()
//add entries
Now a contextMenu is created. But the created context menu is a "flash" context menu, displaying items like "play", "zoom" and so on. I have lost entries for items like "copy", "paste" and so on.
I have tried creating a RichEditableText, and its contextMenu property actually contains a ContextMenu object. I am at lost as to why the RichEditableText object, which is stored in TextInput.textDisplay does not contain a ContextMenu object in its contextMenu property.
Can anyone shed some light on this and how I can ADD context menu items to the context menu of a TextInput?
Still couldn't work out why sparktext.textDisplay.contextMenu is NULL.
But here's what I did:
var testmenu:ContextMenu = new ContextMenu();
testmenu.hideBuiltInItems();
testmenu.clipboardMenu = true; //Use this to show the options such as copy, paste and so on.
sparktext.textDisplay.contextMenu = testmenu;

Change databound Drop Down List programmatically

I have a drop down list that is populated in the page load event from a database table.
The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).
Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview
GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;
Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?
=============================================EDIT
actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct
This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?
To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:
var row = GridView1.SelectedRow;
if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
}
Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.
HTH.
You can do
ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;
This will actually set it.

Resources