我在scrollPane中制作播放列表,但是我想在加载新播放列表时更改scrollPane中的内容。 setContent()只是添加内容。如何删除滚动窗格中的旧内容并插入我的新文本,即。有没有办法做“scrollpane.removeContent()”?
答案 0 :(得分:7)
Uluk是对的。
scrollPane.setContent(node)已经完全符合您的要求(替换现有内容),并且不需要removeContent方法,因为setContent(null)将删除滚动窗格中的任何内容。
以下是一个展示此内容的示例应用:
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class ScrollPaneReplace extends Application {
public static void main(String[] args) { launch(args); }
String[] imageLocs = {
"http://uhallnyu.files.wordpress.com/2011/11/green-apple.jpg",
"http://i.i.com.com/cnwk.1d/i/tim/2011/03/10/orange_iStock_000001331357X_540x405.jpg",
"http://smoothiejuicerecipes.com/pear.jpg"
};
ImageView[] images = new ImageView[imageLocs.length];
@Override public void init() {
for (int i = 0; i < imageLocs.length; i++) {
images[i] = new ImageView(new Image(imageLocs[i], 200, 0, true, true));
}
}
@Override public void start(Stage stage) {
stage.setTitle("Healthy Choices");
stage.getIcons().add(new Image("http://files.softicons.com/download/application-icons/pixelophilia-icons-by-omercetin/png/32/apple-green.png"));
final ScrollPane scroll = new ScrollPane();
scroll.setPrefSize(100, 100);
final IntegerProperty curImageIdx = new SimpleIntegerProperty(0);
scroll.setContent(images[curImageIdx.get()]);
Button next = new Button("Next Image");
next.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent t) {
curImageIdx.set((curImageIdx.get() + 1) % 3);
scroll.setContent(images[curImageIdx.get()]);
}
});
Button remove = new Button("Remove Image");
remove.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent t) {
scroll.setContent(null);
}
});
VBox controls = new VBox(10);
controls.getChildren().setAll(next, remove);
HBox layout = new HBox(10);
layout.getChildren().setAll(controls, scroll);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;");
HBox.setHgrow(scroll, Priority.ALWAYS);
stage.setScene(new Scene(layout));
stage.show();
scroll.setHvalue(0.25); scroll.setVvalue(0.25);
}
}
不同ScrollPane内容设置的示例图片: