onScroll侦听器在JavaFX 2中的TableView中不起作用 [英] onScroll listener does not working in TableView in JavaFX 2

查看:272
本文介绍了onScroll侦听器在JavaFX 2中的TableView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TableView组件的onScroll事件侦听器:

I'm trying to use the onScroll event listener of a TableView component:

FXML:

<TableView fx:id="table" onScroll="#doSomething" tableMenuButtonVisible="true" VBox.vgrow="ALWAYS">
    <columnResizePolicy>
        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
    </columnResizePolicy>
</TableView>

Java控制器:

@FXML
void doSomething(ActionEvent event)
{
    System.out.println("Object: " + event.getSource());
}

但它不起作用!我做错了什么?

But it does not working! What I'm doing wrong?

我需要捕获垂直滚动条来手动控制滚动位置并根据向下或向上滚动来获取相关数据。

I need to capture the vertical scroller to manually control the scroll position and fetch related data according to scroll down or up.

谢谢大家!

推荐答案

我认为可能发生的事情是TableView包含自身正在消耗滚动事件和在内部处理它,所以它永远不会到达你的应用程序处理程序。

I think what might be happening is that the TableView includes itself is consuming the scroll event and processing it internally, so it never gets to your application handler.

最初,我认为你可能想要使用 onScrollTo 而不是 onScroll ,但这似乎并没有真正解决这个问题。

Initially, I thought you might want to use onScrollTo rather than onScroll, but that doesn't seem to really address the issue.

我认为有效的解决方案是对滚动事件应用过滤器。

I think the solution which works is to apply a filter on the scroll event.

此外,您可以编写使用 scrollTo 调用手动控制滚动位置。

In addition, you can write code that makes use of with scrollTo calls to "manually control the scroll position".

以下是您可以尝试的一些示例代码(Java 8):

Here is some sample code you can try (Java 8):

TableScrollerApp.java

package finder;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TableScrollerApp extends Application {
    @Override public void start(final Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(
            getClass().getResource("tablescroller.fxml")
        );
        Parent parent = loader.load();

        stage.setScene(new Scene(new Group(parent)));
        stage.show();
    }

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

TableScrollerController.java

package finder;

import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.fxml.FXML;
import javafx.scene.control.ScrollToEvent;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.ScrollEvent;

import java.util.Arrays;
import java.util.stream.Collectors;

public class TableScrollerController {
    private static final String[] fruitNames = {
            "apples", "oranges", "pears", "peaches", 
            "guavas", "bananas", "jackfruit", "durians"
    };

    @FXML
    private TableView<Fruit> fruitsTable;

    @FXML
    private TableColumn<Fruit, String> fruitsColumn;

    @FXML
    protected void initialize() {
        fruitsColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        fruitsTable.addEventFilter(ScrollEvent.ANY, event ->
                System.out.println("Coded scroll filter: " + event)
        );

        fruitsTable.getItems().setAll(
                Arrays.stream(fruitNames)
                        .map(Fruit::new)
                        .collect(Collectors.toList())
        );

        fruitsTable.scrollTo(5);
    }

    @FXML
    protected void onScrollHandler(ScrollEvent scrollEvent) {
        System.out.println("FXML referenced scroll handler: " + scrollEvent);
    }

    @FXML
    protected void onScrollToHandler(ScrollToEvent<Integer> scrollToEvent) {
        System.out.println("FXML referenced onScroll handler: " + scrollToEvent);
    }

    public static class Fruit {
        private ReadOnlyStringWrapper name;

        public Fruit(String name) {
            this.name = new ReadOnlyStringWrapper(name);
        }

        public String getName() {
            return name.get();
        }

        public ReadOnlyStringProperty nameProperty() {
            return name;
        }
    }
}

tablescroller.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>

<TableView fx:id="fruitsTable" 
           maxHeight="-Infinity" maxWidth="-Infinity" 
           minHeight="-Infinity" minWidth="-Infinity" 
           onScroll="#onScrollHandler" 
           onScrollTo="#onScrollToHandler" 
           prefHeight="100.0" prefWidth="250.0" 
           xmlns="http://javafx.com/javafx/8" 
           xmlns:fx="http://javafx.com/fxml/1" 
           fx:controller="finder.TableScrollerController">
  <columns>
    <TableColumn fx:id="fruitsColumn" 
                 maxWidth="800.0" minWidth="200.0" prefWidth="-1.0" 
                 text="Fruits" />
  </columns>
</TableView>

这篇关于onScroll侦听器在JavaFX 2中的TableView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆