2023-02-16 315
我试图弄清楚如何使用ListView在JavaFX中使用CustomListCell,但是没有运气,我已经抬头了,我发现的只是不完整的教程和问题.以下是我的CustomListCell fxml
<AnchorPane id="AnchorPane" styleClass="backPane"
stylesheets="@../css/mainwindow.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="/zb_users/upload/2023/01/17/picture_placeholder.jpg" />
</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中更新图像视图和标签的正确方法.任何可以提供帮助的人,或者我可以遵循的教程链接,我将不胜感激.
设置设置的方式,在哪里实例化控制器并从中获取视图,您的控制器需要加载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类和模型类AS-IS(带有我的注意事项,我从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);
}
}
几个小时后解决了一切
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);
}
}
}
即使在单元格中一切都必须在
中
以上所述是小编给大家介绍的JavaFX使用一个自定义的列表单元,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33904.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态