javafx实现并显示ListView [英] javafx implementing and showing a ListView
问题描述
我想要一个带有"Persons"名称的ListView,但是当我启动该应用程序时,在ListView上,我看到了一些奇怪的字符串而不是(字符串)名称.我认为这是人"对象的强制问题.我不知道ListView应该是ListView还是ListView.因为当我尝试在listview上设置项目时,使用setItems(ObservableList)可以得到显示在列表上的怪异字符串..这是代码:
I want to have a ListView with the name of the "Persons", but on the ListView when I launch the application, I see some weird strings instead of the (string)names. I think it is a cast problem of the "Person"-object. I didnt know if the ListView should be ListView or ListView. Because when I try to set the items on the listview, using setItems(ObservableList) i get that weird Strings on list shown.. here is the code:
MainApp.java:
MainApp.java:
public static ObservableList<Person> personList;
public static void main(String[] args) {
personList = FXCollections.observableArrayList();
Person p1 = new Person("Tom");
Person p2 = new Person("Anna");
personList.add(p1);
personList.add(p2);
launch(args);
}
PersonController.java:
PersonController.java:
import static ordercheck.OrderCheck.personList;
public class PersonalController implements Initializable {
@FXML
private ListView<Person> personalList;
public void initialize(URL url, ResourceBundle rb) {
personalList.setItems(personList);
....
....
}
....
....
}
我怎么能看到真实姓名而不是这样的字符串:ordercheck.model.Person@2c5ds8dsf8sdf8
how could I see the real names and not such strings: ordercheck.model.Person@2c5ds8dsf8sdf8
谢谢!
推荐答案
任一:
a.覆盖 toString 方法您的Person类.
a. Override the toString method in your Person class.
或
b.设置 cellFactory 在ListView上,然后在此处设置格式文本.
b. Set a cellFactory on the ListView and set your formatted text there.
(a)最简单. .
这篇关于javafx实现并显示ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!