2017-05-16 2 views
1

Immer noch lernen die Seile und ich habe eine Mischung aus Code-Schnipsel und ein bisschen meiner eigenen Arbeit, um dies zu machen, was ich stecken geblieben ist, wie man eine Datei, die ich habe, zu lesen gelagert.ArrayList in ObservableList lesen

In meiner Hauptklasse Ich habe ObservableList von denen haben mich bis A Arraylist zu konvertieren, nach dem ich Fileoutputstream und Object,

Ich möchte in der Lage sein zu „load“ die Daten zurück in über Fileinputstream und Object.

Ich verwendete This to help me save the data but can't work out how to read it back.

Oder wenn Sie an eine alternative Methode denken, um zu tun, was ich danach bin, würde schwellen.

Mein Haupt

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Menu; 
import javafx.scene.control.MenuBar; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.TextField; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class JavaFXGUI extends Application { 

Stage window; 

TableView<Product> table; 
TextField nameInput, priceInput, quantityInput; 

public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    window = primaryStage; 
    window.setTitle("thenewboston - JavaFX"); 

    //Name column 
    TableColumn<Product, String> nameColumn = new TableColumn<>("Name"); 
    nameColumn.setMinWidth(200); 
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); 

    //Price column 
    TableColumn<Product, Double> priceColumn = new TableColumn<>("Price"); 
    priceColumn.setMinWidth(100); 
    priceColumn.setCellValueFactory(new PropertyValueFactory<>("price")); 

    //Quantity column 
    TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity"); 
    quantityColumn.setMinWidth(100); 
    quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); 

    //Name input 
    nameInput = new TextField(); 
    nameInput.setPromptText("Name"); 
    nameInput.setMinWidth(100); 

    //Price input 
    priceInput = new TextField(); 
    priceInput.setPromptText("Price"); 

    //Quantity input 
    quantityInput = new TextField(); 
    quantityInput.setPromptText("Quantity"); 

    //Button 
    Button addButton = new Button("Add"); 
    addButton.setOnAction(e -> addButtonClicked()); 
    Button deleteButton = new Button("Delete"); 
    deleteButton.setOnAction(e -> deleteButtonClicked()); 

    HBox hBox = new HBox(); 
    hBox.setPadding(new Insets(10, 10, 10, 10)); 
    hBox.setSpacing(10); 
    hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton); 

    table = new TableView<>(); 
    table.setItems(getProduct()); 
    table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); 

    Scene scene = new Scene(new VBox(), 400, 350); 
    scene.setFill(Color.OLDLACE); 

    MenuBar menuBar = new MenuBar(); 

    // --- Menu File 
    Menu menuFile = new Menu("File"); 
    MenuItem save = new MenuItem("Save..."); 
    menuFile.getItems().add(save); 
    save.setOnAction(e -> { 
     write(getProduct()); 
     System.out.println("Saved File"); 
    }); 

    // --- Menu Edit 
    Menu menuEdit = new Menu("Edit"); 

    // --- Menu View 
    Menu menuView = new Menu("View"); 

    menuBar.getMenus().addAll(menuFile, menuEdit, menuView); 

    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, table, hBox); 

    window.setScene(scene); 
    window.show(); 
} 

//Add button clicked 
public void addButtonClicked() { 
    Product product = new Product(); 
    product.setName(nameInput.getText()); 
    product.setPrice(Double.parseDouble(priceInput.getText())); 
    product.setQuantity(Integer.parseInt(quantityInput.getText())); 
    table.getItems().add(product); 
    nameInput.clear(); 
    priceInput.clear(); 
    quantityInput.clear(); 
} 

//Delete button clicked 
public void deleteButtonClicked() { 
    ObservableList<Product> productSelected, allProducts; 
    allProducts = table.getItems(); 
    productSelected = table.getSelectionModel().getSelectedItems(); 

    productSelected.forEach(allProducts::remove); 
} 

//Get all of the products 
public ObservableList<Product> getProduct() { 
    ObservableList<Product> products = FXCollections.observableArrayList(); 
    products.add(new Product("Laptop", 859.00, 20)); 
    products.add(new Product("Bouncy Ball", 2.49, 198)); 
    products.add(new Product("Toilet", 99.00, 74)); 
    products.add(new Product("The Notebook DVD", 19.99, 12)); 
    products.add(new Product("Corn", 1.49, 856)); 
    return products; 
} 

public void write(ObservableList<Product> productObservalble) { 
    try { 
     // write object to file 
     FileOutputStream fos = new FileOutputStream("Objectsavefile.ser"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(new ArrayList<Product>(productObservalble)); 
     oos.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

} 

Products.java

import java.io.Serializable; 

public class Product implements Serializable { 

private String name; 
private double price; 
private int quantity; 

/** 
* Create Constructor with empty data 
*/ 
public Product() { 
    this.name = ""; 
    this.price = 0; 
    this.quantity = 0; 
} 

/** 
* 
* @param name - Set the name for the Product 
* @param price - Set the price of said Product 
* @param quantity - Set how many of said product 
*/ 
public Product(String name, double price, int quantity) { 
    this.name = name; 
    this.price = price; 
    this.quantity = quantity; 
} 

/** 
* Get the name of a Product 
* 
* @return 
*/ 
public String getName() { 
    return name; 
} 

/** 
* Set the name of a Product 
* 
* @param name 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* Get the price of a Product 
* 
* @return 
*/ 
public double getPrice() { 
    return price; 
} 

/** 
* Set the price of a Product 
* 
* @param price 
*/ 
public void setPrice(double price) { 
    this.price = price; 
} 

/** 
* Get the quantity of a Product 
* 
* @return 
*/ 
public int getQuantity() { 
    return quantity; 
} 

/** 
* Set the quantity of a Product 
* 
* @param quantity 
*/ 
public void setQuantity(int quantity) { 
    this.quantity = quantity; 
} 

@Override 
public String toString() { 
    return "Product [Name=" + name + ", Price=" + price + ", quantity=" 
      + quantity + "]"; 

} 

} 

Antwort

1

tun genau das Gegenteil:

public ObservableList<Product> read() throws IOException, ClassNotFoundException { 

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(...)); 
    List<Product> list = (List<Product>) ois.readObject(); 
    ois.close(); 
    return FXCollections.observableArrayList(list); 

} 
+0

so wie kann ich diese wiederum in einem Verfahren zum Lesen? – Tumnus

+0

Prost Ich werde versuchen, das funktioniert. – Tumnus

Verwandte Themen