Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Leon Tsoy
- package sample;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.fxml.FXML;
- import javafx.scene.control.ListView;
- import javafx.scene.control.SelectionMode;
- import javafx.scene.control.TextField;
- import javafx.stage.FileChooser;
- import javafx.stage.Stage;
- import java.io.*;
- public class Anzeige {
- @FXML
- private ListView<Person> xadresse;
- @FXML
- private TextField xfirstName;
- @FXML
- private TextField xlastName;
- @FXML
- private TextField xnr;
- @FXML
- private TextField xstreet;
- @FXML
- private TextField xzip;
- @FXML
- private TextField xcity;
- private Stage stage2;
- private Popup popup;
- private File file;
- private FileChooser fc = new FileChooser();
- public void setStage2(Stage stage2) {
- this.stage2 = stage2;
- }
- public void setPopup(Popup pop) {
- this.popup = pop;
- }
- private ObservableList<Person> liste = FXCollections.observableArrayList();
- public void initialize() {
- xadresse.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
- xadresse.setItems(liste);
- xadresse.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> {
- xfirstName.setText(newValue.getFN());
- xlastName.setText(newValue.getLN());
- xnr.setText(newValue.getNr());
- xstreet.setText(newValue.getStreet());
- xzip.setText(newValue.getZip());
- xcity.setText(newValue.getCity());
- }));
- }
- public void saveAs() {
- if (file != null){
- try {
- ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false));
- outputStream.writeObject(xadresse.getItems().size());
- for (Person p: xadresse.getItems()){
- outputStream.writeObject(p);
- }
- outputStream.close();
- }
- catch (IOException error) {
- System.out.println ("Fehler!");
- }
- }
- else{
- create();
- }
- }
- public void open()throws Exception{
- fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("File", "*.txt"));
- File openfile = fc.showOpenDialog(new Stage());
- if (openfile != null) {
- try (ObjectInputStream outputStream = new ObjectInputStream(new FileInputStream(openfile))){
- int x = (int) outputStream.readObject();
- for (int i=0;i<x;i ++){
- xadresse.getItems().add((Person)outputStream.readObject());
- }
- file = openfile;
- }
- }
- }
- public void delete() {
- int pos = xadresse.getSelectionModel().getSelectedIndex();
- liste.remove(pos);
- }
- @FXML
- public void edit() {
- popup.initDialog(xfirstName.getText(), xlastName.getText(), xstreet.getText(), xnr.getText(), xzip.getText(), xcity.getText());
- stage2.showAndWait();
- Person person = popup.getPerson();
- liste.set(xadresse.getSelectionModel().getSelectedIndex(), person);
- }
- public void create(){
- fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Person","*.txt"));
- file = fc.showSaveDialog(new Stage());
- if (file != null) {
- }
- saveAs();
- }
- @FXML
- public void createNew() {
- popup.initDialog();
- stage2.showAndWait();
- liste.add(popup.getPerson());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement