2017-12-19 1 views
2

ListView과 함께 사용하는 방법을 알아 내려고했지만 JavaFX에 행운을 찾지 못했습니다. 찾은 것만 큼 불완전한 자습서와 질문이 있습니다. 다음은 내 CustomListCell FXML사용자 지정 목록 셀을 사용하는 JavaFX

<AnchorPane id="AnchorPane" styleClass="backPane" 
     stylesheets="@../css/mainwindow.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
    <HBox alignment="CENTER_LEFT" styleClass="backPane"> 
    <children> 
     <HBox styleClass="card" HBox.hgrow="ALWAYS"> 
      <children> 
       <ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true"> 
       <image> 
        <Image url="@../images/picture_placeholder.png" /> 
       </image> 
       </ImageView> 
       <VBox HBox.hgrow="ALWAYS"> 
       <children> 
        <Label fx:id="lbTitle" styleClass="fixture-title" stylesheets="@../css/mainwindow.css" text=" Livingston 19:45 Falkirk" /> 
        <Label fx:id="lbDescription" styleClass="fixture-description" text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at turpis nisl. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum laoreet elementum velit. Curabitur tincidunt finibus malesuada. Aliquam dapibus semper scelerisque. Sed tristique tellus eget sem ornare cursus." /> 
       </children></VBox> 
      </children> 
      <HBox.margin> 
       <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
      </HBox.margin> 
     </HBox> 
     </children> 
     </HBox> 
     </children> 
    </AnchorPane> 

그리고 내 모델

public class Ticket { 


private long id; 
private String imageUrl; 
private String title; 
private String description; 

public Ticket(long id, String imageUrl, String title, String description) { 
    this.id = id; 
    this.imageUrl = imageUrl; 
    this.title = title; 
    this.description = description; 
} 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public String getImageUrl() { 
    return imageUrl; 
} 

public void setImageUrl(String imageUrl) { 
    this.imageUrl = imageUrl; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

} 

그리고 내 불완전 CustomListCell

public class TicketCell <Ticket> extends ListCell<Ticket> { 

private final TicketCellController ticketCellController = new TicketCellController(); 
private final Node view = ticketCellController.getView(); 

@Override 
protected void updateItem(Ticket item, boolean empty) { 
    super.updateItem(item, empty); 
    if (empty) { 
     setGraphic(null); 
    } else { 
     ticketCellController.setTicket(item); 
     setGraphic(view); 
    } 
} 
} 

그리고 내 컨트롤러

public class TicketCellController implements Initializable{ 

private static final String TAG = MainWindowController.class.getSimpleName(); 
private Logger logger; 
private Ticket ticket; 
@FXML 
private Label lbTitle; 
@FXML 
private Label lbDescription; 
@FXML 
private AnchorPane anchorPane; 



@Override 
public void initialize(URL url, ResourceBundle rb) { 
    logger = Logger.getLogger(MainWindowController.class); 
    BasicConfigurator.configure(); 
} 

하시기 바랍니다 아니다 그 ticketCellController.getView()을 찾을 수 없습니다. 이 시점에서 길을 잃었습니다. 내 CustomListCell FXML에서 이미지 뷰 및 라벨을 업데이트하는 올바른 방법을 알지 못합니다. 누구나 따라갈 수있는 자습서에 대한 링크가 있으면 도움을받을 수 있습니다. 감사하겠습니다.

+0

해야했습니다 해결 당신은 분명히 단지'TicketCell은 listcell 요소 '확장 ... 또한, 그 클래스에 대한 –

+0

를 코드를 게시한다 (안'TicketCell 를 ...') –

+0

@James_D 내가 내 컨트롤러 –

답변

2

컨트롤러를 인스턴스화하고 컨트롤러에서 뷰를 가져 오는 방식으로 컨트롤러에서 fxml을로드하고 생성 한 뷰를 반환 할 수 있어야합니다. 그래서 다음과 같이됩니다 :

public class TicketCellController { 

    private Ticket ticket; 
    @FXML 
    private Label lbTitle; 
    @FXML 
    private Label lbDescription; 

    private AnchorPane anchorPane; 

    public TicketCellController() { 

     try { 
      // assumes FXML file is in same package as this controller 
      // (also make sure name of FXML resource is correct) 
      FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomListCell.fxml")); 
      loader.setController(this); 
      anchorPane = loader.load(); 
     } catch (IOException exc) { 
      // pretty much fatal here... 
      throw new UncheckedIOException(exc); 
     } 
    } 

    public void setTicket(Ticket ticket) { 
     lbTitle.setText(ticket.getTitle()); 
     lbDescription.setText(ticket.getDescription()); 
    } 

    public Node getView() { 
     return anchorPane ; 
    } 

    // ... 

} 

테스트 클래스입니다. 위의 컨트롤러 클래스와 FXML, CustomListCell 클래스 및 모델 클래스를 그대로 사용합니다 (FXML에서 스타일 시트와 이미지를 제거한 경우 해당 클래스에 액세스 할 수 없으므로주의해야합니다).

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.ListView; 
import javafx.stage.Stage; 

public class Test extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ListView<Ticket> ticketList = new ListView<Ticket>(); 
     ticketList.setCellFactory(lv -> new TicketCell()); 
     for (int i = 1 ; i <= 50 ; i++) { 
      ticketList.getItems().add(new Ticket(i, "", "Ticket "+i, "This is a description of ticket "+i)); 
     } 
     primaryStage.setScene(new Scene(ticketList, 600, 400)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
시간 후
1

이 모든

public class TicketCell extends ListCell<Ticket> { 
private static final String TAG = TicketCell.class.getSimpleName(); 
private Logger logger; 
private Ticket ticket; 
@FXML 
private Label lbTitle; 
@FXML 
private Label lbDescription; 
@FXML 
private AnchorPane anchorPane; 
private FXMLLoader mLLoader; 

public TicketCell(){ 
    logger = Logger.getLogger(MainWindowController.class); 
    BasicConfigurator.configure(); 
} 


public void updateItem(Ticket pos,boolean empty){ 
super.updateItem(pos, empty); 

if(pos == null){ 
setText(null); 
setGraphic(null); 
}else{ 

    if (mLLoader == null) { 
      mLLoader = new FXMLLoader(getClass().getResource("/resources/fxml/TicketDesignCell.fxml")); 
      mLLoader.setController(this); 

      try { 
       mLLoader.load(); 
      } catch (IOException e) { 
       logger.error(TAG, e); 
      } 
logger.error(TAG + " Loading content: "+pos.getTitle()); 
} 


    this.lbTitle.setText(pos.getTitle()); 
    this.lbDescription.setText(pos.getDescription()); 


    setText(null); 
    setGraphic(anchorPane); 


} 

} 

}

모든 문제가 TicketCellController``와 경우 셀 클래스에서도

+0

이것은 좋은 해결책이 아닙니다.'updateItem (...)'메소드가 호출 될 때마다 FXML을로드하고 파싱합니다. 한 번만로드하고 해당 메소드에서 업데이트해야 할 내용을 업데이트해야합니다. 모든 것을 세포 계급에 넣을 이유가 없습니다. –

관련 문제