Replace JavaFX2.0如何动态更改滚动窗格中的内容?

Replace JavaFX2.0如何动态更改滚动窗格中的内容?,replace,javafx-2,scrollpane,Replace,Javafx 2,Scrollpane,我正在滚动窗格中制作播放列表,但我想在加载新播放列表时更改滚动窗格中的内容。setContent()只是添加内容。如何删除scrollpane中的旧内容并插入新文本,即是否有方法执行“scrollpane.removeContent()”?Uluk是正确的 已完全执行您想要的操作(替换现有内容),并且不需要removeContent方法,因为setContent(null)将删除滚动窗格中的任何内容 下面是一个示例应用程序来演示这一点: import javafx.application.Ap

我正在滚动窗格中制作播放列表,但我想在加载新播放列表时更改滚动窗格中的内容。setContent()只是添加内容。如何删除scrollpane中的旧内容并插入新文本,即是否有方法执行“scrollpane.removeContent()”?

Uluk是正确的

已完全执行您想要的操作(替换现有内容),并且不需要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);
  }
}
导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.event.*;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.image.*;
导入javafx.scene.layout.*;
导入javafx.stage.stage;
公共类ScrollPaneReplace扩展了应用程序{
公共静态void main(字符串[]args){launch(args);}
字符串[]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[]图像=新的ImageView[imageLocs.length];
@重写公共void init(){
对于(int i=0;i
不同滚动窗格内容设置的示例图像:


顾名思义,setContent()设置内容而不是添加到其中。再次检查您的代码,您会以某种方式重新设置旧内容。要删除内容,只需调用setContent(null)或setContent(new Pane())v.s.。非常感谢!我确信是setcontent()弄乱了代码,所以我忘了重置我的旧内容变量。