2014-12-12 6 views
3

내 애플 리케이션 용 JavaFx로 사용자 정의 ListView를 만드는 방법. 각 줄 listView에 대해 이미지와 HBox가 필요합니다. enter image description hereJavaFX에서 사용자 정의 컨텐츠가있는 ListView

+0

당신은 사용자 정의 셀 공장을 필요 ListView.setCellFactory를 통해 사용자 정의 CellFactory (...)

Example

작업을 예를 제공해야하고, 설정 listView.setCellFactory를 사용하여 곧 답변을 작성하겠습니다. – Adam

+0

감사합니다. 셀 내부에 사용자 정의 위젯이 아닌 관찰 가능한 목록 안에 사용자 정의 객체가있는 listView에 대한 예제 만 보았습니다. – NCNecros

+0

임의의 내용을 추가하는 예를 추가했습니다. 노드, 텍스트 필드 등이 될 수 있습니다. – Adam

답변

4

당신은

public class CustomListView extends Application { 
    private static class CustomThing { 
     private String name; 
     private int price; 
     public String getName() { 
      return name; 
     } 
     public int getPrice() { 
      return price; 
     } 
     public CustomThing(String name, int price) { 
      super(); 
      this.name = name; 
      this.price = price; 
     } 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     ObservableList<CustomThing> data = FXCollections.observableArrayList(); 
     data.addAll(new CustomThing("Cheese", 123), new CustomThing("Horse", 456), new CustomThing("Jam", 789)); 

     final ListView<CustomThing> listView = new ListView<CustomThing>(data); 
     listView.setCellFactory(new Callback<ListView<CustomThing>, ListCell<CustomThing>>() { 

      @Override 
      public ListCell<CustomThing> call(ListView<CustomThing> arg0) { 
       return new ListCell<CustomThing>() { 

        @Override 
        protected void updateItem(CustomThing item, boolean bln) { 
         super.updateItem(item, bln); 
         if (item != null) { 
          VBox vBox = new VBox(new Text(item.getName()), new Text(String.format("%d $", item.getPrice()))); 
          HBox hBox = new HBox(new Label("[Graphic]"), vBox); 
          hBox.setSpacing(10); 
          setGraphic(hBox); 
         } 
        } 

       }; 
      } 

     }); 

     StackPane root = new StackPane(); 
     root.getChildren().add(listView); 
     primaryStage.setScene(new Scene(root, 200, 250)); 
     primaryStage.show(); 
    } 

} 

+1

예! 이거 야! 감사! – NCNecros

+0

'fxml' (items)을 사용해서 그것을 할 수 있습니까? – Alexiuscrow

+0

@Alexiuscrow 나는 FXML에 정의 된 ListView에 사용자 정의 셀 팩터를 연결할 수 있다고 생각하지 않습니다. – Adam

관련 문제