As far as I understand, when creating a project using the FXML JavaFX Maven Archetype, the project is initially designed with the MVC pattern. However, I wanted to convert it to the MVP architecture (don't ask why). Since I have almost no practical experience in this, only a bit of theory, I would like to know how wrong I am in this approach and what mistakes I am making. Below is a description of this attempt.
Methods in classes are irrelevant; it's all conditional. An answer is needed regarding organization and interaction.
If you simply move the model into the controller, essentially it will be the same without extra files?
Screenshot of the program structure:
Code:
public interface Contract {
public interface ExampleView {
void showData(String data);
String getCurrentData();
void switchToSecondary() throws IOException;
void showList();
}
}
In FXML JavaFX, everything operates through the controller but here, the controller duplicates all calls in the presenter.
public class ExampleViewController implements Initializable, Contract.ExampleView {
@FXML
private ListView<String> listView;
@FXML
private Label dataLabel;
@FXML
private Button primaryButton;
private ExampleViewPresenter presenter;
@Override
public void initialize(URL url, ResourceBundle rb) {
presenter = new ExampleViewPresenter(this);
presenter.init();
presenter.setListView(listView);
presenter.setDataLabel(dataLabel);
}
@Override
public void showData(String data) {
}
@Override
public void switchToSecondary() {
presenter.switchToSecondary();
}
@Override
public String getCurrentData() {
return dataLabel.getText();
}
@FXML
@Override
public void showList() {
presenter.showList();
}
}
Presenter:
public class ExampleViewPresenter {
private ExampleView view;
private ExampleModelData model;
private ListView<String> listView;
private ObservableList<String> items = FXCollections.observableArrayList();
private Label dataLabel;
public ExampleViewPresenter(ExampleView view) {
this.view = view;
this.model = new ExampleModelData();
this.model.addDataListner(new ExampleModelDataListner(){
@Override
public void notifyListeners() {
Platform.runLater(() -> dataLabel.setText(model.getName()));
}
});
}
public void init() {
view.showData("PrimaryPresenter");
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
model.setName(">" + Math.random() + "<");
}
}
}).start();
}
public ExampleModelData getUserModel(){
return model;
}
public void setListView(ListView<String> listView) {
this.listView = listView;
}
public void setDataLabel(Label dataLabel) {
this.dataLabel = dataLabel;
}
public ListView<String> getListView() {
return listView;
}
public void showList(){
items.add("item1");
items.add("item2");
items.add("item3");
listView.setItems(items);
}
public void switchToSecondary() {
throw new UnsupportedOperationException("Not supported Yet");
}
}