与此处的问题类似:Javafx tableview with data from multiple classes 我正在尝试创建一个由多个类组成的表。后端是sqlite表,我正在尝试实现这里找到的Toxi解决方案:http://tagging.pui.ch/post/37027745720/tags-database-schemas这就是为什么我的简化示例就是这样 - 分成不必要的类。
这是我试图弄清楚的Tag列。一个包装类是最简洁的方法,一个BookmarkTag类?您可以设置事件处理程序以在更新行时填写此其他列吗?最终,单元格将包含更好的标签显示(可点击图标)。
谢谢。
我的主要课程:
package complextableview;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ComplexTableView extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Bookmark> bookmarks = FXCollections.observableArrayList();
ObservableList<Tag> tags = FXCollections.observableArrayList();
ObservableList<TagMap> tagMapList = FXCollections.observableArrayList();
TableView<Bookmark> bookmarkTV = new TableView<>(bookmarks);
TableColumn<Bookmark, String> bookmarkNameCol = new TableColumn<>("URL");
// What to put here?
//TableColumn<?, String> bookmarkTagCol = new TableColumn<>("Tags");
bookmarkNameCol.setCellValueFactory(new PropertyValueFactory<>("Name"));
// What to put here?
//bookmarkTagCol.setCellValueFactory(new PropertyValueFactory<>("?"));
bookmarkTV.getColumns().add(bookmarkNameCol);
//bookmarkTV.getColumns().add(bookmarkTagCol);
// Populate data
bookmarks.add(new Bookmark(0, "stackoverflow.com"));
bookmarks.add(new Bookmark(1, "reddit.com"));
tags.add(new Tag(0, "work"));
tags.add(new Tag(1, "home"));
tags.add(new Tag(2, "fun"));
tagMapList.add(new TagMap(0, 0, 0)); // 1st mapping = google & "work"
tagMapList.add(new TagMap(1, 1, 1)); // 2nd mapping = reddit & "home"
tagMapList.add(new TagMap(2, 1, 2)); // 3rd mapping = reddit & "fun"
// Table Output
// URL Tags
// stackoverflow.com 'work'
// reddit.com 'home,fun'
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for ( TagMap tagMapping : tagMapList ){
if ( tagMapping.getBookmarkId() == 1)
sb1.append(tags.get(tagMapping.getTagId()).getString());
else
sb2.append(tags.get(tagMapping.getTagId()).getString());
}
System.out.println(sb1.toString());
System.out.println(sb2.toString());
StackPane root = new StackPane();
root.getChildren().add(bookmarkTV);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("ComplexTableView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
其他课程:
书签
package complextableview;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Bookmark {
private final StringProperty name;
private final IntegerProperty id;
public Bookmark() {
id = new SimpleIntegerProperty(this, "id", 0);
name = new SimpleStringProperty(this, "name", "");
}
public Bookmark(Integer id, String name) {
this();
setId(id);
setName(name);
}
public final Integer getId() {return id.get();}
public final void setId(Integer value) {id.set(value);}
public IntegerProperty getIdProperty() {return id;}
public final String getName() {return name.get();}
public final void setName(String value) {name.set(value);}
public StringProperty getNameProperty() {return name;}
}
标签
package complextableview;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Tag {
public final StringProperty string;
public final IntegerProperty id;
public Tag() {
id = new SimpleIntegerProperty(this, "id", 0);
string = new SimpleStringProperty(this, "string", "");
}
public Tag(Integer id, String string) {
this();
setId(id);
setString(string);
}
public final Integer getId() {return id.get();}
public final void setId(Integer value) {id.set(value);}
public IntegerProperty idProperty() {return id;}
public final String getString() {return string.get();}
public final void setString(String value) {string.set(value);}
public StringProperty stringProperty() {return string;}
}
TagMap
package complextableview;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class TagMap {
public final IntegerProperty id;
public final IntegerProperty bookmarkId;
public final IntegerProperty tagId;
public TagMap(){
id = new SimpleIntegerProperty(this, "id", 0);
bookmarkId = new SimpleIntegerProperty(this, "bookmarkId", 0);
tagId = new SimpleIntegerProperty(this, "tagId", 0);
}
public TagMap(Integer id, Integer bmId, Integer tagId) {
this();
setId(id);
setBookmarkId(bmId);
setTagId(tagId);
}
public final Integer getId() {return id.get();}
public final void setId(Integer value) {id.set(value);}
public IntegerProperty idProperty() {return id;}
public final Integer getTagId() {return tagId.get();}
public final void setTagId(Integer value) {tagId.set(value);}
public IntegerProperty tagIdProperty() {return tagId;}
public final Integer getBookmarkId() {return bookmarkId.get();}
public final void setBookmarkId(Integer value) {bookmarkId.set(value);}
public IntegerProperty bookmarkIdProperty() {return bookmarkId;}
}