Bad resizing borderpane that contains tableview and scrollbar
I have problems with my code, when I try to resize a borderpane the scrollbar at the bottom is doing bad scrolling, the tableview is wider, no fit to the contains. I tried the setManaged method but is not working.
I want that the tableview is fit to the contains not being wider than the contains, the scrollbar doesn't work properly in this case.
If anyone can help me, thanks in advance.
border pane example screen
this code is based on this question.
Option 1 Freeze Column
<?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;
}
javafx tableview scrollbar borderpane
add a comment |
I have problems with my code, when I try to resize a borderpane the scrollbar at the bottom is doing bad scrolling, the tableview is wider, no fit to the contains. I tried the setManaged method but is not working.
I want that the tableview is fit to the contains not being wider than the contains, the scrollbar doesn't work properly in this case.
If anyone can help me, thanks in advance.
border pane example screen
this code is based on this question.
Option 1 Freeze Column
<?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;
}
javafx tableview scrollbar borderpane
add a comment |
I have problems with my code, when I try to resize a borderpane the scrollbar at the bottom is doing bad scrolling, the tableview is wider, no fit to the contains. I tried the setManaged method but is not working.
I want that the tableview is fit to the contains not being wider than the contains, the scrollbar doesn't work properly in this case.
If anyone can help me, thanks in advance.
border pane example screen
this code is based on this question.
Option 1 Freeze Column
<?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;
}
javafx tableview scrollbar borderpane
I have problems with my code, when I try to resize a borderpane the scrollbar at the bottom is doing bad scrolling, the tableview is wider, no fit to the contains. I tried the setManaged method but is not working.
I want that the tableview is fit to the contains not being wider than the contains, the scrollbar doesn't work properly in this case.
If anyone can help me, thanks in advance.
border pane example screen
this code is based on this question.
Option 1 Freeze Column
<?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;
}
javafx tableview scrollbar borderpane
javafx tableview scrollbar borderpane
edited Jan 6 at 15:50
Fernando Guerrero Fuente
asked Jan 2 at 20:36
Fernando Guerrero FuenteFernando Guerrero Fuente
83
83
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012838%2fbad-resizing-borderpane-that-contains-tableview-and-scrollbar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012838%2fbad-resizing-borderpane-that-contains-tableview-and-scrollbar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown