Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. //Leon Tsoy
  2.  
  3. package sample;
  4.  
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.fxml.FXML;
  8. import javafx.scene.control.ListView;
  9. import javafx.scene.control.SelectionMode;
  10. import javafx.scene.control.TextField;
  11. import javafx.stage.FileChooser;
  12. import javafx.stage.Stage;
  13.  
  14. import java.io.*;
  15.  
  16.  
  17. public class Anzeige {
  18.     @FXML
  19.     private ListView<Person> xadresse;
  20.     @FXML
  21.     private TextField xfirstName;
  22.     @FXML
  23.     private TextField xlastName;
  24.     @FXML
  25.     private TextField xnr;
  26.     @FXML
  27.     private TextField xstreet;
  28.     @FXML
  29.     private TextField xzip;
  30.     @FXML
  31.     private TextField xcity;
  32.  
  33.     private Stage stage2;
  34.     private Popup popup;
  35.     private File file;
  36.     private FileChooser fc = new FileChooser();
  37.  
  38.     public void setStage2(Stage stage2) {
  39.         this.stage2 = stage2;
  40.     }
  41.  
  42.     public void setPopup(Popup pop) {
  43.         this.popup = pop;
  44.     }
  45.  
  46.     private ObservableList<Person> liste = FXCollections.observableArrayList();
  47.     public void initialize() {
  48.  
  49.         xadresse.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
  50.         xadresse.setItems(liste);
  51.  
  52.         xadresse.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> {
  53.             xfirstName.setText(newValue.getFN());
  54.             xlastName.setText(newValue.getLN());
  55.             xnr.setText(newValue.getNr());
  56.             xstreet.setText(newValue.getStreet());
  57.             xzip.setText(newValue.getZip());
  58.             xcity.setText(newValue.getCity());
  59.         }));
  60.     }
  61.  
  62.     public void saveAs() {
  63.         if (file != null){
  64.             try {
  65.                 ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false));
  66.                 outputStream.writeObject(xadresse.getItems().size());
  67.  
  68.                 for (Person p: xadresse.getItems()){
  69.                     outputStream.writeObject(p);
  70.                 }
  71.                 outputStream.close();
  72.             }
  73.  
  74.             catch (IOException error) {
  75.                 System.out.println ("Fehler!");
  76.             }
  77.         }
  78.         else{
  79.             create();
  80.         }
  81.     }
  82.  
  83.  
  84.     public void open()throws Exception{
  85.         fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("File", "*.txt"));
  86.         File openfile = fc.showOpenDialog(new Stage());
  87.  
  88.         if (openfile != null) {
  89.             try (ObjectInputStream outputStream = new ObjectInputStream(new FileInputStream(openfile))){
  90.                 int x = (int) outputStream.readObject();
  91.  
  92.                 for (int i=0;i<x;i ++){
  93.                     xadresse.getItems().add((Person)outputStream.readObject());
  94.                 }
  95.                 file = openfile;
  96.             }
  97.         }
  98.     }
  99.  
  100.     public void delete() {
  101.         int pos = xadresse.getSelectionModel().getSelectedIndex();
  102.         liste.remove(pos);
  103.     }
  104.  
  105.  
  106.     @FXML
  107.     public void edit() {
  108.         popup.initDialog(xfirstName.getText(), xlastName.getText(), xstreet.getText(), xnr.getText(), xzip.getText(), xcity.getText());
  109.         stage2.showAndWait();
  110.         Person person = popup.getPerson();
  111.         liste.set(xadresse.getSelectionModel().getSelectedIndex(), person);
  112.  
  113.     }
  114.  
  115.     public void create(){
  116.         fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Person","*.txt"));
  117.         file = fc.showSaveDialog(new Stage());
  118.  
  119.         if (file != null) {
  120.         }
  121.         saveAs();
  122.     }
  123.  
  124.     @FXML
  125.     public void createNew() {
  126.         popup.initDialog();
  127.         stage2.showAndWait();
  128.  
  129.  
  130.             liste.add(popup.getPerson());
  131.  
  132.     }
  133.  
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement