2017-06-20 68 views
0

所以我基本上有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; 
} 

}

回答

1

使列一個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... 
} 
+0

謝謝,我改變了一切 - 像一個魅力。 :) –

+0

@EmilsP。道德:不要爲了演示而妥協數據表示。始終使演示適應數據。 –

+0

再次感謝,真實/好的建議! –