JavaFX 使用自定义列表单元

标签 java javafx

我试图弄清楚如何在 JavaFX 中将 CustomListCellListView 一起使用,但没有运气,我已经查找了所有我可以找到不完整的教程和问题。下面是我的 CustomListCell FXML

  <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);
    }
} 
}

还有我的 Controller

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 中更新 ImageView 和标签的正确方法。如果有人愿意提供帮助或提供我可以遵循的教程链接,我将不胜感激。

最佳答案

您设置事物的方式,即实例化 Controller 并从中获取 View 的方式,您的 Controller 需要加载 fxml 并能够返回它创建的 View 。所以像这样:

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 ;
    }

    // ...

}

这是一个测试类;这适用于上面的 Controller 类以及您的 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);
    }
}

关于JavaFX 使用自定义列表单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47896079/

相关文章:

java - 用字符替换未知的图案大小

java - 如何在旧的hadoop api的reduce中获取当前配置

java - 如何从 javafx imageView 获取 byte[]?

user-interface - Javafx 中的 ProgressBar 不会在 onAction block 中更新

java - 使用java 8 sdk播放声音

java - 创建多个具有相同属性的 HBox

java - Hadoop客户端不断从本地文件系统读取

java - Kotlin:单元测试期间 @JvmStatic 方法 NoClassDefFoundError/ClassNotFoundException

java - 请求比 java 中可能的内存更多的内存

javafx - 如何在javafx中手动跨过网格 Pane 的列?