Skip to content

Instantly share code, notes, and snippets.

@aoetk
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoetk/b771a82d7069cba547b7 to your computer and use it in GitHub Desktop.
Save aoetk/b771a82d7069cba547b7 to your computer and use it in GitHub Desktop.
JavaFXで2つのListViewのスクロールを同期させるサンプル
package sample;
import java.util.Set;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar;
public class Controller {
@FXML
ListView<String> leftListView;
@FXML
ListView<String> rightListView;
@FXML
void addData(ActionEvent event) {
ObservableList<String> items = FXCollections.observableArrayList();
for (int i = 0; i < 50; i++) {
items.add("リストに格納するアイテム " + i + "番目");
}
leftListView.getItems().addAll(items);
rightListView.getItems().addAll(items);
}
public void requestBind() {
Platform.runLater(this::bindScrollBar);
}
private void bindScrollBar() {
ScrollBar leftVerticalScrollBar = getScrollBar(leftListView, true);
ScrollBar rightVerticalScrollBar = getScrollBar(rightListView, true);
leftVerticalScrollBar.valueProperty().bindBidirectional(rightVerticalScrollBar.valueProperty());
ScrollBar leftHorizontalScrollBar = getScrollBar(leftListView, false);
ScrollBar rightHorizontalScrollBar = getScrollBar(rightListView, false);
leftHorizontalScrollBar.valueProperty().bindBidirectional(rightHorizontalScrollBar.valueProperty());
}
private ScrollBar getScrollBar(ListView<String> listView, boolean vertical) {
Set<Node> nodes = listView.lookupAll(".scroll-bar");
for (Node node : nodes) {
if (node instanceof ScrollBar) {
ScrollBar scrollBar = (ScrollBar) node;
if (vertical && scrollBar.getOrientation() == Orientation.VERTICAL) {
return scrollBar;
} else if (!vertical && scrollBar.getOrientation() == Orientation.HORIZONTAL) {
return scrollBar;
}
}
}
throw new IllegalStateException("Not found!");
}
}
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{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Scroll Test");
primaryStage.setScene(new Scene(root));
primaryStage.show();
Controller controller = loader.getController();
controller.requestBind();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="273.0" prefWidth="348.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<HBox BorderPane.alignment="CENTER">
<children>
<ListView fx:id="leftListView" prefHeight="200.0" prefWidth="200.0" />
<ListView fx:id="rightListView" prefHeight="200.0" prefWidth="200.0" />
</children>
</HBox>
</center>
<bottom>
<Button mnemonicParsing="false" onAction="#addData" text="Add Data" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
</Button>
</bottom>
</BorderPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment