JavaFX TableView-Type de chaîne TableColumn trié en Double Type


J'ai donc essentiellement Tableview, avec quelques colonnes là-bas. L'une des colonnes (Prix) est constituée de chaînes qui représentent en fait des doubles. La seule raison est que, parce que je voulais que double, par exemple, que 1 sera affiché comme 1.00. Mais à cause de cela, je ne peux pas utiliser le tri par défaut propely (tri double). Puis-je le changer en quelque sorte, donc quand j'appuie sur ce bouton, il les triera en double type? entrez la description de l'image ici

Coder ici, si nécessaire:

import java.text.NumberFormat;
import java.text.ParseException;
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.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.stage.Stage;

@SuppressWarnings("unused") 
public class Store_DateBase extends Application {

Stage window;
Scene scene;
Button addButton, deleteButton;
TableView<Store_Products> 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("Store sortiment!");

    // name column
    TableColumn<Store_Products, String> nameColumn = new TableColumn<>("Name");
    nameColumn.setMinWidth(200);
    nameColumn.setCellValueFactory(new PropertyValueFactory<Store_Products, String>("name"));
    // price column
    TableColumn<Store_Products, String> priceColumn = new TableColumn<>("Price");
    priceColumn.setMinWidth(200);
    priceColumn.setStyle("-fx-alignment: CENTER;");
    priceColumn.setCellValueFactory(new PropertyValueFactory<Store_Products, String>("price"));
    // Quantity column\
    TableColumn<Store_Products, Integer> intColumn = new TableColumn<>("Quantity");
    intColumn.setMinWidth(200);
    intColumn.setStyle("-fx-alignment: CENTER;");
    intColumn.setCellValueFactory(new PropertyValueFactory<Store_Products, Integer>("quantity"));

    // name inputs
    nameInput = new TextField();
    nameInput.setPromptText("Write a name");
    nameInput.setMinWidth(150);
    // price inputs
    priceInput = new TextField();
    priceInput.setPromptText("Write a price");
    priceInput.setMinWidth(100);
    //quantityInputs
    quantityInput = new TextField();
    quantityInput.setPromptText("Write a quantity");
    quantityInput.setMinWidth(100);

    // buttons
    addButton = new Button("Add");
    addButton.setOnAction(e -> addMeth());

    deleteButton = new Button("Delete");
    deleteButton.setOnAction(e -> deleteMeth());


    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, intColumn);

    VBox layout = new VBox(10);
    layout.setPadding(new Insets(20, 20, 20, 20));
    layout.getChildren().addAll(table, hBox);

    scene = new Scene(layout);
    window.setScene(scene);
    window.show();
}

// delete button clicked
private void deleteMeth() {
    ObservableList<Store_Products> productSelected, allProducts;
    allProducts = table.getItems();
    productSelected = table.getSelectionModel().getSelectedItems();
    // for all products what are selected remove them from table.
    productSelected.forEach(allProducts::remove);
}

// add button clicked
private void addMeth() {
    Store_Products produkts = new Store_Products();
    // get name
    produkts.setName(nameInput.getText());
    // get price
    double price = PriceValue(produkts);
    if (price != 0)
        produkts.setPrice(price);
    else
        return;
    // get quantity
    int quantity = QuantityValue(produkts);
    if (quantity != 0)
        produkts.setQuantity(quantity);
    else
        return;
    // add to the table
    table.getItems().add(produkts);
    // clear what is written after push "addButton"
    nameInput.clear();
    priceInput.clear();
    quantityInput.clear();
}

private double PriceValue(Store_Products price) {
    try {
        String value = priceInput.getText().replaceAll(",", ".");
        double doubleValue = Double.parseDouble(value);
        return doubleValue;
    } catch (Exception e) {
        return 0;
    }

}

private int QuantityValue(Store_Products quantity) {
    try {
        int value = Integer.parseInt(quantityInput.getText());
        return value;
    } catch (Exception e) {
        return 0;
    }

}

// get default products
public ObservableList<Store_Products> getProduct() {
    ObservableList<Store_Products> products = FXCollections.observableArrayList();
    products.add(new Store_Products("Laptop", 850.00, 27));
    products.add(new Store_Products("Desktop computer", 499.99, 66));
    products.add(new Store_Products("Proccesor", 50.00, 55));
    products.add(new Store_Products("Screen", 119.99, 22));
    products.add(new Store_Products("keyboard", 20.00, 270));
    products.add(new Store_Products("mouse", 16.99, 60));
    products.add(new Store_Products("mic", 5.90, 101));
    return products;
}

}

Author: Emils P., 2017-06-20

1 answers

Faire la colonne a TableColumn<StoreProduct, Number>, et utiliser une usine de cellules pour contrôler la façon dont il est affiché:

TableColumn<StoreProduct, Number> priceColumn = new TableColumn<>("Price");
// may need to update the properties in StoreProduct to have the correct type
// (but likely will be more convenient anyway)
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
priceColumn.setCellFactory(tc -> new TableCell<StoreProduct, Number>() {
    @Override
    protected void updateItem(Number price, boolean empty) {
        if (empty) {
            setText("");
        } else {
            setText(String.format("%.2f", price.doubleValue()));
        }
    }
});

Cela fonctionnerait avec, par exemple

public class StoreProduct {

    private final DoubleProperty price = new SimpleDoubleProperty() ;

    public DoubleProperty priceProperty() {
        return price ;
    }

    public final double getPrice() {
        return priceProperty().get();
    }

    public final void setPrice(double price) {
        priceProperty().set(price);
    }

    // other properties...
}
 1
Author: James_D, 2017-06-20 20:07:05