2016-05-12 94 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;