Плохое изменение размеров границы, которая содержит таблицу и полосу прокрутки - PullRequest
0 голосов
/ 02 января 2019

У меня проблемы с моим кодом, когда я пытаюсь изменить размер границы, полоса прокрутки внизу делает плохую прокрутку, просмотр таблицы шире, не подходит к содержимому. Я попробовал метод setManaged, но он не работает.

Я хочу, чтобы табличное представление соответствовало содержимому, которое не шире содержимого, в этом случае полоса прокрутки не работает должным образом.

Если кто-нибудь может мне помочь, заранее спасибо.

Пример экрана с рамкой границы

этот код основан на этом вопросе. Вариант 1 Стоп-кадр

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

    <AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SyncrTwoTablesController">
       <children>
          <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="100.0" minWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <children>
                <BorderPane prefHeight="200.0" prefWidth="200.0">
                   <center>
            <TableView fx:id="tableNoScroll" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <columns>
                <TableColumn fx:id="tcName" prefWidth="75.0" text="Name" />
              </columns>
            </TableView>
                   </center>
                   <bottom>
                      <Region maxHeight="14.0" minHeight="14.0" prefHeight="14.0" BorderPane.alignment="CENTER" />
                   </bottom>
                </BorderPane>
                <BorderPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
            <bottom>
              <ScrollBar fx:id="hScroll">
                         <BorderPane.margin>
                            <Insets right="15.0" />
                         </BorderPane.margin>
                      </ScrollBar>
            </bottom>
            <right>
              <ScrollBar fx:id="vScroll" orientation="VERTICAL">
                         <BorderPane.margin>
                            <Insets top="25.0" />
                         </BorderPane.margin></ScrollBar>
            </right>
                   <center>
            <TableView fx:id="tableScroll" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <columns>
                <TableColumn prefWidth="75.0" text="Column X" />
                <TableColumn prefWidth="75.0" text="Column X" />
              </columns>
            </TableView>
                   </center>
          </BorderPane>
             </children>
          </HBox>
       </children>
    </AnchorPane>

package application;

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) {
        try {
           Parent root = FXMLLoader.load(getClass().getResource("syncrtwotables.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

package application;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.util.Callback;

public class SyncrTwoTablesController implements Initializable {

   @FXML
   private ScrollBar           hScroll;

   @FXML
   private HBox                hBox;

   @FXML
   private TableView<RowBean> tableNoScroll;

   @FXML
   private TableView<RowBean> tableScroll;

   @FXML
   private ScrollBar           vScroll;

   private TestBean            testBean;
   @FXML
   private TableColumn<RowBean, String> tcName;

   @Override
   public void initialize(URL location, ResourceBundle resources) {
      // TODO Auto-generated method stub
      System.out.println("Controller");

      initializeBean();
      fillTables();
      //hScroll.setMin(tableScroll.getWidth());
      Platform.runLater(() -> addSynchronizedVerticalScrolls());
      Platform.runLater(() -> addSynchronizedHorizontalScrolls());
      //addSynchronizedVerticalScrolls();
      //addSynchronizedHorizontalScrolls();
   }

   private void fillTables() {
      tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
      tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
      tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));

      List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
      TableColumn<RowBean, String> col = null;
      tableScroll.getColumns().clear();
      if (testBean.getLstRow().size() > 0) {
         for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
            col = new TableColumn<RowBean, String>("col");
            int id = i;
            col.setCellValueFactory(
                  new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
                     @Override
                     public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
                        return p.getValue().getLstColBean().get(id) != null
                              ? p.getValue().getLstColBean().get(id).getColValue()
                              : new SimpleStringProperty("");
                     }
                  });
            lstColums.add(col);
         }
         tableScroll.getColumns().addAll(lstColums);
      }
   }

   private void initializeBean() {
      ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
      ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
      ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
      ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
      ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
      ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
      List<ColBean> lstColBean = new ArrayList<ColBean>();
      lstColBean.add(colBean);
      lstColBean.add(colBean2);
      lstColBean.add(colBean3);
      lstColBean.add(colBean4);
      lstColBean.add(colBean5);
      lstColBean.add(colBean6);
      RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean2 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean3 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean4 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean5 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean6 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean7 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean15 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean8 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean9 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean10 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean11 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean12 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean13 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean14 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean16 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean17 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean18 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean19 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean20 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean21 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean22 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean23 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean24 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean25 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean26 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean27 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean28 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean29 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean30 = new RowBean(new SimpleStringProperty("hola"), lstColBean);
      RowBean rowBean31 = new RowBean(new SimpleStringProperty("hola"), lstColBean);

      List<RowBean> lstRow = new ArrayList<RowBean>();
      lstRow.add(rowBean);
      lstRow.add(rowBean2);
      lstRow.add(rowBean3);
      lstRow.add(rowBean4);
      lstRow.add(rowBean5);
      lstRow.add(rowBean6);
      lstRow.add(rowBean7);
      lstRow.add(rowBean8);
      lstRow.add(rowBean9);
      lstRow.add(rowBean10);
      lstRow.add(rowBean11);
      lstRow.add(rowBean12);
      lstRow.add(rowBean13);
      lstRow.add(rowBean14);
      lstRow.add(rowBean15);
      lstRow.add(rowBean16);
      lstRow.add(rowBean17);
      lstRow.add(rowBean18);
      lstRow.add(rowBean19);
      lstRow.add(rowBean20);
      lstRow.add(rowBean21);
      lstRow.add(rowBean22);
      lstRow.add(rowBean23);
      lstRow.add(rowBean24);
      lstRow.add(rowBean25);
      lstRow.add(rowBean26);
      lstRow.add(rowBean27);
      lstRow.add(rowBean28);
      lstRow.add(rowBean29);
      lstRow.add(rowBean30);
      lstRow.add(rowBean31);

      testBean = new TestBean(new SimpleStringProperty("test"), lstRow);

   }

   /**
    * Method that adds synchronization to the horizontal scrollbars.
    */
   private void addSynchronizedHorizontalScrolls() {
      ScrollBar scrollTable = null;
      for (Node node : tableScroll.lookupAll(".scroll-bar")) {
         if (node instanceof ScrollBar) {
            scrollTable = (ScrollBar) node;
            if (scrollTable.getOrientation() == Orientation.HORIZONTAL) {
               break;
            }
         }
      }
      if (scrollTable == null) {
         hScroll.setVisible(false);
      }
      if (scrollTable.isVisible() && vScroll != null) {
         hScroll.setVisible(true);
         scrollTable.setPrefHeight(0);
         scrollTable.setMaxHeight(0);
         scrollTable.setVisible(false);
         scrollTable.setOpacity(1);
         hScroll.setMax(scrollTable.getMax());
         hScroll.setMin(scrollTable.getMin());
         hScroll.setVisibleAmount(scrollTable.getVisibleAmount());
         hScroll.managedProperty().bind(hScroll.visibleProperty());
         bindScrollBarValues(scrollTable, hScroll);
      }
   }

   /**
    * Method that adds synchronization to the horizontal scrollbars.
    */
   private void addSynchronizedVerticalScrolls() {
      ScrollBar scrollVertical = null;
      for (Node node : tableScroll.lookupAll(".scroll-bar:vertical")) {
         if (node instanceof ScrollBar) {
            scrollVertical = (ScrollBar) node;
            if (scrollVertical.getOrientation() == Orientation.VERTICAL) {
               break;
            }
         }
      }
      ScrollBar noHScrollTable = null;
      for (Node node : tableNoScroll.lookupAll(".scroll-bar")) {
         if (node instanceof ScrollBar) {
            noHScrollTable = (ScrollBar) node;
            if (noHScrollTable.getOrientation() == Orientation.VERTICAL) {
               break;
            }
         }
      }
      if (scrollVertical == null || !scrollVertical.isVisible()) {
         vScroll.setVisible(false);
      }
      if (scrollVertical.isVisible() && vScroll != null) {
         vScroll.setVisible(true);
         scrollVertical.setVisible(false);
         noHScrollTable.setVisible(false);
         vScroll.setMax(scrollVertical.getMax());
         vScroll.setMin(scrollVertical.getMin());
         vScroll.setVisibleAmount(scrollVertical.getVisibleAmount());
         bindScrollBarValues(scrollVertical, noHScrollTable, vScroll);

      }
   }

   protected void bindScrollBarValues(ScrollBar scrollTv, ScrollBar scrollOut) {
      scrollTv.valueProperty().addListener((src, ov, nv) -> {
         scrollOut.setValue(nv.doubleValue());
         scrollOut.setVisibleAmount(scrollTv.getVisibleAmount());
      });
      scrollOut.valueProperty().addListener((src, ov, nv) -> {
         scrollTv.setValue(nv.doubleValue());
         scrollOut.setVisibleAmount(scrollTv.getVisibleAmount());
      });

   }

   protected void bindScrollBarValues(ScrollBar scrollTv, ScrollBar noScrollTv, ScrollBar scrollOut) {
      scrollTv.valueProperty().addListener((src, ov, nv) -> {
         scrollOut.setValue(nv.doubleValue());
         noScrollTv.setValue(nv.doubleValue());
         scrollOut.setVisibleAmount(scrollTv.getVisibleAmount());
      });
      scrollOut.valueProperty().addListener((src, ov, nv) -> {
         scrollTv.setValue(nv.doubleValue());
         noScrollTv.setValue(nv.doubleValue());
         scrollOut.setVisibleAmount(scrollTv.getVisibleAmount());
      });
      noScrollTv.valueProperty().addListener((src, ov, nv) -> {
         scrollTv.setValue(nv.doubleValue());
         scrollOut.setValue(nv.doubleValue());
         scrollOut.setVisibleAmount(scrollTv.getVisibleAmount());
      });


   }


}

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */

/* The main scrollbar **track** CSS class  */

#tableScroll, #tableNoScroll .scroll-bar:vertical .track{
        -fx-padding:0px;
    -fx-background-color:transparent;
    -fx-border-color:transparent;
    -fx-background-radius: 0em;
    -fx-border-radius:2em;

}

/* The increment and decrement button CSS class of scrollbar */

#tableScroll .scroll-bar:vertical .increment-button ,
#tableScroll .scroll-bar:vertical .decrement-button {

    -fx-background-color:transparent;
    -fx-background-radius: 0em;
    -fx-padding:0 0 0 0;
}

#tableNoScroll .scroll-bar:vertical .increment-button ,
#tableNoScroll .scroll-bar:vertical .decrement-button {

    -fx-background-color:transparent;
    -fx-background-radius: 0em;
    -fx-padding:0 0 0 0;
}

#tableScroll  .scroll-bar:vertical .increment-arrow,
#tableScroll  .scroll-bar:vertical  .decrement-arrow
{
    -fx-shape: " "; 
    -fx-padding:0;        
}

#tableNoScroll  .scroll-bar:vertical .increment-arrow,
#tableNoScroll  .scroll-bar:vertical  .decrement-arrow
{
    -fx-shape: " "; 
    -fx-padding:0;        
}

/* The main scrollbar **thumb** CSS class which we drag every time (movable) */
#tableScroll .scroll-bar:vertical .thumb {
    -fx-background-color:transparent;
    -fx-background-insets: 0, 0, 0;
    -fx-background-radius: 2em;
    -fx-padding:0px;

}

/* The main scrollbar **thumb** CSS class which we drag every time (movable) */
#tableNoScroll .scroll-bar:vertical .thumb {
    -fx-background-color:transparent;
    -fx-background-insets: 0, 0, 0;
    -fx-background-radius: 2em;
    -fx-padding:0px;

}

#tableScroll .scroll-bar:horizontal .track{
        -fx-padding:0px;
    -fx-background-color:transparent;
    -fx-border-color:transparent;
    -fx-background-radius: 0em;
    -fx-border-radius:2em;

}

#tableNoScroll .scroll-bar:horizontal .track{
        -fx-padding:0px;
    -fx-background-color:transparent;
    -fx-border-color:transparent;
    -fx-background-radius: 0em;
    -fx-border-radius:2em;

}

/* The increment and decrement button CSS class of scrollbar */

#tableScroll .scroll-bar:horizontal .increment-button ,
#tableScroll .scroll-bar:horizontal .decrement-button {

    -fx-background-color:transparent;
    -fx-background-radius: 0em;
    -fx-padding:0 0 0 0;
}

#tableScroll  .scroll-bar:horizontal .increment-arrow,
#tableScroll  .scroll-bar:horizontal  .decrement-arrow
{
    -fx-shape: " "; 
    -fx-padding:0;        
}

/* The main scrollbar **thumb** CSS class which we drag every time (movable) */
#tableScroll .scroll-bar:horizontal .thumb {
    -fx-background-color:transparent;
    -fx-background-insets: 0, 0, 0;
    -fx-background-radius: 2em;
    -fx-padding:0px;

}
...