Commit 966c601d authored by ElaFHNW's avatar ElaFHNW
Browse files

MVC for data entry

parent d0ffed36
package mvcGUI;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Controller {
private final Model model;
private final View view;
protected Controller(Model model, View view) {
this.model = model;
this.view = view;
view.newItem.setOnAction(this::handle);
view.saveItem.setOnAction( this::handle);
view.quit.setOnAction( this::handle);
}
public void handle(ActionEvent event) {
Button btn = (Button) event.getSource();
if (btn == view.newItem) {
view.nameField.setText("");
view.socialSecurityNumberField.setText("");
view.addressField.setText("");
view.hobbiesField.setText("");
view.userMessage.setText("");
} else if (btn == view.saveItem) {
String err = "";
String errorText = model.validate(view);
if (errorText.equals("")) {
Model ass2 = new Model();
ass2.setName(view.nameField.getText());
ass2.setSocialSecurityNumber(Integer.parseInt(view.socialSecurityNumberField.getText()));
ass2.setAddress(view.addressField.getText());
ass2.setHobby(view.hobbiesField.getText().split(","));
Model.addToObjectList(ass2);
view.userMessage.setText("added user\n" + ass2.getName());
}
else {
view.userMessage.setText(errorText);
}
} else { // quit button
for (Model ass2: model.getObjectList()) {
System.out.println(ass2);
}
((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}
}
}
package mvcGUI;
import javafx.application.Application;
import javafx.stage.Stage;
public class MVC extends Application {
private View view;
private Controller controller;
private Model model;
public static void main(String[] args) {
launch(args);
}
/**
* Optional. Program initialization can go here, for example, connection to
* a database, or a network server. The primaryStage is not yet available.
*/
@Override
public void init() {
// Nothing to do in this example
}
/**
* Note the dependencies between model, view and controller. Additionally,
* the view needs access to the primaryStage
*/
@Override
public void start(Stage primaryStage) {
// Initialize the GUI
model = new Model();
view = new View(primaryStage, model);
controller = new Controller(model, view);
// Display the GUI after all initialization is complete
view.start();
}
}
package mvcGUI;
import java.util.ArrayList;
import java.util.Arrays;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class Model {
View view;
private String name;
private int socialSecurityNumber;
private String[] hobby;
private String address;
private static ArrayList<Model> objectList = new ArrayList<Model> ();
public Model() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(int socialSecurityNumber) {
this.socialSecurityNumber = socialSecurityNumber;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static void addToObjectList(Model ass2) {
objectList.add(ass2);
}
public ArrayList<Model> getObjectList() {
return objectList;
}
public static int getCount() {
return objectList.size();
}
@Override
public String toString() {
return "Assignment2 [name=" + name + ", ssn=" + socialSecurityNumber + ", hobby=" +
Arrays.toString(hobby) + ", address=" + address
+ "]";
}
public String validate(View view) {
String valError = "";
if (view.nameField.getText().length()<6) {
valError += "Name, ";
}
String ssn = view.socialSecurityNumberField.getText();
if (! (ssn.length()==4 && ssn.matches("[0-9]+"))) {
valError += "SSN, ";
}
if (view.hobbiesField.getText().length()<3) {
valError += "hobby, ";
}
if (view.addressField.getText().length()<15) {
valError += "address, ";
}
if (valError.equals("")) {
return valError;
}
else {
return valError + " incorrect";
}
}
}
\ No newline at end of file
package mvcGUI;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class View {
private final Model model;
private Stage stage;
protected Label userMessage;
protected Button newItem, saveItem, quit;
protected TextField nameField, socialSecurityNumberField, hobbiesField, addressField;
protected View(Stage stage, Model model) {
this.stage = stage;
this.model = model;
// buttons
newItem = new Button("New");
// newItem.setOnAction(this::processNewItemButtonPress);
saveItem = new Button("Save");
// saveItem.setOnAction(this::processSaveButtonPress);
quit = new Button("Output and Quit");
// quit.setOnAction(this::processQuitButtonPress);
Font ft1 = new Font("Verdana",16);
newItem.setFont(ft1);
newItem.setTextFill(Color.BLUE);
saveItem.setFont(ft1);
saveItem.setTextFill(Color.GREEN);
quit.setFont(ft1);
quit.setTextFill(Color.BLACK);
HBox hbButtons = new HBox();
hbButtons.setSpacing(10);
hbButtons.getChildren().addAll(newItem,saveItem,quit);
Label labEntry = new Label("New User");
labEntry.setFont(ft1);
HBox hbEntry = new HBox(labEntry);
Label nameLab = new Label("Name, 6 letters min");
nameField = new TextField();
Label socialSecurityNumberLab = new Label("SSN, 4 digits");
socialSecurityNumberField = new TextField();
Label hobbiesLab = new Label("Hobbies, 3 letters min");
hobbiesField = new TextField();
Label addressLab = new Label("Address, 15 letters min");
addressField = new TextField();
userMessage = new Label("");
userMessage.setTextFill(Color.RED);
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.getColumnConstraints().add(new ColumnConstraints(100)); // column 0 is 100 wide
gridPane.getColumnConstraints().add(new ColumnConstraints(300)); // column 1 is 300 wide
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.add(hbButtons,1,0);
// labels going on the left hand side, column 0
gridPane.add(hbEntry,0,1);
gridPane.add(nameLab,0,2);
gridPane.add(socialSecurityNumberLab,0,3);
gridPane.add(hobbiesLab,0,4);
gridPane.add(addressLab,0,5);
// text fields on the right hand side, column 1
gridPane.add(nameField,1,2);
gridPane.add(socialSecurityNumberField,1,3);
gridPane.add(hobbiesField,1,4);
gridPane.add(addressField,1,5);
gridPane.add(userMessage,1,6);
Scene scene = new Scene(gridPane,500,600);
stage.setScene(scene);
stage.setTitle("My Object GUI");
stage.show();
}
public void start() {
stage.show();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment