Hbox中的ImageView不会显示在GridPane中

时间:2016-09-14 14:20:29

标签: java javafx imageview gridpane

我正在创建一个显示有关汽车信息的应用程序,稍后我将添加CRUD功能,但是现在我想要在gridpane上显示一个图像。

我有一个只包含start方法的小型Main-Class,然后我有另一个包含所有GUI组件的类。这是GUI:

GUI

我想让图片显示在屏幕的右上角区域。

我还有一个汽车类,我从中创建了汽车对象并放入了一个数组。该数组包含有关图像文件的品牌,型号,reg.nr和URL的信息。我还在carclass中创建了get()方法。

所以我成功地将所有其他组件添加到我的GUIComponents类的网格中,最后我尝试添加它,只是为了尝试显示第一个carobjects图像:

HBox hbCarImg = new HBox();
ImageView imageView = new ImageView(new Image(carArray[0].getImageURL()));
hbCarImg.getChildren().add(imageView);
add(hbCarImg, 2, 1, 2, 3);

我希望hbox出现在第2列第1行,并且跨越2列和3行。

当我这样做时没有任何事情发生。花了很多时间搜索这个,代码看起来很好,我不知道什么是行不通的。

感谢任何提示或帮助!

1 个答案:

答案 0 :(得分:0)

虽然有点长但是应该这样做:

  //start with scrollpane

  ScrollPane sp = new ScrollPane();  

  //create a gridpane
  GridPane gb = new GridPane();
  gb.setHgap(5);
  gb.setVgap(5);


  //for debugging purposes, make sure you remove it
  gb.setGridLinesVisible(true);

  gb.setPadding(new Insets(10,10,10,10));
  gb.setPrefSize(600, 600);

  //initialize the number of rows and columns
  int imgCol = 0;
  int imgRow = 0;


  //i am making use of an entity bean class (POJO)
  //you can edit as you deem fit
  //create a list
  List<Info> list = rentalBn.carStatus();

  //create an String array list
  /**i stored the filepath of my images to the database 
  instead of theother way round**/
  ArrayList<String> fileArray = new ArrayList<>();
   ArrayList<String> nameArray = new ArrayList<>();
   ArrayList<String> modelArray = new ArrayList<>();
   ArrayList<String> manuArray = new ArrayList<>();
   ArrayList<Float> priceArray = new ArrayList<>();
   ArrayList<String> availArray = new ArrayList<>();


  ImageView img;

  //Use for loop to populate the arraylist with elements from the list
    for(Info inf : list){
      fileArray.add(inf.getImage());
      nameArray.add(inf.getCarname());
      modelArray.add(inf.getCarmodel());
      manuArray.add(inf.getManufacturer());
      priceArray.add(inf.getPrice());
      availArray.add(inf.getAvailable());
  }

System.out.println(fileArray.size());
System.out.println(nameArray.size());

for(int i = 0; i < fileArray.size(); i++){
     System.out.println(fileArray.get(i));
     System.out.println(nameArray.get(i));

//convert to bufferedImage
BufferedImage bi;
bi = ImageIO.read(new File(fileArray.get(i)));

//convert to FXImage
 Image image = SwingFXUtils.toFXImage(bi, null);


  img = new ImageView();
  img.setFitHeight(150);
  img.setFitHeight(150);
  img.setPreserveRatio(false);
  img.setImage(image);


 //add text below the images
 Text nameText = new Text();
 nameText.setText("Car Name: " + nameArray.get(i));

 Text modelText = new Text();
 modelText.setText("Car Model: " + modelArray.get(i));

 Text manuText = new Text();
 manuText.setText("Manufacturer: " + manuArray.get(i));

 Text priceText = new Text();
 priceText.setText("Price: " + priceArray.get(i));

 Text availText = new Text();
 availText.setText("Available: " + availArray.get(i));



 //create a VBox
  VBox hb = new VBox();
  hb.setPrefSize(150, 150);


  //add the imageView and other texts inside the box          
  hb.getChildren().addAll(img, nameText, modelText, 
  manuText, priceText, availText );                                       

 //create a pane, change the background
 Pane grid = new Pane();
 grid.setStyle("-fx-background-color: #00ff00;");
 grid.setPrefSize(200, 400);
 grid.getChildren().add(hb);

  //add the pane inside the gridpane
  gb.add(grid, imgCol, imgRow);

 //then add the gridpane inside the scrollpane
  sp.setContent(gb);

  imgCol++;

  //reset the column and row
  if(imgCol >3){
      imgCol =0;

      imgRow++;
  }

}


    Scene scene = new Scene(sp, 750, 700);


    primaryStage.setTitle("stage title");
    primaryStage.setFullScreen(false);

    primaryStage.setScene(scene);
    primaryStage.show();
}

您可以参考THISPOJO/entity