/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package graphical.core;
import java.lang.reflect.Field;
import java.util.Set;
import com.sun.javafx.scene.control.skin.ScrollPaneSkin;
import javafx.beans.Observable;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Skin;
import javafx.scene.layout.Background;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
/**
*
* @author XiaHui
*/
public class ListRootPanel extends VBox {
public VBox box = new VBox();
ScrollPane scrollPane = new ScrollPane();
public ListRootPanel() {
initComponent();
iniEvent();
}
private void initComponent() {
scrollPane.setContent(box);
scrollPane.setPannable(true);
scrollPane.setStyle("-fx-background-color:rgba(255,255,255,0)");
scrollPane.setBackground(Background.EMPTY);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.widthProperty().addListener((Observable observable) -> {
box.setPrefWidth(getWidth());
});
getChildren().add(scrollPane);
setUnitIncr();
}
private void iniEvent() {
setMouseTransparent(false);
setOnMouseDragged(v->{
v.consume();
});
box.setOnMouseDragged(v->{
v.consume();
});
scrollPane.setOnMouseDragged(v->{
v.consume();
});
}
public void addNode(Node node) {
if (!box.getChildren().contains(node)) {
box.getChildren().add(node);
}
}
public void addNode(int index, Node node) {
if (box.getChildren().contains(node)) {
box.getChildren().remove(node);
}
if(!box.getChildren().contains(node)){
box.getChildren().add(index,node);
}
}
public void removeNode(Node node) {
box.getChildren().remove(node);
}
public void removeNode(int index) {
box.getChildren().remove(index);
}
public void clearNode() {
box.getChildren().clear();
}
public int nodeSize() {
return box.getChildren().size();
}
private void setUnitIncr() {
try {
Skin<?> skin = scrollPane.getSkin();
if (skin instanceof ScrollPaneSkin) {
Field field = skin.getClass().getDeclaredField("vsb");
if (null != field) {
field.setAccessible(true);
ScrollBar scrollBar = (ScrollBar) field.get(skin);
field.setAccessible(false);
scrollBar.setUnitIncrement(20.0);
}
field = skin.getClass().getDeclaredField("hsb");
if (null != field) {
field.setAccessible(true);
ScrollBar scrollBar = (ScrollBar) field.get(skin);
field.setAccessible(false);
scrollBar.setUnitIncrement(20.0);
}
}
} catch (final Exception e) {
e.printStackTrace();
}
Set<Node> nodes = lookupAll(".scroll-bar");
if (null != nodes) {
for (Node node : nodes) {
if (node instanceof ScrollBar) {
ScrollBar sb = (ScrollBar) node;
sb.setUnitIncrement(20.0);
}
}
}
}
}
为什么滚动条不能滚动,反而随着VBox的高而变长?