此时,我学习了如何使用javafx-fxml应用程序制作程序。我了解了如何在listview上显示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);
}
}
主要的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());
}
}
我尝试了很多代码来解决它,通过添加此代码setGrapics(学生==null?null: gridPane);上的学生列表视图Cell.java
并添加此代码fx:控制器="belajarlistview. Controller"
上的Main.fxml
但我有一些类似的错误:错误1
错误
请帮帮我
好吧,我有问题了。此代码的主要问题是mLLoader=新的FXMLLoader(getClass()。getResource(“/fxml/ListCell.fxml”)
只需将这个(“/fxml/ListCell.fxml”)
更改为(“ListCell.fxml”)
程序运行良好。非常感谢fabian
我试图在应用程序启动时显示项目的listview。但在应用程序启动时,没有显示listview,只有一个空白屏幕。下面是代码: MainActivity.java 但是现在我在logcat中得到了以下内容: 并且应用程序停止..请帮助!
我试图在片段中填充一个具有自定义布局的listView。但是,当我启动应用程序时,listview的内容没有加载(这是使用数组适配器获得的)。下面是加载listView的代码: 这里是我的CustomAdapter的代码: } 这是列表项布局的xml: 这里是主要布局: 我没有收到任何错误消息,但是当我启动应用程序时,listView中没有显示任何项目,并且永远不会调用我的适配器的getView方
我有3节课。MainActivity,OneFragment,TwoFragment,我在OneFragment上使用Sqlite填充了listview,并且我从TwoFragment中添加了记录(使用我的SqlHelper类使用的CreateData函数),但是新记录没有显示在listview中。 我该怎么解决这个? 如果你想看我可以上传的代码。
搜索视图显示在每个列表视图行:检查附加的scrrenshot。 我怎样才能让它只在顶端出现一次呢。 在此输入图像描述
我正在使用Android Studio v3。1.2. 我有一对带有列表视图和按钮的活动。无论ListView中有多少项,按钮都应对齐底部。 在我的主活动中,ListView和按钮在RelativeLayout上完美渲染。我可以添加很多项目到ListView和按钮保持不变。 另一方面,我的JournalAbFragment是一个在表格布局中使用RelativeLayout的片段。该片段由PetDe
我不知道如何解决这个问题,我似乎找不到一个会导致它失败的问题。下面是GUI代码的其余部分。它很长。将jtable添加到jpanel从第152行开始。