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

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");

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));

JavaFX Change list of labels on button click

I have a list of Labels in a JavaFX app that I've preset with a holder value as such:
for(int i = 0; i < 4; i++) {
lblUserNames.add(new Label("Username goes here"));
}
and I'm trying to change the label to display the username on the click of a button by using
public void setUsername(int index, String lblUserName) {
this.lblUserNames.set(index, new Label(lblUserName));
}
But it's still showing the holder text instead of updating to show the usernames; and yet when I print out the list of labels, the values have indeed changed.
I had it working before when I had just an array of labels (Label[]) and could use ".setText(lblUserName). I changed it to an ArrayList so that more users can be added and the code wouldn't have to change much, but now I can't use setText() anymore.
I've seen similar questions for changing the text for just a Label but the solution is to use setText() which won't work with an ArrayList of Labels. Is there any way to update and replace the holder text with the new labels for an ArrayList?
When you replace the label in your list, you are not replacing it in the UI. The original label is still shown in the UI. Just call setText(...) on the existing label:
public void setUsername(int index, String lblUserName) {
this.lblUserNames.get(index).setText(lblUserName);
}

Dimension field in AX 2009 report dialog?

Under ax 2009 my requirement is to open a dialog box when I opens a report and it should show a drop down. So currently my drop down is SiteId from InventSite table. As show in code below.
public class ReportRun extends ObjectRun
{
//Dialog
DialogField dfSiteName;
//Range
InventSiteId siteName;
}
public boolean getFromDialog()
{
;
siteName = dfSiteName.value();
return true;
}
public Object dialog(Object _dialog)
{
DialogRunBase dialog;
FormDateControl siteNameControl;
;
dialog = super(_dialog);
dialog.caption("Sales Overview Range Dialog");
dialog.addGroup("Selec Range");
dfSiteName = dialog.addField(typeid(InventSiteId),"Site","Select Range");
siteNameControl = dfSiteName.control();
siteNameControl.mandatory(true);
return dialog;
}
Everything is working fine with this code. Now instead drop down of SiteId from InventSite table in dialog box I want drop down of Dimension[1] from InventSite table in dialog box. I am not able to do that. Please guide me on this.
If you code work fine and you want to add only the Dimension[1] from inventSite table go to AOT\Data Dictionary\Tables\InventSite\Field Groups\AutoLookup here you there you will see SiteId and Name fields. You need to add new field then go to properties of this new field and select in property DataField the field that you need.
If you add this new field will be visible in all lookups for InventSiteId edt.

How do I create a JavaFX Alert with a check box for "Do not ask again"?

I would like to use the standard JavaFX Alert class for a confirmation dialog that includes a check box for "Do not ask again". Is this possible, or do I have to create a custom Dialog from scratch?
I tried using the DialogPane.setExpandableContent() method, but that's not really what I want - this adds a Hide/Show button in the button bar, and the check box appears in the main body of the dialog, whereas I want the check box to appear in the button bar.
Yes, it is possible, with a little bit of work. You can override DialogPane.createDetailsButton() to return any node you want in place of the Hide/Show button. The trick is that you need to reconstruct the Alert after that, because you will have got rid of the standard contents created by the Alert. You also need to fool the DialogPane into thinking there is expanded content so that it shows your checkbox. Here's an example of a factory method to create an Alert with an opt-out check box. The text and action of the check box are customizable.
public static Alert createAlertWithOptOut(AlertType type, String title, String headerText,
String message, String optOutMessage, Consumer<Boolean> optOutAction,
ButtonType... buttonTypes) {
Alert alert = new Alert(type);
// Need to force the alert to layout in order to grab the graphic,
// as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Node graphic = alert.getDialogPane().getGraphic();
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {
#Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
optOut.setText(optOutMessage);
optOut.setOnAction(e -> optOutAction.accept(optOut.isSelected()));
return optOut;
}
});
alert.getDialogPane().getButtonTypes().addAll(buttonTypes);
alert.getDialogPane().setContentText(message);
// Fool the dialog into thinking there is some expandable content
// a Group won't take up any space if it has no children
alert.getDialogPane().setExpandableContent(new Group());
alert.getDialogPane().setExpanded(true);
// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
alert.setTitle(title);
alert.setHeaderText(headerText);
return alert;
}
And here is an example of the factory method being used, where prefs is some preference store that saves the user's choice
Alert alert = createAlertWithOptOut(AlertType.CONFIRMATION, "Exit", null,
"Are you sure you wish to exit?", "Do not ask again",
param -> prefs.put(KEY_AUTO_EXIT, param ? "Always" : "Never"), ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().filter(t -> t == ButtonType.YES).isPresent()) {
System.exit();
}
And here's what the dialog looks like:

target and display an object of list control in flex

i have made a list control. i want to display the name of the objects in it in a text control box
the code i am using here is
public function add(event:MouseEvent):void
{
var str:String;
str = mylistcontrol.dataProvider.getItemAt(0).toString();
mytextarea.text += str+ "has been added";
mytextarea.text += "\n";
}
The problem with this code is i am using index value of 0. however i want to display the name of object on which i have clicked or which is highlighted.
any ideas and thoughts?
When you say the name of the object do you mean the name of the ItemRenderer?
If that's the case one method you could use involves creating a custom event and a custom item renderer...
Create a custom ItemRenderer when clicked dispatch your CustomEvent which would a have a data property into which you can put anything you like.

Resources