2017-07-06 1 views
0

私はスクロールバーを接続したいので、同時にスクロールすることができる2つのテーブルがあります。私の知る限り、TableViews独自のスクロールバーのバインドは不可能です(私が間違っていれば私を修正してください)。TableViewScrollPaneにラップすることにしました。JavaFX TableView:テーブルからscollbarsを削除/無効にします(代わりにScrollPaneを使用)

ただし、テーブルのスクロールはまだTableView独自のスクロールバーに設定されています。

下は基本的なコード例です。ご覧のように、ScrollPaneスクロールバーの双方向バインディングが機能します。しかし、上記の問題(主なコントロールはTableViews独自のスクロールバーです)のため、テーブルのデータはこれの影響を受けません。

誰かが私にこれを助けることができますか? TableViewsスクロールバーを使用する代わりにスクロールバーを使用してTableViewに移動したいと思います(バインドできないため)。

public class Main extends Application { 
    private TableView<Person> table = new TableView<>(); 
    private TableView<Person> table2 = new TableView<>(); 
    private final ObservableList<Person> data = 
      FXCollections.observableArrayList(
        new Person("Jacob", "Smith"), 
        new Person("Isabella", "Johnson"), 
        new Person("Ethan", "Williams"), 
        new Person("Emma", "Jones"), 
        new Person("Michael", "Brown") 
      ); 

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

    @Override 
    public void start(Stage stage) { 
     Scene scene = new Scene(new Group()); 
     stage.setTitle("Table View Sample"); 
     stage.setWidth(300); 
     stage.setHeight(1000); 

     TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); 
     firstNameCol.setPrefWidth(100); 
     firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); 

     TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name"); 
     lastNameCol.setPrefWidth(100); 
     lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName")); 

     table.setItems(data); 
     table.getColumns().addAll(firstNameCol, lastNameCol); 

     table2.setItems(data); 
     table2.getColumns().addAll(firstNameCol, lastNameCol); 

     ScrollPane sp = new ScrollPane(table); 
     sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); 
     sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); 

     ScrollPane sp2 = new ScrollPane(table2); 
     sp2.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); 
     sp2.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); 

     sp.setHmax(3); 
     sp2.setHmax(3); 
     sp.hvalueProperty().bindBidirectional(sp2.hvalueProperty()); 

     final VBox vbox = new VBox(); 
     vbox.setSpacing(5); 
     vbox.setPadding(new Insets(10, 0, 0, 10)); 
     vbox.getChildren().addAll(sp, sp2); 

     ((Group) scene.getRoot()).getChildren().addAll(vbox); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    public static class Person { 

     private final SimpleStringProperty firstName; 
     private final SimpleStringProperty lastName; 

     private Person(String fName, String lName) { 
      this.firstName = new SimpleStringProperty(fName); 
      this.lastName = new SimpleStringProperty(lName); 
     } 

     public String getFirstName() { 
      return firstName.get(); 
     } 

     public void setFirstName(String fName) { 
      firstName.set(fName); 
     } 

     public String getLastName() { 
      return lastName.get(); 
     } 

     public void setLastName(String fName) { 
      lastName.set(fName); 
     } 
    } 
} 

答えて

1

スクロールバーをこのようにバインドすることが可能である:

void bindScrollBars(TableView<?> firstTable, TableView<?> secondTable, Orienatation orieantation){ 
    ScrollBar firstScrollBar = null; 
    ScrollBar secondScrollBar = null; 

    for (Node node : firstTable.lookupAll(".scroll-bar")) { 
      if (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(orientation)) { 
       firstScrollBar= (ScrollBar) node; 
      } 
    } 

    for (Node node : secondTable.lookupAll(".scroll-bar")) { 
      if (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(orientation)) { 
       secondScrollBar = (ScrollBar) node; 
      } 
    } 

    if(firstScrollBar != null && secondScrollBar != null) { firstScrollBar.valueProperty().bindBidirectional(secondScrollBar.valueProperty()); } 

私にとっては、それが正常に動作し、あなたが水平方向と垂直方向の両方にバインドすることができます。

1

最初の問題は、VBoxがシーンで正しくスケーリングされていないことです。つまり、ScrollPaneがコンテンツより小さくなることはありません。これに

((Group) scene.getRoot()).getChildren().addAll(vbox); 

:あなたは、これを変更することでこの問題を解決することができ

Scene scene = new Scene(vbox); 

ます。またstartの初めに、あなたの前の宣言を削除する必要があります:

Scene scene = new Scene(new Group()); //Remove this line 

今すぐあなたのScrollPaneあなたのシーンに合わせて適切にスケールします。しかし、あなたはまだ彼らが垂直に一致するように彼らのvvalueをバインドする必要があります。今すぐあなたのスクロールが正常に動作

sp.hvalueProperty().bindBidirectional(sp2.hvalueProperty()); 
sp.vvalueProperty().bindBidirectional(sp2.vvalueProperty()); //Add this line 

:あなたは自分のhvalue Sをバインドする場所の下の行を追加します。

working scrollbars

関連する問題