下面列出了javafx.scene.control.Skin#javafx.scene.control.ScrollBar 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void fixHorizontalScrollbar()
{
for(Node n: lookupAll(".scroll-bar"))
{
if(n instanceof ScrollBar)
{
ScrollBar b = (ScrollBar)n;
if(b.getOrientation() == Orientation.HORIZONTAL)
{
if(isAutoResizeMode())
{
b.setManaged(false);
b.setPrefHeight(0);
b.setPrefWidth(0);
}
else
{
b.setManaged(true);
b.setPrefHeight(USE_COMPUTED_SIZE);
b.setPrefWidth(USE_COMPUTED_SIZE);
}
}
}
}
}
private void initializeScrollListener() {
threadService.runActionLater(() -> {
this.treeView = controller.getFileSystemView();
Set<Node> nodes = this.treeView.lookupAll(".scroll-bar");
for (Node node : nodes) {
ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
verticalScrollState.updateState(scrollBar);
scrollBar.valueProperty().addListener((observable, oldValue, newValue) -> {
verticalScrollState.updateState(scrollBar, newValue);
});
} else if (scrollBar.getOrientation() == Orientation.HORIZONTAL) {
horizontalScrollState.updateState(scrollBar);
scrollBar.valueProperty().addListener((observable, oldValue, newValue) -> {
horizontalScrollState.updateState(scrollBar, newValue);
});
}
}
});
}
private void restoreTreeScrollState() {
threadService.schedule(() -> { // run after some ms
threadService.runActionLater(() -> { // run in ui thread
Set<Node> nodes = this.treeView.lookupAll(".scroll-bar");
for (Node node : nodes) {
ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
verticalScrollState.restoreState(scrollBar);
} else if (scrollBar.getOrientation() == Orientation.HORIZONTAL) {
horizontalScrollState.restoreState(scrollBar);
}
}
});
}, 50, TimeUnit.MILLISECONDS);
}
/**
* Computes the size that should be reserved for horizontal scrollbar in
* size hints (min/pref height)
*/
private double computeHsbSizeHint(ScrollPane sp) {
return ((sp.getHbarPolicy() == ScrollBarPolicy.ALWAYS) ||
(sp.getHbarPolicy() == ScrollBarPolicy.AS_NEEDED && (sp.getPrefViewportHeight() > 0 || sp.getMinViewportHeight() > 0)))
? hsb.prefHeight(ScrollBar.USE_COMPUTED_SIZE)
: 0;
}
/**
* Computes the size that should be reserved for vertical scrollbar in size
* hints (min/pref width)
*/
private double computeVsbSizeHint(ScrollPane sp) {
return ((sp.getVbarPolicy() == ScrollBarPolicy.ALWAYS) ||
(sp.getVbarPolicy() == ScrollBarPolicy.AS_NEEDED && (sp.getPrefViewportWidth() > 0
|| sp.getMinViewportWidth() > 0)))
? vsb.prefWidth(ScrollBar.USE_COMPUTED_SIZE)
: 0;
}
private void bindScrollBars() {
final Set<Node> nodes = lookupAll("ScrollBar");
for (Node node : nodes) {
if (node instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) node;
if (bar.getOrientation().equals(Orientation.VERTICAL)) {
bindScrollBars(verticalScrollBar, bar);
} else if (bar.getOrientation().equals(Orientation.HORIZONTAL)) {
bindScrollBars(horizontalScrollBar, bar);
}
}
}
}
private void bindScrollBars(ScrollBar scrollBarA, ScrollBar scrollBarB) {
scrollBarA.valueProperty().bindBidirectional(scrollBarB.valueProperty());
scrollBarA.minProperty().bindBidirectional(scrollBarB.minProperty());
scrollBarA.maxProperty().bindBidirectional(scrollBarB.maxProperty());
scrollBarA.visibleAmountProperty().bindBidirectional(scrollBarB.visibleAmountProperty());
scrollBarA.unitIncrementProperty().bindBidirectional(scrollBarB.unitIncrementProperty());
scrollBarA.blockIncrementProperty().bindBidirectional(scrollBarB.blockIncrementProperty());
}
public static ScrollBar getVScrollBar(WebView webView) {
try {
Set<Node> scrolls = webView.lookupAll(".scroll-bar");
for (Node scrollNode : scrolls) {
if (ScrollBar.class.isInstance(scrollNode)) {
ScrollBar scroll = (ScrollBar) scrollNode;
if (scroll.getOrientation() == Orientation.VERTICAL) {
return scroll;
}
}
}
} catch (Exception e) {
}
return null;
}
private ScrollBar horizontalScrollBar(double minw, double minh, double prefw, double prefh, double maxw, double maxh) {
final ScrollBar scrollBar = new ScrollBar();
scrollBar.setMinSize(minw, minh);
scrollBar.setPrefSize(prefw, prefh);
scrollBar.setMaxSize(maxw, maxh);
scrollBar.setVisibleAmount(50);
scrollBar.setMax(xBarWidth-(2*circleRadius));
return scrollBar;
}
private ScrollBar verticalScrollBar(double minw, double minh, double prefw, double prefh, double maxw, double maxh) {
final ScrollBar scrollBar = new ScrollBar();
scrollBar.setMinSize(minw, minh);
scrollBar.setPrefSize(prefw, prefh);
scrollBar.setMaxSize(maxw, maxh);
scrollBar.setVisibleAmount(50);
scrollBar.setMax(yBarHeight-(2*circleRadius));
return scrollBar;
}
private ScrollBar horizontalScrollBar(double minw, double minh, double prefw, double prefh, double maxw, double maxh) {
final ScrollBar scrollBar = new ScrollBar();
scrollBar.setMinSize(minw, minh);
scrollBar.setPrefSize(prefw, prefh);
scrollBar.setMaxSize(maxw, maxh);
scrollBar.setVisibleAmount(50);
scrollBar.setMax(xBarWidth-(2*circleRadius));
return scrollBar;
}
private ScrollBar verticalScrollBar(double minw, double minh, double prefw, double prefh, double maxw, double maxh) {
final ScrollBar scrollBar = new ScrollBar();
scrollBar.setMinSize(minw, minh);
scrollBar.setPrefSize(prefw, prefh);
scrollBar.setMaxSize(maxw, maxh);
scrollBar.setVisibleAmount(50);
scrollBar.setMax(yBarHeight-(2*circleRadius));
return scrollBar;
}
private void bindScrollBars() {
ScrollBar plainTextScrollBar = plainTextList.lookupAll(".scroll-bar").stream().map(e -> (ScrollBar) e).filter(e -> e.getOrientation().equals(Orientation.HORIZONTAL)).findFirst().orElse(null);
ScrollBar decryptedTextScrollBar = decryptedTextList.lookupAll(".scroll-bar").stream().map(e -> (ScrollBar) e).filter(e -> e.getOrientation().equals(Orientation.HORIZONTAL)).findFirst().orElse(null);
if (plainTextScrollBar != null && decryptedTextScrollBar != null) {
plainTextScrollBar.valueProperty().bindBidirectional(decryptedTextScrollBar.valueProperty());
} else {
LOGGER.error("couldn't find scrollbars");
}
}
/**
* bidirectionally bind scroll bars of two nodes
*
* @param node1
* @param node2
* @param orientation
*/
public static void bidirectionallyBindScrollBars(final Node node1, final Node node2, Orientation orientation) {
final ScrollBar scrollBar1 = findScrollBar(node1, orientation);
final ScrollBar scrollBar2 = findScrollBar(node2, orientation);
if (scrollBar1 != null && scrollBar2 != null) {
final Single<Boolean> inChange = new Single<>(false);
scrollBar1.valueProperty().addListener(observable -> {
if (!inChange.get()) {
try {
inChange.set(true);
scrollBar2.setValue(scrollBar1.getValue() * (scrollBar2.getMax() - scrollBar2.getMin()) / (scrollBar1.getMax() - scrollBar1.getMin()));
} finally {
inChange.set(false);
}
}
});
scrollBar2.valueProperty().addListener(observable -> {
if (!inChange.get()) {
try {
inChange.set(true);
scrollBar1.setValue(scrollBar2.getValue() * (scrollBar1.getMax() - scrollBar1.getMin()) / (scrollBar2.getMax() - scrollBar2.getMin()));
} finally {
inChange.set(false);
}
}
});
}
}
/**
* Find the scrollbar of the given table.
*
* @param node
* @return
*/
public static ScrollBar findScrollBar(Node node, Orientation orientation) {
Set<Node> below = node.lookupAll(".scroll-bar");
for (final Node nodeBelow : below) {
if (nodeBelow instanceof ScrollBar) {
ScrollBar sb = (ScrollBar) nodeBelow;
if (sb.getOrientation() == orientation) {
return sb;
}
}
}
return null;
}
protected void fixHorizontalScrollbar()
{
Skin skin = tree.getSkin();
if(skin == null)
{
return;
}
for(Node n: skin.getNode().lookupAll(".scroll-bar"))
{
if(n instanceof ScrollBar)
{
ScrollBar b = (ScrollBar)n;
if(b.getOrientation() == Orientation.HORIZONTAL)
{
if(isAutoResizeMode())
{
b.setManaged(false);
b.setPrefHeight(0);
b.setPrefWidth(0);
}
else
{
b.setManaged(true);
b.setPrefHeight(USE_COMPUTED_SIZE);
b.setPrefWidth(USE_COMPUTED_SIZE);
}
}
}
}
}
private ScrollBar getVerticalScrollbar(TableView<?> table) {
ScrollBar result = null;
for (Node n : table.lookupAll(".scroll-bar")) {
if (n instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) n;
if (bar.getOrientation().equals(Orientation.VERTICAL)) {
result = bar;
}
}
}
return result;
}
void scrolled(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double value = newValue.doubleValue();
// System.out.println("Scrolled to " + value);
ScrollBar bar = getVerticalScrollbar(tableView);
if (value == bar.getMax()) {
// System.out.println("Adding new persons.");
// double targetValue = value * items.size();
// addPersons();
// bar.setValue(targetValue / items.size());
}
}
public Float getScrollWidth() {
if( this.scrollWidth == null ) {
ScrollBar scrollBar = new ScrollBar();
scrollBar.setManaged(false);
scrollBar.applyCss();
this.scrollWidth = Double.valueOf(scrollBar.getWidth()).floatValue();
}
return this.scrollWidth;
}
private static ScrollBar findScrollBar(Node node, Orientation orientation) {
Set<Node> scrollBars = node.lookupAll(".scroll-bar");
for (Node scrollBar : scrollBars) {
if (scrollBar instanceof ScrollBar &&
((ScrollBar)scrollBar).getOrientation() == orientation)
return (ScrollBar) scrollBar;
}
return null;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
MultipleBallPane ballPane = new MultipleBallPane();
ballPane.setStyle("-fx-border-color: yellow");
Button btAdd = new Button("+");
Button btSubtract = new Button("-");
HBox hBox = new HBox(10);
hBox.getChildren().addAll(btAdd, btSubtract);
hBox.setAlignment(Pos.CENTER);
// Add or remove a ball
btAdd.setOnAction(e -> ballPane.add());
btSubtract.setOnAction(e -> ballPane.subtract());
// Pause and resume animation
ballPane.setOnMousePressed(e -> ballPane.pause());
ballPane.setOnMouseReleased(e -> ballPane.play());
// Use a scroll bar to control animation speed
ScrollBar sbSpeed = new ScrollBar();
sbSpeed.setMax(20);
sbSpeed.setValue(10);
ballPane.rateProperty().bind(sbSpeed.valueProperty());
BorderPane pane = new BorderPane();
pane.setCenter(ballPane);
pane.setTop(sbSpeed);
pane.setBottom(hBox);
// Create a scene and place the in the stage
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle("Exercise_20_09"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static double getScrollbarWidth(Node scrollablePane) {
Node node = scrollablePane.lookup(".scroll-bar");
if (node instanceof ScrollBar) {
final ScrollBar bar = (ScrollBar) node;
if (bar.getOrientation().equals(Orientation.VERTICAL))
return bar.getWidth();
}
return 0;
}
public MenuItem createRightPanelMenuItem() {
MenuItem createRight = new MenuItem("Create");
createRight.setOnAction(e -> {
logger.info("Menu: Panels > Create");
panelControl.createNewPanelAtEnd();
// listener is used as panelsScroll's Hmax property doesn't update
// synchronously
ChangeListener<Number> listener = new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
for (Node child : panelsScrollPane.getChildrenUnmodifiable()) {
if (child instanceof ScrollBar) {
ScrollBar scrollBar = (ScrollBar) child;
if (scrollBar.getOrientation() == Orientation.HORIZONTAL
&& scrollBar.visibleProperty().get()) {
panelControl.scrollToCurrentlySelectedPanel();
break;
}
}
}
panelControl.widthProperty().removeListener(this);
}
};
panelControl.widthProperty().addListener(listener);
});
createRight.setAccelerator(CREATE_RIGHT_PANEL);
return createRight;
}
private static void setupUnitIncrement(ScrollBar bar) {
bar.unitIncrementProperty().bind(new DoubleBinding() {
{ bind(bar.maxProperty(), bar.visibleAmountProperty()); }
@Override
protected double computeValue() {
double max = bar.getMax();
double visible = bar.getVisibleAmount();
return max > visible
? 16 / (max - visible) * max
: 0;
}
});
}
public void updateState(ScrollBar scrollBar, Number newValue) {
if (Objects.nonNull(newValue)) {
double value = newValue.doubleValue();
if (value > 0) {
updateState(scrollBar);
this.setValue(value);
}
}
}
public void restoreState(ScrollBar scrollBar) {
if (value > 0) {
updateState(scrollBar);
scrollBar.setMin(getMin());
scrollBar.setMax(getMax());
scrollBar.setUnitIncrement(getUnitIncrement());
scrollBar.setBlockIncrement(getBlockIncrement());
scrollBar.setValue(value);
}
}
public void handleAttributeMoved(double sceneX, double sceneY) {
if (draggingAttributeNode != null) {
final Point2D location = sceneToLocal(sceneX, sceneY);
double x = location.getX() - draggingOffset.getX();
if (x < 0) {
x = 0;
}
if (x > RunPane.this.getWidth() - draggingAttributeNode.getWidth()) {
x = RunPane.this.getWidth() - draggingAttributeNode.getWidth();
}
double y = location.getY() - draggingOffset.getY();
if (y < 0) {
y = 0;
}
if (y > RunPane.this.getHeight() - draggingAttributeNode.getHeight()) {
y = RunPane.this.getHeight() - draggingAttributeNode.getHeight();
}
draggingAttributeNode.setLayoutX(x);
draggingAttributeNode.setLayoutY(y);
final Point2D tableLocation = sampleDataView.sceneToLocal(sceneX, sceneY);
double offset = 0;
Set<Node> nodes = sampleDataView.lookupAll(".scroll-bar");
for (final Node node : nodes) {
if (node instanceof ScrollBar) {
final ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.HORIZONTAL) {
offset = scrollBar.getValue();
break;
}
}
}
double totalWidth = 0;
mouseOverColumn = null;
final double cellPadding = 0.5; // ?
if (tableLocation.getX() >= 0 && tableLocation.getX() <= sampleDataView.getWidth() && tableLocation.getY() >= 0 && tableLocation.getY() <= sampleDataView.getHeight()) {
double columnLocation = tableLocation.getX() + offset;
for (TableColumn<TableRow, ?> column : sampleDataView.getColumns()) {
totalWidth += column.getWidth() + cellPadding;
if (columnLocation < totalWidth) {
mouseOverColumn = (ImportTableColumn) column;
break;
}
}
}
if (mouseOverColumn != null) {
// Allow for the SplitPane left side inset+padding (1+1 hard-coded).
final double edge = 2;
columnRectangle.setLayoutX(edge + sampleDataView.getLayoutX() + totalWidth - mouseOverColumn.getWidth() - offset);
columnRectangle.setLayoutY(sampleDataView.getLayoutY());
columnRectangle.setWidth(mouseOverColumn.getWidth());
columnRectangle.setHeight(sampleDataView.getHeight());
columnRectangle.setVisible(true);
} else {
columnRectangle.setVisible(false);
}
}
}
@Override
protected ScrollBar createJFXNode() throws Exception
{
final ScrollBar scrollbar = new ScrollBar();
scrollbar.setOrientation(model_widget.propHorizontal().getValue() ? Orientation.VERTICAL : Orientation.HORIZONTAL);
scrollbar.setFocusTraversable(true);
scrollbar.setOnKeyPressed((final KeyEvent event) ->
{
switch (event.getCode())
{
case DOWN: jfx_node.decrement();
break;
case UP: jfx_node.increment();
break;
case PAGE_UP:
//In theory, this may be unsafe; i.e. if max/min are changed
//after node creation.
jfx_node.adjustValue(max);
break;
case PAGE_DOWN:
jfx_node.adjustValue(min);
break;
default: break;
}
});
if (! toolkit.isEditMode())
{
scrollbar.addEventFilter(MouseEvent.ANY, e ->
{
if (e.getButton() == MouseButton.SECONDARY)
{
// Disable the contemporary triggering of a value change and of the
// opening of contextual menu when right-clicking on the scrollbar's
// buttons.
e.consume();
}
else if (MouseEvent.MOUSE_PRESSED.equals(e.getEventType()))
{
// Prevent UI value update while actively changing
isValueChanging = true;
}
else if (MouseEvent.MOUSE_RELEASED.equals(e.getEventType()))
{
// Prevent UI value update while actively changing
isValueChanging = false;
}
});
}
enablementChanged(null, null, null);
limitsChanged(null, null, null);
// This code manages layout,
// because otherwise for example border changes would trigger
// expensive Node.notifyParentOfBoundsChange() recursing up the scene graph
scrollbar.setManaged(false);
return scrollbar;
}
@Override protected void resize() {
super.resize();
width = tile.getWidth() - tile.getInsets().getLeft() - tile.getInsets().getRight();
height = tile.getHeight() - tile.getInsets().getTop() - tile.getInsets().getBottom();
size = width < height ? width : height;
double containerWidth = contentBounds.getWidth();
double containerHeight = contentBounds.getHeight();
if (null == verticalScrollBar) {
for (Node n : graphicContainer.lookupAll(".scroll-bar")) {
if (n instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) n;
if (bar.getOrientation().equals(Orientation.VERTICAL)) {
verticalScrollBar = bar;
verticalScrollBar.visibleProperty().addListener((o, ov, nv) -> {
if (nv) {
scrollBarWidth = verticalScrollBar.getLayoutBounds().getWidth();
}
});
break;
}
}
}
} else {
if (verticalScrollBar.isVisible()) {
contentBounds.setWidth(contentBounds.getWidth() - scrollBarWidth);
} else {
contentBounds.setWidth(contentBounds.getWidth() + scrollBarWidth);
}
}
if (tile.isShowing() && width > 0 && height > 0) {
pane.setMaxSize(width, height);
pane.setPrefSize(width, height);
if (containerWidth > 0 && containerHeight > 0) {
graphicContainer.setMinSize(containerWidth, containerHeight);
graphicContainer.setMaxSize(containerWidth, containerHeight);
graphicContainer.setPrefSize(containerWidth, containerHeight);
graphicContainer.relocate(contentBounds.getX(), contentBounds.getY());
if (null != tile) {
Node graphic = tile.getGraphic();
if (tile.getGraphic() instanceof Shape) {
double graphicWidth = graphic.getBoundsInLocal().getWidth();
double graphicHeight = graphic.getBoundsInLocal().getHeight();
if (graphicWidth > containerWidth || graphicHeight > containerHeight) {
double scale = 1;
if (graphicWidth - containerWidth > graphicHeight - containerHeight) {
scale = containerWidth / graphicWidth;
} else {
scale = containerHeight / graphicHeight;
}
graphic.setScaleX(scale);
graphic.setScaleY(scale);
}
} else if (tile.getGraphic() instanceof ImageView) {
ImageView imgView = (ImageView) graphic;
imgView.setPreserveRatio(true);
imgView.setFitWidth(containerWidth);
imgView.setFitHeight(containerHeight);
//((ImageView) graphic).setFitWidth(containerWidth);
//((ImageView) graphic).setFitHeight(containerHeight);
}
}
}
resizeStaticText();
}
}
private ListChangeListener<IMatchBlock> createChangeListener(final TableItem tableItem, final LongProperty previousSelectionTime) {
final ReadLayoutPane pane = tableItem.getPane();
return c -> {
if (c.next()) {
if (!pane.getMatchSelection().isEmpty())
tableView.getSelectionModel().select(tableItem);
}
if (System.currentTimeMillis() - 200 > previousSelectionTime.get()) { // only if sufficient time has passed since last scroll...
try {
final double focusCoordinate;
int focusIndex = pane.getMatchSelection().getFocusIndex();
if (focusIndex >= 0 && pane.getMatchSelection().getItems()[focusIndex] != null) {
final IMatchBlock focusMatch = pane.getMatchSelection().getItems()[focusIndex];
focusCoordinate = 0.5 * (focusMatch.getAlignedQueryStart() + focusMatch.getAlignedQueryEnd());
double leadingWidth = 0;
double lastWidth = 0;
double totalWidth = 0;
{
int numberOfColumns = tableView.getColumns().size();
int columns = 0;
for (TableColumn col : tableView.getColumns()) {
if (col.isVisible()) {
if (columns < numberOfColumns - 1)
leadingWidth += col.getWidth();
else
lastWidth = col.getWidth();
totalWidth += col.getWidth();
}
columns++;
}
}
final double coordinateToShow = leadingWidth + lastWidth * (focusCoordinate / maxReadLength.get());
final ScrollBar hScrollBar = FXUtilities.findScrollBar(tableView, Orientation.HORIZONTAL);
if (hScrollBar != null) { // should never be null, but best to check...
final double newPos = (hScrollBar.getMax() - hScrollBar.getMin()) * ((coordinateToShow) / totalWidth);
Platform.runLater(() -> {
tableView.scrollTo(tableItem);
hScrollBar.setValue(newPos);
});
}
}
} catch (Exception ignored) {
}
}
previousSelectionTime.set(System.currentTimeMillis());
};
}