How can I do this MVP model view presenter

Trying to implement this design pattern but need some help, my view class extends Application and has a start method inside it how can I access this and launch the view through my main class ?
19 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
I have more classes wym this is my view:
package MVP.View;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class View extends Application {
private ViewController viewController;

public View() throws Exception {
viewController = new ViewController();
}

public ViewController getViewController() {
return this.viewController;
}

@Override
public void start(Stage stage) throws Exception {
FXMLLoader load = new FXMLLoader(getClass().getResource("fxml/mainview.fxml"));
load.setController(viewController);
Parent parent = load.load();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
Application.launch();
}
}
package MVP.View;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class View extends Application {
private ViewController viewController;

public View() throws Exception {
viewController = new ViewController();
}

public ViewController getViewController() {
return this.viewController;
}

@Override
public void start(Stage stage) throws Exception {
FXMLLoader load = new FXMLLoader(getClass().getResource("fxml/mainview.fxml"));
load.setController(viewController);
Parent parent = load.load();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
Application.launch();
}
}
this is my ViewController:
imports...

public class ViewController implements Initializable {
@FXML
private ImageView planMetro;

@FXML
private ImageView logoStib;

@FXML
private TableView table;

@FXML
private TableColumn stationsCol;

@FXML
private TableColumn stationsLignes;

@FXML
private ChoiceBox<StationsDto> choiceOrigin;

@FXML
private ChoiceBox<StationsDto> choiceDestination;

@FXML
private Button searchBtn;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
initImages();
}

public void setSearchButton(boolean active) {
searchBtn.setDisable(active);
}

public void addButtonHandler(Presentation presentation) {
SearchButtonHandler handler = new SearchButtonHandler(presentation);
searchBtn.setOnAction(handler);
}

public void setCB(List<StationsDto> list) {
choiceDestination.setItems(FXCollections.observableList(list));
choiceOrigin.setItems(FXCollections.observableList(list));
}

private void initImages() {
logoStib.setImage(new Image(readFile("img/logostib.png")));
planMetro.setImage(new Image(readFile("img/metroplan.png")));
}

private InputStream readFile(String url) {
File file = new File(getClass().getResource(url).getFile());
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Couldn't find file at: " + url + ". Error: " + e.getMessage());
}
return is;
}
}
imports...

public class ViewController implements Initializable {
@FXML
private ImageView planMetro;

@FXML
private ImageView logoStib;

@FXML
private TableView table;

@FXML
private TableColumn stationsCol;

@FXML
private TableColumn stationsLignes;

@FXML
private ChoiceBox<StationsDto> choiceOrigin;

@FXML
private ChoiceBox<StationsDto> choiceDestination;

@FXML
private Button searchBtn;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
initImages();
}

public void setSearchButton(boolean active) {
searchBtn.setDisable(active);
}

public void addButtonHandler(Presentation presentation) {
SearchButtonHandler handler = new SearchButtonHandler(presentation);
searchBtn.setOnAction(handler);
}

public void setCB(List<StationsDto> list) {
choiceDestination.setItems(FXCollections.observableList(list));
choiceOrigin.setItems(FXCollections.observableList(list));
}

private void initImages() {
logoStib.setImage(new Image(readFile("img/logostib.png")));
planMetro.setImage(new Image(readFile("img/metroplan.png")));
}

private InputStream readFile(String url) {
File file = new File(getClass().getResource(url).getFile());
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Couldn't find file at: " + url + ". Error: " + e.getMessage());
}
return is;
}
}
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
dont understadn this give an example lps
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
i know what mvc is only difference between mvp and mvc is observer pattern in mvp presenter observers model and updates view in mvc view observes model and updates itself its kinda just to remove completely the logic from the view in mvp and in mvc view has some logic for displaying etc
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
public class Main {
public static void main(String[] args) {
View view = new View();
Model model = new Model();
Controller controller = new Controller(model, view);

view.displayQuestion();
controller.captureInput();
}
}

public class Controller {
Model model;
View view;

Controller(Model model, View view) {
this.model = model;
this.view = view;
}

captureInput() {
Scanner keyboard = new Scanner(System.in);
List<String> words = new ArrayList<>();
While (view.getTotalWords() > 0) {
view.askQuestion();
String word = keyboard.nextLine();
words.add(word);
}
model.
}
}

public class View implements Observer {
totalWords;

View() {
totalWords = 5;
}

int getTotalWords() {
return totalWords;
}

displayQuestion(){
sout("Introduce " + totalWords + " words");
}

askQuestion() {
sout("Introduce " + totalWords + " words");
totalWords--;
}

update(word) {
sout("Your random word: " + word);
}
}

public class Model implements Observable {
Random random;
List<String> words;
List<Observer> observers;

Model(){
random = new Random();
words = new ArrayList<>();
}

void randomWord() {
String word = words.get(random.nextInt(5))
notifyObservers(word);
}

addObserver(Observer observer) {
observers.add(observer);
}

removeObserver(Observer observer) {
observers.remove(observer);
}

notifyObservers(word) {
observers.forEach((s) -> s.update(word));
}
}
public class Main {
public static void main(String[] args) {
View view = new View();
Model model = new Model();
Controller controller = new Controller(model, view);

view.displayQuestion();
controller.captureInput();
}
}

public class Controller {
Model model;
View view;

Controller(Model model, View view) {
this.model = model;
this.view = view;
}

captureInput() {
Scanner keyboard = new Scanner(System.in);
List<String> words = new ArrayList<>();
While (view.getTotalWords() > 0) {
view.askQuestion();
String word = keyboard.nextLine();
words.add(word);
}
model.
}
}

public class View implements Observer {
totalWords;

View() {
totalWords = 5;
}

int getTotalWords() {
return totalWords;
}

displayQuestion(){
sout("Introduce " + totalWords + " words");
}

askQuestion() {
sout("Introduce " + totalWords + " words");
totalWords--;
}

update(word) {
sout("Your random word: " + word);
}
}

public class Model implements Observable {
Random random;
List<String> words;
List<Observer> observers;

Model(){
random = new Random();
words = new ArrayList<>();
}

void randomWord() {
String word = words.get(random.nextInt(5))
notifyObservers(word);
}

addObserver(Observer observer) {
observers.add(observer);
}

removeObserver(Observer observer) {
observers.remove(observer);
}

notifyObservers(word) {
observers.forEach((s) -> s.update(word));
}
}
I would do something like this sec im completing
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yeah im adding that rn ah not the interface i was adding to the model methods
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
why wopuld they not be hot swappable by simply adding the methods and attributes instead of completely new interfaces ?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
ah I see yeah I see now ty
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
I could yeah I will do so after eating im kinda hungry
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot7mo ago
Post Closed
This post has been closed by <@1200560437331239014>.
Want results from more Discord servers?
Add your server