В это время я узнал, как сделать программу с помощью приложения javafx fxml.Я узнаю, как отобразить listcell на listview.Я использую код ниже.Но из кода, он не может отобразить listcell на просмотр списка.Когда я запускаю программу, просто показываю listview и listcell не появляется.Пожалуйста, помогите мне.
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
Но у меня есть какая-то ошибка, подобная этой: ошибка 1
ошибка
пожалуйста, помогите мне