ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaFX TableView测试

2022-05-04 16:01:26  阅读:239  来源: 互联网

标签:TableColumn return TableView JavaFX javafx 测试 new import public


数据模型Data2.java
package TableViewTest;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
 
public class Data2 {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleIntegerProperty age = new SimpleIntegerProperty();
    private SimpleDoubleProperty score = new SimpleDoubleProperty();
    private SimpleBooleanProperty is = new SimpleBooleanProperty(    );
 
    public Data2(String name, int age, double score, boolean is) {
        this.name.set(name);
        this.age.set(age);
        this.score.set(score);
        this.is.set(is);
    }
 
    public Data2() {
 
    }
 
    public String getName() {
        return name.get();
    }
 
    public SimpleStringProperty nameProperty() {
        return name;
    }
 
    public void setName(String name) {
        this.name.set(name);
    }
 
    public int getAge() {
        return age.get();
    }
 
    public SimpleIntegerProperty ageProperty() {
        return age;
    }
 
    public void setAge(int age) {
        this.age.set(age);
    }
 
    public double getScore() {
        return score.get();
    }
 
    public SimpleDoubleProperty scoreProperty() {
        return score;
    }
 
    public void setScore(double score) {
        this.score.set(score);
    }
 
    public boolean isIs() {
        return is.get();
    }
 
    public SimpleBooleanProperty isProperty() {
        return is;
    }
 
    public void setIs(boolean is) {
        this.is.set(is);
    }
    public SimpleStringProperty getNameProperty(){
        return  this.name;
    }
    public SimpleIntegerProperty getAgeProperty(){
        return  this.age;
    }
    public SimpleDoubleProperty getScoreProperty(){
        return  this.score;
    }
    public SimpleBooleanProperty getIsProperty(){
        return  this.is;
    }
}

TableView测试

package TableViewTest;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
 
public class TableTest extends Application {
    public static void main(String[] args) {
        Application.launch(TableTest.class,args);
    }
 
    @Override
    public void start(Stage primaryStage) throws Exception {
        
        TableView<Data2> tableView = new TableView<Data2>();
    
        TableColumn<Data2,String> tc_name = new TableColumn<>("姓名");
        TableColumn<Data2,Number> tc_age = new TableColumn<>("年龄");
        TableColumn<Data2,Number> tc_score = new TableColumn<>("分数");
        TableColumn<Data2,Boolean> tc_is = new TableColumn<>("is");
        tableView.getColumns().addAll(tc_name,tc_age,tc_score,tc_is);
        
 
        Button button = new Button("button");
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setTopAnchor(tableView,100.0);
        anchorPane.setLeftAnchor(tableView,100.0);
        anchorPane.getChildren().addAll(button,tableView);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.setTitle("网格布局");
        primaryStage.getIcons().add(new Image("http://www.haotuo.net.cn/Resources/cq/qunlogo.png"));
        primaryStage.show();
 
 
 
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                
               Data2 d1= new Data2("A",15,70,true);
               Data2 d2= new Data2("B",15,11,false);
               Data2 d3= new Data2("C",18,100,true);
               Data2 d4= new Data2("D",20,52,true);
               
        
               ObservableList<Data2> list = FXCollections.observableArrayList();
        
               list.addAll(d1,d2,d3,d4);
               tableView.setItems(list);
              
               
               tc_name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, String>, ObservableValue<String>>() {
                   @Override
                   public ObservableValue<String> call(TableColumn.CellDataFeatures<Data2, String> param) {
                       return param.getValue().getNameProperty();
                   }
               });
        
               tc_age.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Number>, ObservableValue<Number>>() {
                   @Override
                   public ObservableValue<Number> call(TableColumn.CellDataFeatures<Data2, Number> param) {
                       return param.getValue().getAgeProperty();
                   }
               });
        
               tc_score.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Number>, ObservableValue<Number>>() {
                   @Override
                   public ObservableValue<Number> call(TableColumn.CellDataFeatures<Data2, Number> param) {
                       return param.getValue().getScoreProperty();
                   }
               });
               tc_is.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Boolean>, ObservableValue<Boolean>>() {
                   @Override
                   public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Data2, Boolean> param) {
                       return param.getValue().getIsProperty();
                   }
               });
            }
        });
 
    }
}

 

标签:TableColumn,return,TableView,JavaFX,javafx,测试,new,import,public
来源: https://www.cnblogs.com/Lorrina/p/16221348.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有