看之前的教程,估计是版本太低的原因,在绑定自己定义的column和fxBean时,用的方法是:
firstName.setProperty("firstName");
但是在我的neatbeans就没有这样setProperty的方法,于是查看api,发现他的用法是:
firstColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestTableView, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<TestTableView, String> param) {
return param.getValue().getFirstProperty();
}
});
单元格工厂需要设置指定如何填充所有的cell在一个单一的TableColumn。提供了一个tablecolumn.celldatafeatures,需要返回一个observablevalue。返回的observablevalue实例将观察内部允许立即更新,并在屏幕上面刷新。
上面的firstColumn是自己定义的一个TableColumn
TestTableView是定义的一个java fx的bean(和普通的bean不一样,有property)
String是说明这个列中的内容是String类型的
return param.getValue().getFirstProperty();
这句是返回的一个在javafx bean定义的FirstProperty。
更简单的用法是:
firstColumn.setCellValueFactory(new PropertyValueFactory<TestTableView,String>("firstName"));
解释和上面的一样,这样就把这两者绑定在一起了,以后只要加入一个bean<TestTableView>的对象到TableView即可。
代码:
1.定义的javafx Bean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ControlTest;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
*
* @author Administrator
*/
public class TestTableView {
//这点要注意,在初始化的时候一定要写上new,否则会出错的,因为他不像int那样活着是String那样有默认的构造函数。
private StringProperty firstName=new SimpleStringProperty();
private StringProperty lastName=new SimpleStringProperty();
private StringProperty age=new SimpleStringProperty();
//下面就是中规中矩的写法,但是要注意和构造普通bean方法的区别
public TestTableView(String firstName,String lastName,String age){
this.setFirstName(firstName);
this.setLastName(lastName);
this.setAge(age);
}
public TestTableView(){
}
public String getFirstName(){
return this.firstName.get();
}
public void setFirstName(String firstName){
this.firstName.set(firstName);
}
public String getLastName(){
return this.lastName.get();
}
public void setLastName(String lastName){
this.lastName.set(lastName);
}
public String getAge(){
return this.age.get();
}
public void setAge(String age){
this.age.set(age);
}
public StringProperty getAgeProperty(){
return this.age;
}
public StringProperty getFirstProperty(){
return this.firstName;
}
public StringProperty getLastProperty(){
return this.lastName;
}
}
2.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ControlTest;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
/**
*
* @author Administrator
*/
public class TableViewTest extends Application {
@Override
public void start(Stage primaryStage) {
Label label=new Label("my table view");
//定义一个tableview
TableView<TestTableView> tableView=new TableView<>();
//定义不同多个tablecolumn
TableColumn<TestTableView,String> nameColumn=new TableColumn<TestTableView,String>("name");
TableColumn<TestTableView,String> firstColumn=new TableColumn<TestTableView,String>("firstName");
TableColumn<TestTableView,String> LastColumn=new TableColumn<TestTableView,String>("lastName");
TableColumn<TestTableView,String> ageColumn=new TableColumn<TestTableView,String>("age");
//把列和bean对应起来,这个很重要,两种写法,第一种
firstColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestTableView, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<TestTableView, String> param) {
return param.getValue().getFirstProperty();
}
});
//firstColumn.setCellValueFactory(new PropertyValueFactory<TestTableView,String>("firstName"));第二种用法
System.out.println("zhge s ces hide ");
LastColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestTableView, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<TestTableView, String> param) {
return param.getValue().getLastProperty();
}
});
ageColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestTableView, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<TestTableView, String> param) {
return param.getValue().getAgeProperty();
}
});
nameColumn.getColumns().addAll(firstColumn,LastColumn);
tableView.getColumns().addAll(nameColumn,ageColumn);
ObservableList<TestTableView> list=FXCollections.observableArrayList(
new TestTableView("zhang", "san", "45"),
new TestTableView("li", "si", "34"),
new TestTableView("wang", "wu", "78")
);
tableView.setItems(list);
HBox hBox=new HBox();
hBox.getChildren().add(tableView);
HBox.setHgrow(tableView, Priority.ALWAYS);
VBox vBox=new VBox();
vBox.getChildren().addAll(label,hBox);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}