Advertisement
TobNil

JavaFx-Bilverkstad begining 3.0

Apr 5th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.96 KB | None | 0 0
  1. Main.java
  2.  
  3. import java.io.IOException;
  4.  
  5. import javafx.application.Application;
  6. import javafx.fxml.FXMLLoader;
  7. import javafx.stage.Stage;
  8. import javafx.scene.Scene;
  9. import javafx.scene.layout.AnchorPane;
  10. import javafx.scene.layout.BorderPane;
  11.  
  12. public class Main extends Application {
  13.     private static Stage primaryStage;
  14.    
  15.     @Override
  16.     public void start(Stage primaryStage) {
  17.         Main.primaryStage = primaryStage;
  18.         try {
  19.             showMainScene();
  20.         } catch (IOException e) {
  21.             System.out.println("Error: " + e.getMessage());
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.    
  26.     public static void showMainScene() throws IOException{
  27.         FXMLLoader fxmlLoader = new FXMLLoader();
  28.        
  29.         fxmlLoader.setLocation(Main.class.getResource("/com/example/views/CustomerView.fxml"));
  30.         AnchorPane layout = fxmlLoader.load();
  31.        
  32.         Scene scene = new Scene(layout);
  33.        
  34.         primaryStage.setScene(scene);
  35.        
  36.         primaryStage.show();
  37.     }
  38.    
  39.     public static void showListOfCustomers() throws IOException{
  40.         FXMLLoader fxmlLoader = new FXMLLoader();
  41.        
  42.         fxmlLoader.setLocation(Main.class.getResource("/com/example/views/ListOfCustomersView.fxml"));
  43.        
  44.         BorderPane layout = fxmlLoader.load();
  45.         Scene scene = new Scene(layout);
  46.        
  47.         primaryStage.setScene(scene);
  48.         primaryStage.show();
  49.     }
  50.    
  51.     public static void main(String[] args) {
  52.         launch(args);
  53.     }
  54. }
  55.  
  56. ===============================================================================================================
  57.  
  58. CustomerModel.java
  59.  
  60. public class CustomerModel {
  61.     private Long id;
  62.     private String firstName;
  63.     private String lastName;
  64.     private String phoneNumber;
  65.    
  66.     public CustomerModel(){
  67.        
  68.     }
  69.  
  70.     public Long getId() {
  71.         return id;
  72.     }
  73.  
  74.     public void setId(Long id) {
  75.         this.id = id;
  76.     }
  77.  
  78.     public String getFirstName() {
  79.         return firstName;
  80.     }
  81.  
  82.     public void setFirstName(String firstName) {
  83.         this.firstName = firstName;
  84.     }
  85.  
  86.     public String getLastName() {
  87.         return lastName;
  88.     }
  89.  
  90.     public void setLastName(String lastName) {
  91.         this.lastName = lastName;
  92.     }
  93.  
  94.     public String getPhoneNumber() {
  95.         return phoneNumber;
  96.     }
  97.  
  98.     public void setPhoneNumber(String phoneNumber) {
  99.         this.phoneNumber = phoneNumber;
  100.     }    
  101. }
  102.  
  103. ===============================================================================================================
  104. CustomerController.java
  105.  
  106. import java.sql.SQLException;
  107. import java.util.ArrayList;
  108. import java.util.List;
  109.  
  110. import javax.sql.rowset.JdbcRowSet;
  111. import javax.sql.rowset.RowSetProvider;
  112.  
  113. import com.example.models.CustomerModel;
  114.  
  115.  
  116. public class CustomerController {
  117.     private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  118.     private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/javafx";
  119.     private static final String DATABASE_USERNAME = "root";
  120.     private static final String DATABASE_PASSWORD = "password";
  121.     private JdbcRowSet jdbcRowSet = null;
  122.    
  123.     private final String ID_COLUMN = "id";
  124.     private final String FIRST_NAME_COLUMN = "first_name";
  125.     private final String LAST_NAME_COLUMN = "last_name";
  126.     private final String PHONE_NUMBER_COLUMN = "phone_number";
  127.    
  128.     public CustomerController(){
  129.         try{
  130.             Class.forName(JDBC_DRIVER);
  131.             jdbcRowSet = RowSetProvider.newFactory().createJdbcRowSet();
  132.             jdbcRowSet.setUrl(DATABASE_URL);
  133.             jdbcRowSet.setUsername(DATABASE_USERNAME);
  134.             jdbcRowSet.setPassword(DATABASE_PASSWORD);
  135.             jdbcRowSet.setCommand("SELECT * FROM customers");
  136.             jdbcRowSet.execute();
  137.         } catch (Exception e){
  138.             System.out.println("Error: " + e.getMessage());
  139.         }
  140.     }
  141.    
  142.     public CustomerModel addCustomerToDatabase(CustomerModel customerModel){
  143.         try{
  144.             jdbcRowSet.moveToInsertRow();
  145.             jdbcRowSet.updateString(FIRST_NAME_COLUMN, customerModel.getFirstName());
  146.             jdbcRowSet.updateString(LAST_NAME_COLUMN, customerModel.getLastName());
  147.             jdbcRowSet.updateString(PHONE_NUMBER_COLUMN, customerModel.getPhoneNumber());
  148.             jdbcRowSet.insertRow();
  149.             jdbcRowSet.moveToCurrentRow();
  150.            
  151.         } catch (SQLException e){
  152.             System.out.println("Error: " + e.getMessage());
  153.             try{
  154.                 jdbcRowSet.rollback();
  155.                 customerModel = null;
  156.             } catch(Exception ex){}
  157.         } catch (Exception exception) {
  158.             System.out.println("Error: " + exception.getMessage());
  159.         }
  160.        
  161.         return customerModel;
  162.     }
  163.    
  164.     public List<CustomerModel> getAllCustomers() throws SQLException{
  165.         List<CustomerModel> listOfCustomers = new ArrayList<>();
  166.        
  167.         jdbcRowSet.beforeFirst();
  168.        
  169.         while(jdbcRowSet.next()){
  170.             CustomerModel customerModel = new CustomerModel();
  171.            
  172.             Long id = jdbcRowSet.getLong(ID_COLUMN);
  173.             String firstName = jdbcRowSet.getString(FIRST_NAME_COLUMN);
  174.             String lastName = jdbcRowSet.getString(LAST_NAME_COLUMN);
  175.             String phoneNumber = jdbcRowSet.getString(PHONE_NUMBER_COLUMN);
  176.            
  177.             customerModel.setId(id);
  178.             customerModel.setFirstName(firstName);
  179.             customerModel.setLastName(lastName);
  180.             customerModel.setPhoneNumber(phoneNumber);
  181.            
  182.             listOfCustomers.add(customerModel);
  183.         }
  184.        
  185.         return listOfCustomers;
  186.     }
  187.    
  188. }
  189.  
  190. ===============================================================================================================
  191. CustomerViewController.java
  192.  
  193. import java.io.IOException;
  194. import java.net.URL;
  195. import java.util.ResourceBundle;
  196.  
  197. import com.example.controllers.CustomerController;
  198. import com.example.models.CustomerModel;
  199.  
  200. import application.Main;
  201. import javafx.event.ActionEvent;
  202. import javafx.event.EventHandler;
  203. import javafx.fxml.FXML;
  204. import javafx.fxml.Initializable;
  205. import javafx.scene.control.Button;
  206. import javafx.scene.control.TextField;
  207.  
  208. public class CustomerViewController implements Initializable{
  209.     @FXML private TextField tfId;
  210.    
  211.     @FXML private TextField tfFirstName;
  212.    
  213.     @FXML private TextField tfLastName;
  214.    
  215.     @FXML private TextField tfPhoneNumber;
  216.    
  217.     @FXML private Button btnAddCustomerToDatabase;
  218.    
  219.     @FXML private Button btnShowList;
  220.    
  221.     private CustomerController customerController;
  222.    
  223.     public CustomerViewController(){
  224.        
  225.     }
  226.  
  227.     @Override
  228.     public void initialize(URL location, ResourceBundle resources) {
  229.         customerController = new CustomerController();
  230.        
  231.         assert btnAddCustomerToDatabase != null : "fx:id=\"btnAddCustomerToDatabase\" was not injected.";
  232.         assert btnShowList != null : "Felmeddelande vid null";
  233.        
  234.         btnAddCustomerToDatabase.setOnAction(new EventHandler<ActionEvent>() {
  235.            
  236.             @Override
  237.             public void handle(ActionEvent event) {
  238.                 addCustomerToDatabase();
  239.             }
  240.         });
  241.        
  242.         btnShowList.setOnAction(new EventHandler<ActionEvent>() {
  243.            
  244.             @Override
  245.             public void handle(ActionEvent event) {
  246.                 switchSceneToList();
  247.             }
  248.         });
  249.     }
  250.    
  251.     public void addCustomerToDatabase(){
  252.         CustomerModel customer = getTextFieldData();
  253.        
  254.         customerController.addCustomerToDatabase(customer);
  255.        
  256.         System.out.println("Customer added! Hooray!");
  257.     }
  258.    
  259.     public CustomerModel getTextFieldData(){
  260.         CustomerModel customer = new CustomerModel();
  261.        
  262.         customer.setFirstName(tfFirstName.getText());
  263.         customer.setLastName(tfLastName.getText());
  264.         customer.setPhoneNumber(tfPhoneNumber.getText());
  265.        
  266.         return customer;
  267.     }
  268.    
  269.     public void switchSceneToList(){
  270.         try {
  271.             Main.showListOfCustomers();
  272.         } catch (IOException e) {
  273.             // TODO Auto-generated catch block
  274.             e.printStackTrace();
  275.         }
  276.     }
  277.    
  278. }
  279.  
  280. ===============================================================================================================
  281. ListOfCustomersViewController.java
  282.  
  283. import java.net.URL;
  284. import java.sql.SQLException;
  285. import java.util.List;
  286. import java.util.ResourceBundle;
  287.  
  288. import com.example.controllers.CustomerController;
  289. import com.example.models.CustomerModel;
  290.  
  291. import javafx.collections.FXCollections;
  292. import javafx.collections.ObservableList;
  293. import javafx.fxml.FXML;
  294. import javafx.fxml.Initializable;
  295. import javafx.scene.control.ListView;
  296.  
  297. public class ListOfCustomersViewController implements Initializable{
  298.     @FXML private ListView<String> lvListOfCustomers;
  299.  
  300.     private CustomerController customerController;
  301.  
  302.     public ListOfCustomersViewController() {
  303.        
  304.     }
  305.    
  306.     @Override
  307.     public void initialize(URL location, ResourceBundle resources) {
  308.         customerController = new CustomerController();
  309.         ObservableList<String> observableList = FXCollections.observableArrayList();
  310.         List<CustomerModel> localListOfCustomers;
  311.         try {
  312.             localListOfCustomers = customerController.getAllCustomers();
  313.            
  314.             for(CustomerModel customer : localListOfCustomers){
  315.                 observableList.add("ID: " + customer.getId() + "\n " + customer.getFirstName() + " " + customer.getLastName());
  316.             }
  317.        
  318.         } catch (SQLException e) {
  319.             // TODO Auto-generated catch block
  320.             e.printStackTrace();
  321.         }
  322.         lvListOfCustomers.setItems(observableList);
  323.     }
  324.    
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement