So habe ich grundsätzlich Tableview, mit einigen Spalten dort. Eine der Spalten (Price) besteht aus Strings, die eigentlich Doubles darstellen. Nur der Grund, warum ist das, weil ich wollte, dass zum Beispiel verdoppelt, wie 1 als 1,00 angezeigt wird. Aber aus diesem Grund kann ich die Standardsortierung nicht verwenden (sort double). Kann ich es irgendwie ändern, wenn ich also diesen Knopf drücke, wird es als Doppeltyp sortiert? enter image description hereJavaFx TableView - TableColumn Zeichenfolientyp sortiert als Doppeltyp
hier Code, falls erforderlich:
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;
}
}
Danke, ich habe alles verändert - funktioniert wie ein Charme. :) –
@EmilsP. Moral: Kompromisse bei der Datendarstellung wegen der Präsentation. Passen Sie die Präsentation immer an die Daten an. –
Danke nochmal, echt/guter Rat! –