JavaFX bidirectional binding of choicebox - java

My question is simple, is there any way of making a bidirectional bind between a choicebox's items and an arraylist (that can be added to and removed from, from another class)
My SettingsService contains a simple ArrayList containing User objects. New items can be added from other classes and places in my application.
If I add a user to this list, how can the new item automatically appear in the choicebox?
Example:
Viewmodel
public class ViewModel {
private SettingsService settings = new SettingsService();
public final ObjectProperty<ObservableList<User>> userChoiceBoxItems = new SimpleObjectProperty<>();
public ViewModel() {
userChoiceBoxItems.setValue(FXCollections.observableArrayList(settings.getUsers()))
}
}
View
public class View {
private ViewModel viewModel = new ViewModel();
#FXML
ChoiceBox<User> userChoiceBox;
#FXML
public void initialize() {
userChoiceBox.itemsProperty().bindBidirectional(viewModel.userChoiceBoxItems);
}
Thanks for the help

create a new ObservableList as
ObservableList<User> tempList = FXCollections.observableArrayList()
and add all items to the list as
templist.addAll(userChoiceBoxItems)
and then bidirectionally bind

Related

How to integrate the model part of a program with FXML-Controller? [duplicate]

I'm new to the GUI world/OO design pattern and I want to use MVC pattern for my GUI application, I have read a little tutorial about MVC pattern, the Model will contain the data, the View will contain the visual element and the Controller will tie between the View and the Model.
I have a View that contains a ListView node, and the ListView will be filled with names, from a Person Class (Model). But I'm a little confused about one thing.
What I want to know is if loading the data from a file is the responsibility of the Controller or the Model?? And the ObservableList of the names: should it be stored in the Controller or the Model?
There are many different variations of this pattern. In particular, "MVC" in the context of a web application is interpreted somewhat differently to "MVC" in the context of a thick client (e.g. desktop) application (because a web application has to sit atop the request-response cycle). This is just one approach to implementing MVC in the context of a thick client application, using JavaFX.
Your Person class is not really the model, unless you have a very simple application: this is typically what we call a domain object, and the model will contain references to it, along with other data. In a narrow context, such as when you are just thinking about the ListView, you can think of the Person as your data model (it models the data in each element of the ListView), but in the wider context of the application, there is more data and state to consider.
If you are displaying a ListView<Person> the data you need, as a minimum, is an ObservableList<Person>. You might also want a property such as currentPerson, that might represent the selected item in the list.
If the only view you have is the ListView, then creating a separate class to store this would be overkill, but any real application will usually end up with multiple views. At this point, having the data shared in a model becomes a very useful way for different controllers to communicate with each other.
So, for example, you might have something like this:
public class DataModel {
private final ObservableList<Person> personList = FXCollections.observableArrayList();
private final ObjectProperty<Person> currentPerson = new SimpleObjectPropery<>(null);
public ObjectProperty<Person> currentPersonProperty() {
return currentPerson ;
}
public final Person getCurrentPerson() {
return currentPerson().get();
}
public final void setCurrentPerson(Person person) {
currentPerson().set(person);
}
public ObservableList<Person> getPersonList() {
return personList ;
}
}
Now you might have a controller for the ListView display that looks like this:
public class ListController {
#FXML
private ListView<Person> listView ;
private DataModel model ;
public void initModel(DataModel model) {
// ensure model is only set once:
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
listView.setItems(model.getPersonList());
listView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) ->
model.setCurrentPerson(newSelection));
model.currentPersonProperty().addListener((obs, oldPerson, newPerson) -> {
if (newPerson == null) {
listView.getSelectionModel().clearSelection();
} else {
listView.getSelectionModel().select(newPerson);
}
});
}
}
This controller essentially just binds the data displayed in the list to the data in the model, and ensures the model's currentPerson is always the selected item in the list view.
Now you might have another view, say an editor, with three text fields for the firstName, lastName, and email properties of a person. It's controller might look like:
public class EditorController {
#FXML
private TextField firstNameField ;
#FXML
private TextField lastNameField ;
#FXML
private TextField emailField ;
private DataModel model ;
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
model.currentPersonProperty().addListener((obs, oldPerson, newPerson) -> {
if (oldPerson != null) {
firstNameField.textProperty().unbindBidirectional(oldPerson.firstNameProperty());
lastNameField.textProperty().unbindBidirectional(oldPerson.lastNameProperty());
emailField.textProperty().unbindBidirectional(oldPerson.emailProperty());
}
if (newPerson == null) {
firstNameField.setText("");
lastNameField.setText("");
emailField.setText("");
} else {
firstNameField.textProperty().bindBidirectional(newPerson.firstNameProperty());
lastNameField.textProperty().bindBidirectional(newPerson.lastNameProperty());
emailField.textProperty().bindBidirectional(newPerson.emailProperty());
}
});
}
}
Now if you set things up so both these controllers are sharing the same model, the editor will edit the currently selected item in the list.
Loading and saving data should be done via the model. Sometimes you will even factor this out into a separate class to which the model has a reference (allowing you to easily switch between a file-based data loader and a database data loader, or an implementation that accesses a web service, for example). In the simple case you might do
public class DataModel {
// other code as before...
public void loadData(File file) throws IOException {
// load data from file and store in personList...
}
public void saveData(File file) throws IOException {
// save contents of personList to file ...
}
}
Then you might have a controller that provides access to this functionality:
public class MenuController {
private DataModel model ;
#FXML
private MenuBar menuBar ;
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
}
#FXML
public void load() {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(menuBar.getScene().getWindow());
if (file != null) {
try {
model.loadData(file);
} catch (IOException exc) {
// handle exception...
}
}
}
#FXML
public void save() {
// similar to load...
}
}
Now you can easily assemble an application:
public class ContactApp extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
FXMLLoader listLoader = new FXMLLoader(getClass().getResource("list.fxml"));
root.setCenter(listLoader.load());
ListController listController = listLoader.getController();
FXMLLoader editorLoader = new FXMLLoader(getClass().getResource("editor.fxml"));
root.setRight(editorLoader.load());
EditorController editorController = editorLoader.getController();
FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
root.setTop(menuLoader.load());
MenuController menuController = menuLoader.getController();
DataModel model = new DataModel();
listController.initModel(model);
editorController.initModel(model);
menuController.initModel(model);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
As I said, there are many variations of this pattern (and this is probably more a model-view-presenter, or "passive view" variation), but that's one approach (one I basically favor). It's a bit more natural to provide the model to the controllers via their constructor, but then it's a lot harder to define the controller class with a fx:controller attribute. This pattern also lends itself strongly to dependency injection frameworks.
Update: full code for this example is here.
If you are interested in a tutorial on MVC in JavaFX, see:
The Eden Coding tutorial: How to apply MVC in JavaFX
What i want to know is that if loading the data from a file is the responsibility of the Controller Or the model?
For me the model is only responsible of bringing the required data structures that represent the business logic of the application.
The action of loading that data from any source should be done by the Controller Layer. You could also use the repository pattern, which can help you in abstracting from the type of source when you are acessing the data from the view. With this implemented you should not care if the Repository implementation is loading the data from file, SQL, NoSQL, Web service ...
And the ObservableList of the names will be stored in the controller or the model?
For me the ObservableList is part of the View. It is the kind of data structure you can bind to JavaFX controls. So for example a ObservableList<String> could be populated with Strings from the model but the ObservableList reference should be an attribute of some View´s class.
In JavaFX it's very pleasant to bind JavaFX controls with Observable Properties backed by domain objects from the model.
You could also have a look to viewmodel concept. For me a JavaFX bean backed by a POJO could be considered as a view-model, you could see it as a model object ready to be presented in the view. So for example if your view needs to show some total value calculated from 2 model attributes, this total value could be an attribute of the view-model. This attribute would not be persisted and it would be calculated any time you show the view.

delete an item from a listview using a button on the listitem [duplicate]

I'm new to the GUI world/OO design pattern and I want to use MVC pattern for my GUI application, I have read a little tutorial about MVC pattern, the Model will contain the data, the View will contain the visual element and the Controller will tie between the View and the Model.
I have a View that contains a ListView node, and the ListView will be filled with names, from a Person Class (Model). But I'm a little confused about one thing.
What I want to know is if loading the data from a file is the responsibility of the Controller or the Model?? And the ObservableList of the names: should it be stored in the Controller or the Model?
There are many different variations of this pattern. In particular, "MVC" in the context of a web application is interpreted somewhat differently to "MVC" in the context of a thick client (e.g. desktop) application (because a web application has to sit atop the request-response cycle). This is just one approach to implementing MVC in the context of a thick client application, using JavaFX.
Your Person class is not really the model, unless you have a very simple application: this is typically what we call a domain object, and the model will contain references to it, along with other data. In a narrow context, such as when you are just thinking about the ListView, you can think of the Person as your data model (it models the data in each element of the ListView), but in the wider context of the application, there is more data and state to consider.
If you are displaying a ListView<Person> the data you need, as a minimum, is an ObservableList<Person>. You might also want a property such as currentPerson, that might represent the selected item in the list.
If the only view you have is the ListView, then creating a separate class to store this would be overkill, but any real application will usually end up with multiple views. At this point, having the data shared in a model becomes a very useful way for different controllers to communicate with each other.
So, for example, you might have something like this:
public class DataModel {
private final ObservableList<Person> personList = FXCollections.observableArrayList();
private final ObjectProperty<Person> currentPerson = new SimpleObjectPropery<>(null);
public ObjectProperty<Person> currentPersonProperty() {
return currentPerson ;
}
public final Person getCurrentPerson() {
return currentPerson().get();
}
public final void setCurrentPerson(Person person) {
currentPerson().set(person);
}
public ObservableList<Person> getPersonList() {
return personList ;
}
}
Now you might have a controller for the ListView display that looks like this:
public class ListController {
#FXML
private ListView<Person> listView ;
private DataModel model ;
public void initModel(DataModel model) {
// ensure model is only set once:
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
listView.setItems(model.getPersonList());
listView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) ->
model.setCurrentPerson(newSelection));
model.currentPersonProperty().addListener((obs, oldPerson, newPerson) -> {
if (newPerson == null) {
listView.getSelectionModel().clearSelection();
} else {
listView.getSelectionModel().select(newPerson);
}
});
}
}
This controller essentially just binds the data displayed in the list to the data in the model, and ensures the model's currentPerson is always the selected item in the list view.
Now you might have another view, say an editor, with three text fields for the firstName, lastName, and email properties of a person. It's controller might look like:
public class EditorController {
#FXML
private TextField firstNameField ;
#FXML
private TextField lastNameField ;
#FXML
private TextField emailField ;
private DataModel model ;
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
model.currentPersonProperty().addListener((obs, oldPerson, newPerson) -> {
if (oldPerson != null) {
firstNameField.textProperty().unbindBidirectional(oldPerson.firstNameProperty());
lastNameField.textProperty().unbindBidirectional(oldPerson.lastNameProperty());
emailField.textProperty().unbindBidirectional(oldPerson.emailProperty());
}
if (newPerson == null) {
firstNameField.setText("");
lastNameField.setText("");
emailField.setText("");
} else {
firstNameField.textProperty().bindBidirectional(newPerson.firstNameProperty());
lastNameField.textProperty().bindBidirectional(newPerson.lastNameProperty());
emailField.textProperty().bindBidirectional(newPerson.emailProperty());
}
});
}
}
Now if you set things up so both these controllers are sharing the same model, the editor will edit the currently selected item in the list.
Loading and saving data should be done via the model. Sometimes you will even factor this out into a separate class to which the model has a reference (allowing you to easily switch between a file-based data loader and a database data loader, or an implementation that accesses a web service, for example). In the simple case you might do
public class DataModel {
// other code as before...
public void loadData(File file) throws IOException {
// load data from file and store in personList...
}
public void saveData(File file) throws IOException {
// save contents of personList to file ...
}
}
Then you might have a controller that provides access to this functionality:
public class MenuController {
private DataModel model ;
#FXML
private MenuBar menuBar ;
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
}
#FXML
public void load() {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(menuBar.getScene().getWindow());
if (file != null) {
try {
model.loadData(file);
} catch (IOException exc) {
// handle exception...
}
}
}
#FXML
public void save() {
// similar to load...
}
}
Now you can easily assemble an application:
public class ContactApp extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
FXMLLoader listLoader = new FXMLLoader(getClass().getResource("list.fxml"));
root.setCenter(listLoader.load());
ListController listController = listLoader.getController();
FXMLLoader editorLoader = new FXMLLoader(getClass().getResource("editor.fxml"));
root.setRight(editorLoader.load());
EditorController editorController = editorLoader.getController();
FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
root.setTop(menuLoader.load());
MenuController menuController = menuLoader.getController();
DataModel model = new DataModel();
listController.initModel(model);
editorController.initModel(model);
menuController.initModel(model);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
As I said, there are many variations of this pattern (and this is probably more a model-view-presenter, or "passive view" variation), but that's one approach (one I basically favor). It's a bit more natural to provide the model to the controllers via their constructor, but then it's a lot harder to define the controller class with a fx:controller attribute. This pattern also lends itself strongly to dependency injection frameworks.
Update: full code for this example is here.
If you are interested in a tutorial on MVC in JavaFX, see:
The Eden Coding tutorial: How to apply MVC in JavaFX
What i want to know is that if loading the data from a file is the responsibility of the Controller Or the model?
For me the model is only responsible of bringing the required data structures that represent the business logic of the application.
The action of loading that data from any source should be done by the Controller Layer. You could also use the repository pattern, which can help you in abstracting from the type of source when you are acessing the data from the view. With this implemented you should not care if the Repository implementation is loading the data from file, SQL, NoSQL, Web service ...
And the ObservableList of the names will be stored in the controller or the model?
For me the ObservableList is part of the View. It is the kind of data structure you can bind to JavaFX controls. So for example a ObservableList<String> could be populated with Strings from the model but the ObservableList reference should be an attribute of some View´s class.
In JavaFX it's very pleasant to bind JavaFX controls with Observable Properties backed by domain objects from the model.
You could also have a look to viewmodel concept. For me a JavaFX bean backed by a POJO could be considered as a view-model, you could see it as a model object ready to be presented in the view. So for example if your view needs to show some total value calculated from 2 model attributes, this total value could be an attribute of the view-model. This attribute would not be persisted and it would be calculated any time you show the view.

JavaFX Combo Box Seeing Instances of warehouses

I have a controller that has a combobox defined as so:
private ComboBox warehouseComboBox;
and a method that populates that combobox in that controller
#FXML
void createWarehouseInstance (ActionEvent event){
ArrayList<BikePart> inventory = new ArrayList<>(); //empty inventory
String whName = warehouseNameField.getText();
warehouse wh = new warehouse(whName, inventory); //create new instance
warehouseComboBox.getItems().add(wh);
warehouseArrayList.add(wh); //add instance to arrayList
warehouseNameField.clear(); //clear text box
}
What I'm trying to do is get the state of that combobox and that determines which instance of a class I would be manipulating.
My attempt is so with this method. Where I show a list of Bicycle parts depending on the instance selected with the combo box.
#FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Object selection = warehouseComboBox.getSelectionModel().getSelectedItem();
if (warehouseArrayList.contains(selection)){
bikePartList.appendText(warehouse.printAll()); //what do I do here?
}
}
For context, Warehouses hold BikeParts, and I'm figuring out that if I want to dynamically create warehouses, I can't make this static
If I understand what you're trying to do:
First declare your ComboBox with the proper type:
private ComboBox<Warehouse> warehouseComboBox;
and then you can do
#FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Warehouse selectedWarehouse = warehouseComboBox.getSelectionModel().getSelectedItem();
// I think this is what you're trying to do:
bikePartList.appendText(selectedWarehouse.printAll()); //what do I do here?
}
Note that I changed your class name to conform to standard naming conventions.

JavaFX Line Chart [duplicate]

I have a class Foo which just load the FXML and create the scene.
In the FXML, I set the controller to be FooController (fx:controller="FooController")
And I add a MenuButton:
<MenuButton fx:id="menuButton" layoutX="264.1875" layoutY="146.5" mnemonicParsing="false" text="MenuButton" />
And I try to set the menuButton in the FooController:
public class FooController implements Initializable{
#FXML
final MenuButton menuButton = new MenuButton("Modalities");
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
final ObservableList<CheckMenuItem> listFilter = FXCollections.observableArrayList();
final MenuButton menuButton = new MenuButton("Modalities");
CheckMenuItem item1 = new CheckMenuItem("T1");
CheckMenuItem item2 = new CheckMenuItem("T1C");
CheckMenuItem item3 = new CheckMenuItem("T2");
listFilter.addAll(item, item2, item3);
menuButton.getItems().addAll(listFilter);
menuButton.setId("menuButton");
}
}
But despite of setting everything for the MenuButton it doesn't display any of the CheckMenuItems in the GUI.
How can I load those items in menuButton defined in the FXML?
Never set an #FXML initialized value to a new value.
In your posted code, you are doing this twice, when you should not be doing it at all.
The FXMLLoader will create new items within the hierarchy of the component it instantiates and inject references to these new items into your controller. If you then set these references to new values, the new values will never be included in the displayed component hierarchy unless you specifically add them there, which kind of defeats the purpose of using FXML in the first place as it ignores things defined in your FXML file.
What you should have is:
public class FooController {
#FXML
MenuButton menuButton;
public void initialize() {
menuButton.getItems().addAll(
FXCollections.observableArrayList(
new CheckMenuItem("T1"),
new CheckMenuItem("T1C"),
new CheckMenuItem("T2")
)
);
}
}
Also note that if your CheckMenuItems in the above code are static rather than a dynamic list, then you could just define them all in your FXML document instead of creating them in code.
Note: This is a duplicate question and has been asked before (not the exact question, but the substance of it), but my google skills couldn't dig up some of the duplicates.

ActionListener does not work to move data from on JList to the Other JList by the button

My program is based on a API. I got a JList with and a model that has a some names. And a selectListener to get the seleted item and a button to send that item to the other window which has another Here is my first list:
First List (window) and send the items to the other list.
final DefaultListModel<String> Names = new DefaultListModel<String>();
final JList<MyAPI> Places = new JList(Names);
private JList<MyAPI> locList;
private DefaultListModel<MyAPI> favourites;
public AddLocation(JList<MyAPI> locList, DefaultListModel<MyAPI> favourites){
this.locList = locList;
this.favourites = favourites;
}
addThis.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object chose = Places.getSelectedValue();
favourites.addElement((MyAPI) chose); // error in this line
}
});
And this is the other window that selected items should be added to here:
final DefaultListModel<MyAPI> favourites;
final JList<MyAPI> locList;
favourites = new DefaultListModel<MyAPI>();
locList = new JList<MyAPI>(favourites);
So now both windows loads and the first list loads with its names in it. but when I press the button add this, it gives error and points to this line:
favourites.addElement((MyAPI) chose);
How can I solve it?
Your first model is defined like this:
final DefaultListModel<String> Names ...;
Your second model is defined like this:
final DefaultListModel<MyAPI> favourites;
Your first list model conains String instances, your second model contains MyAPI instances. So when this line is executed:
favourites.addElement((MyAPI) chose);
you are trying to make a MyAPI out of a String, which does not work and probably a ClassCastException is thrown.
Either you need to declare the second list model as final DefaultListModel<String> favourites; or you create an instance of MyAPI based on the selected String (new MyAPI(chose) ?).

Categories