Javafx 无法将多条数据插入到表视图中
Javafx can not insert multi data into tableview
我有一个项目需要在 javafx 的 tableview 上显示数据。问题是:如果entry中有一条数据,它可以显示在tableview上。但我想将多数据添加到列表中,但我不知道如何。我应该在哪里编辑?
public void SelectOKAction(ActionEvent event) {
String searchStr=select_text.getText();
if(searchStr==null||searchStr.trim().equals("")){
AlertBox.display("Wrong", "Please enter entry's name");
return;
}
Entry entry=entryList.search(searchStr);
if(entry==null){//NOT FOOUND
AlertBox.display("Wrong", "Entry not exists!");
}
//show data in table
OrderedList l=new OrderedLinkedList();
l.add(entry);
showDataInTable(l);
}
向表视图插入数据:
private void showDataInTable(OrderedList entryList){
ObservableList<Entry> list = FXCollections.emptyObservableList();
for(int i=0;i<entryList.size();i++){
Entry entry=entryList.get(i);
Class<? extends Entry> clz=entry.getClass();
if(Untils.checkField(clz, "journal")){
Col_Journal.setCellValueFactory(new PropertyValueFactory<Entry, String>("journal"));
}
if(Untils.checkField(clz, "booktitle")){
Col_Booktitle.setCellValueFactory(new PropertyValueFactory<Entry, String>("booktitle"));
}
if(Untils.checkField(clz, "author")){
Col_Author.setCellValueFactory(new PropertyValueFactory<Entry, String>("author_str"));
}
if(Untils.checkField(clz, "editor")){
Col_Editor.setCellValueFactory(new PropertyValueFactory<Entry, String>("editor"));
}
Col_BKey.setCellValueFactory(new PropertyValueFactory<Entry, String>("name"));
Col_Title.setCellValueFactory(new PropertyValueFactory<Entry, String>("title"));
Col_Year.setCellValueFactory(new PropertyValueFactory<Entry, String>("year"));
Col_EntryType.setCellValueFactory(new PropertyValueFactory<Entry, String>("entryType"));
list.add(entry);
}
table_id.setItems(list);
}
其中许多行应该留在循环之外
private void showDataInTable(OrderedList entryList){
ObservableList<Entry> list = FXCollections.emptyObservableList();
for(int i=0;i<entryList.size();i++){
Entry entry=entryList.get(i);
list.add(entry);
}
Col_Journal.setCellValueFactory(new PropertyValueFactory<Entry, String>("journal"));
Col_Booktitle.setCellValueFactory(new PropertyValueFactory<Entry, String>("booktitle"));
Col_Author.setCellValueFactory(new PropertyValueFactory<Entry, String>("author_str"));
Col_Editor.setCellValueFactory(new PropertyValueFactory<Entry, String>("editor"));
Col_BKey.setCellValueFactory(new PropertyValueFactory<Entry, String>("name"));
Col_Title.setCellValueFactory(new PropertyValueFactory<Entry, String>("title"));
Col_Year.setCellValueFactory(new PropertyValueFactory<Entry, String>("year"));
Col_EntryType.setCellValueFactory(new PropertyValueFactory<Entry, String>("entryType"));
table_id.setItems(list);
}
我已经去掉了checkField
,你不需要那么多类似的class,你可以用一个大的Entry
class,看起来和这个很像将未使用的字段留空
public class Entry {
private SimpleStringProperty journal = new SimpleStringProperty("");
private SimpleStringProperty booktitle = new SimpleStringProperty("");
private SimpleStringProperty author_str = new SimpleStringProperty("");
private SimpleStringProperty editor = new SimpleStringProperty("");
private SimpleStringProperty name = new SimpleStringProperty("");
private SimpleStringProperty title = new SimpleStringProperty("");
private SimpleStringProperty year = new SimpleStringProperty("");
private SimpleStringProperty entryType = new SimpleStringProperty("");
public Entry(String Sjournal, String Sbooktitle, String Sauthor_str, String Seditor, String Sname, String Stitle, String Syear, String SentryType){
journal.set(Sjournal);
booktitle.set(Sbooktitle);
author_str.set(Sauthor_str);
editor.set(Seditor);
name.set(Sname);
title.set(Stitle);
year.set(Syear);
entryType.set(SentryType);
}
}
我有一个项目需要在 javafx 的 tableview 上显示数据。问题是:如果entry中有一条数据,它可以显示在tableview上。但我想将多数据添加到列表中,但我不知道如何。我应该在哪里编辑?
public void SelectOKAction(ActionEvent event) {
String searchStr=select_text.getText();
if(searchStr==null||searchStr.trim().equals("")){
AlertBox.display("Wrong", "Please enter entry's name");
return;
}
Entry entry=entryList.search(searchStr);
if(entry==null){//NOT FOOUND
AlertBox.display("Wrong", "Entry not exists!");
}
//show data in table
OrderedList l=new OrderedLinkedList();
l.add(entry);
showDataInTable(l);
}
向表视图插入数据:
private void showDataInTable(OrderedList entryList){
ObservableList<Entry> list = FXCollections.emptyObservableList();
for(int i=0;i<entryList.size();i++){
Entry entry=entryList.get(i);
Class<? extends Entry> clz=entry.getClass();
if(Untils.checkField(clz, "journal")){
Col_Journal.setCellValueFactory(new PropertyValueFactory<Entry, String>("journal"));
}
if(Untils.checkField(clz, "booktitle")){
Col_Booktitle.setCellValueFactory(new PropertyValueFactory<Entry, String>("booktitle"));
}
if(Untils.checkField(clz, "author")){
Col_Author.setCellValueFactory(new PropertyValueFactory<Entry, String>("author_str"));
}
if(Untils.checkField(clz, "editor")){
Col_Editor.setCellValueFactory(new PropertyValueFactory<Entry, String>("editor"));
}
Col_BKey.setCellValueFactory(new PropertyValueFactory<Entry, String>("name"));
Col_Title.setCellValueFactory(new PropertyValueFactory<Entry, String>("title"));
Col_Year.setCellValueFactory(new PropertyValueFactory<Entry, String>("year"));
Col_EntryType.setCellValueFactory(new PropertyValueFactory<Entry, String>("entryType"));
list.add(entry);
}
table_id.setItems(list);
}
其中许多行应该留在循环之外
private void showDataInTable(OrderedList entryList){
ObservableList<Entry> list = FXCollections.emptyObservableList();
for(int i=0;i<entryList.size();i++){
Entry entry=entryList.get(i);
list.add(entry);
}
Col_Journal.setCellValueFactory(new PropertyValueFactory<Entry, String>("journal"));
Col_Booktitle.setCellValueFactory(new PropertyValueFactory<Entry, String>("booktitle"));
Col_Author.setCellValueFactory(new PropertyValueFactory<Entry, String>("author_str"));
Col_Editor.setCellValueFactory(new PropertyValueFactory<Entry, String>("editor"));
Col_BKey.setCellValueFactory(new PropertyValueFactory<Entry, String>("name"));
Col_Title.setCellValueFactory(new PropertyValueFactory<Entry, String>("title"));
Col_Year.setCellValueFactory(new PropertyValueFactory<Entry, String>("year"));
Col_EntryType.setCellValueFactory(new PropertyValueFactory<Entry, String>("entryType"));
table_id.setItems(list);
}
我已经去掉了checkField
,你不需要那么多类似的class,你可以用一个大的Entry
class,看起来和这个很像将未使用的字段留空
public class Entry {
private SimpleStringProperty journal = new SimpleStringProperty("");
private SimpleStringProperty booktitle = new SimpleStringProperty("");
private SimpleStringProperty author_str = new SimpleStringProperty("");
private SimpleStringProperty editor = new SimpleStringProperty("");
private SimpleStringProperty name = new SimpleStringProperty("");
private SimpleStringProperty title = new SimpleStringProperty("");
private SimpleStringProperty year = new SimpleStringProperty("");
private SimpleStringProperty entryType = new SimpleStringProperty("");
public Entry(String Sjournal, String Sbooktitle, String Sauthor_str, String Seditor, String Sname, String Stitle, String Syear, String SentryType){
journal.set(Sjournal);
booktitle.set(Sbooktitle);
author_str.set(Sauthor_str);
editor.set(Seditor);
name.set(Sname);
title.set(Stitle);
year.set(Syear);
entryType.set(SentryType);
}
}