我是JavaFX的新手。 我尝试了一个具有自定义单元格列表视图的应用程序。在列表视图单元格中,我有一个HBox,其中包含4个标签,1个imageview和2个按钮。我使用SceneBuilder创建了一个单元。我无法处理任何按钮的Onaction事件。您能检查一下我的代码并提出建议吗?所有标签和imageview都可以正常工作。
- 我的列表视图在MainSCeneController中声明
public class MainSceneController implements Initializable {
@FXML public ListView<model.Post> listView = new ListView<>();;
ObservableList <model.Post> StudentObservablelist;
public void LoadDataButtonPushed(actionEvent actionEvent) {
StudentObservablelist = FXCollections.observableArrayList();
for (int i = 0; i < Uni_Link.All_posts.size(); i++) {
StudentObservablelist.add(Uni_Link.All_posts.get(i));
}
listView.setItems(StudentObservablelist);
listView.setCellFactory(new Callback<ListView<Post>,ListCell<Post>>() {
@Override
public ListCell<Post> call(ListView<Post> studentListView) {
return new StudentListViewCell();
}
});
}
- 列表单元格看起来像这样:
public class StudentListViewCell extends ListCell<Post> {
@FXML private HBox hMain /*,hButton*/;
//@FXML private VBox V1,V2,V3,V4;
@FXML private ImageView image = new ImageView();
@FXML private Label label1 = new Label();
@FXML private Label label2 = new Label() ;
@FXML private Label label3 = new Label();
@FXML private Label label4 = new Label();
@FXML private Label label5 = new Label();
@FXML private Label label6 = new Label();
@FXML private Button reply = new Button("Reply") ;
@FXML private Button more_details = new Button("More Details") ;
FXMLLoader loader;
public StudentListViewCell(){
super();
}
protected void updateItem(Post post,boolean empty) {
super.updateItem(post,empty);
setText(null); // No text in label of super class
if (empty){
setGraphic(null);
}
else {
if(loader == null) {
try {
loader = new FXMLLoader(getclass().getResource("/view/ListViewCell.fxml"));
loader.setController(this);
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
label1.setText("Title: "+post.getTitle());
label2.setText("Status: "+post.getStatus());
label3.setText("Creator: "+post.getcreator_ID());
String photo = post.getPhoto();
image.setImage(new Image("/Images/"+photo));
setGraphic(hMain);
在此处,使用Scenebuilder创建ListViewCell.fxml文件。看起来像这样
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<HBox fx:id="hMain" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="110.0" prefWidth="691.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox fx:id="V3" prefHeight="110.0" prefWidth="130.0">
<children>
<ImageView fx:id="image" fitHeight="110.0" fitWidth="144.0" pickOnBounds="true" preserveRatio="true" />
</children>
</VBox>
<VBox fx:id="V1" alignment="TOP_CENTER" prefHeight="110.0" prefWidth="117.0" spacing="20.0">
<children>
<Label fx:id="label1" text=" " />
<Label fx:id="label2" text=" " />
</children>
<padding>
<Insets top="20.0" />
</padding>
</VBox>
<VBox fx:id="V2" alignment="TOP_CENTER" layoutX="149.0" layoutY="10.0" prefHeight="110.0" prefWidth="131.0" spacing="20.0">
<children>
<Label fx:id="label3" text=" " />
<Label fx:id="label4" text=" " />
</children>
<padding>
<Insets top="20.0" />
</padding>
</VBox>
<VBox fx:id="V4" alignment="TOP_CENTER" prefHeight="110.0" prefWidth="125.0" spacing="20.0">
<children>
<Label fx:id="label5" text=" " />
<Label fx:id="label6" prefHeight="19.0" prefWidth="116.0" text=" " />
</children>
<padding>
<Insets top="20.0" />
</padding>
</VBox>
<HBox fx:id="hButton" alignment="CENTER_LEFT" prefHeight="110.0" prefWidth="180.0" spacing="20.0">
<children>
<Button fx:id="reply" mnemonicParsing="false" onaction="#replyButtonPushed" text="Reply" />
<Button fx:id="more_details" mnemonicParsing="false" text="More Details" />
</children>
<padding>
<Insets left="20.0" />
</padding>
</HBox>
</children>
</HBox>
我必须处理listButton中每个Post单元格的ReplyButtonPushed方法,该方法是回复按钮的onaction方法。