gpt4 book ai didi

java - ListCell 不显示在 ListView 上

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:59 27 4
gpt4 key购买 nike

这时候我学会了如何用javafx fxml应用程序制作一个程序。我了解如何在 ListView 上显示列表单元格。我使用下面的代码。但从代码来看,它无法在listview上显示listcell。当我运行程序时,只显示 ListView ,而列表单元格不会出现。请帮助我。

Main.java

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));

Scene scene = new Scene(root);

primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ListView fx:id="listView" layoutX="67.0" layoutY="29.0" prefHeight="320.0" prefWidth="376.0" />
</children>
</AnchorPane>

Student.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package belajarlistview;

/**
*
* @author kuupie
*/
public class Student {

private static int studentIdAct = 0;
private int studentId;
private String name;
private GENDER gender;

enum GENDER {
MALE,
FEMALE
}

public Student(String name, GENDER gender) {
studentId = studentIdAct++;
this.name = name;
this.gender = gender;
}

public int getStudentId() {
return studentId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public GENDER getGender() {
return gender;
}

public void setGender(GENDER gender) {
this.gender = gender;
}
}

ListCell.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane fx:id="listCellDetail" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="39.0" prefWidth="421.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="label1" alignment="CENTER" prefHeight="36.0" prefWidth="137.0" text="Label">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label fx:id="label2" alignment="CENTER" prefHeight="36.0" prefWidth="137.0" text="Label" GridPane.columnIndex="2" />
<FontAwesomeIconView fx:id="fxIconGender" strokeLineCap="ROUND" strokeLineJoin="ROUND" GridPane.columnIndex="1" />
</children>
</GridPane>

StudentListViewCell.java

import java.io.IOException;

/**
* Created by Johannes on 23.05.16.
*
*/

public class StudentListViewCell extends ListCell<Student> {

@FXML
private Label label1;

@FXML
private Label label2;

@FXML
private FontAwesomeIconView fxIconGender;

@FXML
private GridPane gridPane;

private FXMLLoader mLLoader;

@Override
protected void updateItem(Student student, boolean empty) {
super.updateItem(student, empty);

mLLoader = new FXMLLoader(getClass().getResource("/fxml/ListCell.fxml"));
mLLoader.setController(this);

try {
mLLoader.load();
} catch (IOException e) {
e.printStackTrace();
}

label1.setText(String.valueOf(student.getStudentId()));
label2.setText(student.getName());

if(student.getGender().equals(Student.GENDER.MALE)) {
fxIconGender.setIcon(FontAwesomeIcon.MARS);
} else if(student.getGender().equals(Student.GENDER.FEMALE)) {
fxIconGender.setIcon(FontAwesomeIcon.VENUS);
} else {
fxIconGender.setIcon(FontAwesomeIcon.GENDERLESS);
}

setText(null);
setGraphic(gridPane);
}
}

Controller.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package belajarlistview;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

@FXML
private ListView<Student> listView;

private final ObservableList<Student> studentObservableList;

public Controller() {

studentObservableList = FXCollections.observableArrayList();

//add some Students
studentObservableList.addAll(
new Student("John Doe", Student.GENDER.MALE),
new Student("Jane Doe", Student.GENDER.FEMALE),
new Student("Donte Dunigan", Student.GENDER.MALE),
new Student("Gavin Genna", Student.GENDER.MALE),
new Student("Darin Dear", Student.GENDER.MALE),
new Student("Pura Petty", Student.GENDER.FEMALE),
new Student("Herma Hines", Student.GENDER.FEMALE)
);


}

@Override
public void initialize(URL location, ResourceBundle resources) {
listView.setItems(studentObservableList);
listView.setCellFactory(studentListView -> new StudentListViewCell());

}

}

我尝试了很多代码来解决这个问题,通过添加此代码setGraphic(student == null ? null : gridPane);StudentListViewCell.java和添加此代码fx:controller="belajarlistview.Controller"Main.fxml

但是我遇到了一些这样的错误: error 1

error

请帮助我

最佳答案

好的我遇到问题了。这段代码的主要问题 mLLoader = new FXMLLoader(getClass().getResource("/fxml/ListCell.fxml")); 只需将这一个 ("/fxml/ListCell.fxml") 更改为 ("ListCell.fxml") 程序运行良好。非常感谢@fabian

关于java - ListCell 不显示在 ListView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52569590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com