JavaFx TableView - TableColumn字符串类型排序为双类型
问题描述:
所以我基本上有Tableview,有一些列。其中一列(价格)由实际上代表双打的字符串组成。唯一的原因是,因为我想要双打,例如,1将显示为1.00。但由于它,我不能使用默认排序(排序双打)。我可以以某种方式改变它,所以当我按下该按钮时,它会将它们排序为Double类型? enter image description hereJavaFx TableView - TableColumn字符串类型排序为双类型
这里的代码,如果需要的话:
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;
}
}
答
使列一个TableColumn<StoreProduct, Number>
,并使用电池工厂,以控制它的显示方式:
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()));
}
}
});
这与工作,例如
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...
}
谢谢,我改变了一切 - 像一个魅力。 :) –
@EmilsP。道德:不要为了演示而妥协数据表示。始终使演示适应数据。 –
再次感谢,真实/好的建议! –