【问题标题】:JavaFX ScrollListener ListViewJavaFX ScrollListener ListView
【发布时间】:2015-04-17 12:21:18
【问题描述】:

我创建了以下示例,其中包含带有注册滚动事件侦听器的ListView。不幸的是,监听器只会在通过鼠标滚轮滚动时触发,但在使用侧面的滚动条时不会触发。代码有什么问题?

Controller.java

package sample;

import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;

public class Controller implements Initializable {
    @FXML
    private ListView lstPreview;

    private ObservableList items = FXCollections.observableArrayList();

    public Controller() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        items.addAll(Arrays.asList("Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"));
        lstPreview.setItems(items);
        lstPreview.addEventFilter(javafx.scene.input.ScrollEvent.ANY, event -> { System.out.println("ScrollEvent!"); });
        lstPreview.addEventFilter(javafx.scene.control.ScrollToEvent.ANY, event -> { System.out.println("ScrollToEvent!"); });

    }
}

Main.java

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

sample.fxml

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

<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import java.net.URL?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <stylesheets>
      <URL value="@style.css" />
   </stylesheets>
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints hgrow="ALWAYS" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
      <RowConstraints vgrow="ALWAYS" />
   </rowConstraints>
   <children>
      <ListView fx:id="lstPreview" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS" />
   </children>
</GridPane>

解决方案

我最终使用了@Uluk Biy 解决方案,也许有一天 Java 8 JDK 会获得这样的功能:

/**
 * Looks up the first found scrollbar of a given control matching the desired orientation. If none found, null is returned
 *
 * @param control
 * @param orientation
 * @return
 */
public static javafx.scene.control.ScrollBar getScrollbarComponent(javafx.scene.control.Control control, javafx.geometry.Orientation orientation) {
    javafx.scene.Node n = control.lookup(".scroll-bar");
    if (n instanceof javafx.scene.control.ScrollBar) {
        final javafx.scene.control.ScrollBar bar = (javafx.scene.control.ScrollBar) n;
        if (bar.getOrientation().equals(orientation)) {
            return bar;
        }
    }
    return null;
}

像这样使用它:

javafx.scene.control.ScrollBar timelineBar = de.elara.asgard.util.FxUtils.getScrollbarComponent(listTimeline, javafx.geometry.Orientation.HORIZONTAL);
        if (timelineBar != null) {
            timelineBar.addEventFilter(ScrollToEvent.ANY, event -> { doScrollingTimeline(); });
        }

【问题讨论】:

  • 您可以访问 ScrollBar 并向其添加事件处理程序。见this link
  • 谢谢,我用你的建议解决了!

标签: listview scroll javafx fxml


【解决方案1】:

根据Documentation

滚动事件表示用户通过鼠标滚轮进行了滚动, 触控板、触摸屏或其他类似设备。

所以它与ListViewScrollBars 无关。由于它们是实现细节,除了做一些反射技巧之外,访问它们并不容易。

【讨论】:

  • 虽然可以使用反射,但它不是唯一的方法。我在下面提供了另一个简单的答案。
【解决方案2】:

这是一种无需任何反射技巧的简单方法:

首先,您应该实现自己的 ListViewSkin 扩展,例如 CustomListViewSkin。稍后您将使用 .setSkin 方法在列表视图上设置皮肤。 (您可以扩展 ListView 并覆盖 createDefaultSkin 方法以使您的自定义皮肤也起作用)。 现在,您应该扩展 VirtualFlow(比如CustomVirtualFlow)并让 CustomListViewSkin 在运行时通过覆盖 CustomVirtualFlow 类的 createVirtualFlow 创建一个 CustomVirtualFlow 实例。 然后,您应该将此方法添加到 CustomVirtualFlow 以完成工作。

import com.sun.javafx.scene.control.skin.VirtualFlow;
import com.sun.javafx.scene.control.skin.VirtualScrollBar;
import javafx.scene.control.ListCell;

public class CustomVirtualFlow<T> extends VirtualFlow<ListCell<T>>
{

   public VirtualScrollBar getVerticalBar()
   {
       return getVbar();
   }

   public VirtualScrollBar getHorizontalBar()
   {
       return getHbar();
   }

}

最后,将此方法添加到您的 CustomListViewSkin 实现中:

public CustomVirtualFlow<ListCell<T>> getFlow()
{
    return (CustomVirtualFlow<ListCell<T>>) flow;
}

现在:

ListView lv = new ListView();
CustomListViewSkin skin = new CustomListViewSkin(lv);
lv.setSkin(skin);

//add the list view to some scene

CustomVirtualFlow flow = skin.getFlow();
ScrollBar vb = flow.getVerticalBar();
//do whatever you like with the vb instance now

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 2017-05-28
  • 1970-01-01
  • 2017-04-19
  • 2012-06-18
  • 2015-02-27
  • 2015-01-22
相关资源
最近更新 更多