2016-05-12 129 views
1

我試圖用我的tableview這樣滑動efect: http://jsfiddle.net/43nGr/90/ 我真的不知道該怎麼做。在JavaFx中滑動TableView

這是我的簡單程序,它顯示一個按鈕和一個tableView。 My program

這是我的主類的代碼:

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application{ 

    Stage window; 
    TableView <Product> table; 
    Button showTableButton; 

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

@Override 
public void start(Stage primaryStage) throws Exception { 
    window = primaryStage; 
    window.setTitle("Liga Pilkarska"); 

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

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

    //Third Column 
    TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity"); 
    quantityColumn.setMinWidth(200); 
    quantityColumn.setMaxWidth(200); 
    quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); 

    //Creating a table and adding columns 
    table = new TableView(); 
    table.setItems(getProduct()); 
    table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); 

    table.setFixedCellSize(25); 

    //setting height of table  
    table.prefHeightProperty().bind(table.fixedCellSizeProperty().multiply(Bindings.size(table.getItems()).add(1.01))); 
    table.minHeightProperty().bind(table.prefHeightProperty()); 
    table.maxHeightProperty().bind(table.prefHeightProperty()); 

    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 
    showTableButton = new Button("Show Table"); 

    VBox vBox = new VBox(); 
    vBox.getChildren().addAll(showTableButton,table); 

    //Creating scene 
    Scene scene = new Scene(vBox, 800,600); 
    scene.getStylesheets().add("Viper.css"); 
    window.setScene(scene); 
    window.show(); 

} 

public ObservableList<Product> getProduct(){ 
    ObservableList<Product> products = FXCollections.observableArrayList(); 
    products.add(new Product("Laptop", 2300, 10)); 
    products.add(new Product("Latarka", 50, 19)); 
    products.add(new Product("Suszarka", 89.99, 22)); 
    products.add(new Product("Monitor", 100, 44)); 
    products.add(new Product("Kukurydza", 1.5, 1)); 

    return products; 
} 

} 

,這是我的「產品」類,它是在表的代碼。

public class Product { 
public String name; 
public double price; 
public int quantity; 


public Product(){ 
    this.name = ""; 
    this.price = 0; 
    this.quantity = 0; 
} 

public Product(String name, double price, int quantity){ 
    this.name = name; 
    this.price = price; 
    this.quantity = quantity; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public double getPrice() { 
    return price; 
} 

public void setPrice(double price) { 
    this.price = price; 
} 

public int getQuantity() { 
    return quantity; 
} 

public void setQuantity(int quantity) { 
    this.quantity = quantity; 
} 






} 

因此,如果我點擊或懸停在按鈕上,tableView應該開始做滑動efect。 如果有人知道如何做到這一點,那就太好了。

回答

0

您可以使用動畫TitledPane(容器)將動畫標誌設置爲TRUE,並在需要的事件觸發時調用展開。

或者更好的方法創建分離的線程,動畫首選/最小大小的TableView,取決於你的組件放置在什麼樣的容器。從動畫線程不要忘記設置JFXAT的大小和睡眠動畫線程。 爲了防止2個動畫同時運行,使用線程在執行前必須檢查的鎖/標誌。簡單的布爾值就足夠了。

1

我找到了解決方案,也許它有助於某人。

我們需要覆蓋Transition的interpolate方法。 所以我創建了新的類來擴展Transition。

import javafx.animation.Transition; 
import javafx.scene.control.TableView; 
import javafx.util.Duration; 

public class ResizeTheHeightOfTableView extends Transition{ 

public TableView<Product> table; 
public int maxSize; 

public ResizeTheHeightOfTableView(Duration duration, TableView<Product> table, int maxSize){ 
    setCycleDuration(duration); 
    this.maxSize = maxSize; 
    this.table = table; 
} 

@Override 
protected void interpolate(double fraction) { 
    table.setFixedCellSize(fraction * maxSize); 
} 

} 

,然後在主類只需添加兩行代碼:

ResizeTheHeightOfTableView ft = new ResizeTheHeightOfTableView(Duration.millis(500), table, 25); 
showTableButton.setOnAction(e -> ft.play()); 

不要忘記導入時間:

import javafx.util.Duration;