JavaFX 表格操作 监听选择的 行列坐标 监听 选择的行 表格可编辑

  • Post author:
  • Post category:java

package fx.com.监听;

import fx.com.Data2;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
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.ChangeListener;
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.*;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(Main.class,args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        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<Data2> tableView = new TableView<>(list);


        TableColumn<Data2,String> tc_name = new TableColumn<>("姓名");
        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();
            }
        });

        TableColumn<Data2,Number> tc_age = new TableColumn<>("年龄");
        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();
            }
        });

        TableColumn<Data2,Number> tc_score = new TableColumn<>("分数");
        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();
            }
        });
        TableColumn<Data2,Boolean> tc_is = new TableColumn<>("嗯");
        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();
            }
        });




        tableView.getColumns().addAll(tc_name,tc_age,tc_score,tc_is);

        tableView.setPrefWidth(600);
        tableView.setPrefHeight(400);

        double v = tableView.getPrefWidth() / tableView.getColumns().size()+20;
        tc_age.setPrefWidth(v);
        tc_is.setPrefWidth(v);
        tc_name.setPrefWidth(v);
        tc_score.setPrefWidth(v);

        tc_name.setVisible(true);//隐藏
        tableView.setTableMenuButtonVisible(true);//右上角菜单
        tableView.scrollTo(2);//滚动到 第2列
        tableView.scrollToColumnIndex(2);//滚动到 第2行
        tableView.setFixedCellSize(60);//一列的高度
        tableView.setStyle("-fx-font-size: 20");//字体大小

        //设置表格 年龄列可以编辑
        tableView.setEditable(true);
        tc_name.setCellFactory(TextFieldTableCell.forTableColumn());
        tc_age.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<Number>() {
            @Override
            public String toString(Number object) {
                return String.valueOf(object);
            }

            @Override
            public Number fromString(String string) {
                return Integer.valueOf(string);
            }
        }));
        tc_is.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<Boolean>() {
            @Override
            public String toString(Boolean object) {
                return String.valueOf(object);
            }

            @Override
            public Boolean fromString(String string) {
                return Boolean.valueOf(string);
            }
        }));

        //表格可以多选
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        //监听选中的
        tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Data2>() {
            @Override
            public void changed(ObservableValue<? extends Data2> observable, Data2 oldValue, Data2 newValue) {
                ObservableList<Integer> selectedIndices = tableView.getSelectionModel().getSelectedIndices();
                for (int i = 0; i < selectedIndices.size(); i++) {
//                    System.out.println(selectedIndices.get(i));
                }
            }
        });
        //单独选择
        tableView.getSelectionModel().setCellSelectionEnabled(true);
        //单独选择的监听
        tableView.getSelectionModel().getSelectedCells().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                ObservableList<TablePosition> observableList = (ObservableList<TablePosition>) observable;
                for(int i=0;i<observableList.size();i++){
                    TablePosition tablePosition = observableList.get(i);
                    Object cellData = tablePosition.getTableColumn().getCellData(tablePosition.getRow());

                    System.out.println("您选择的坐标为:("+tablePosition.getColumn()+","+tablePosition.getRow()+")_内容为:"+cellData.toString());
                }
            }
        });


        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) {
                //选择指定 一个
                tableView.getSelectionModel().select(2,tc_age);
                tableView.requestFocus();
            }
        });

    }
}

 


版权声明:本文为jialan75原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。