栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检测双击TableView JavaFX的行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检测双击TableView JavaFX的行

TableView<MyType> table = new TableView<>();//...table.setRowFactory( tv -> {    TableRow<MyType> row = new TableRow<>();    row.setonMouseClicked(event -> {        if (event.getClickCount() == 2 && (! row.isEmpty()) ) { MyType rowData = row.getItem(); System.out.println(rowData);        }    });    return row ;});

这是一个完整的工作示例:

import java.util.Random;import java.util.function.Function;import javafx.application.Application;import javafx.beans.property.IntegerProperty;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.beans.value.Observablevalue;import javafx.scene.Scene;import javafx.scene.control.TableColumn;import javafx.scene.control.TableRow;import javafx.scene.control.TableView;import javafx.stage.Stage;public class TableViewDoubleClickonRow extends Application {    @Override    public void start(Stage primaryStage) {        TableView<Item> table = new TableView<>();        table.setRowFactory(tv -> { TableRow<Item> row = new TableRow<>(); row.setonMouseClicked(event -> {     if (event.getClickCount() == 2 && (! row.isEmpty()) ) {         Item rowData = row.getItem();         System.out.println("Double click on: "+rowData.getName());     } }); return row ;        });        table.getColumns().add(column("Item", Item::nameProperty));        table.getColumns().add(column("Value", Item::valueProperty));        Random rng = new Random();        for (int i = 1 ; i <= 50 ; i++) { table.getItems().add(new Item("Item "+i, rng.nextInt(1000)));        }        Scene scene = new Scene(table);        primaryStage.setScene(scene);        primaryStage.show();    }    private static <S,T> TableColumn<S,T> column(String title, Function<S, Observablevalue<T>> property) {        TableColumn<S,T> col = new TableColumn<>(title);        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));        return col ;    }    public static class Item {        private final StringProperty name = new SimpleStringProperty();        private final IntegerProperty value = new SimpleIntegerProperty();        public Item(String name, int value) { setName(name); setValue(value);        }        public StringProperty nameProperty() { return name ;        }        public final String getName() { return nameProperty().get();        }        public final void setName(String name) { nameProperty().set(name);        }        public IntegerProperty valueProperty() { return value ;        }        public final int getValue() { return valueProperty().get();        }        public final void setValue(int value) { valueProperty().set(value);        }    }    public static void main(String[] args) {        launch(args);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号