2013-10-21 2 views
0

TreeView을 사용하여 카테고리와 실제 컨텐트를 표시하고 싶습니다. 필자는이 예제를 따르기 위해 tutorial을 사용했습니다. 왜냐하면 그들은 비슷한 것을하지만 실제로 단순화하기 때문입니다. 나는이집합과 엔티티 사이의 컴퍼 지션을 벗어난 TreeView 빌드

public class Category { 
    final StringProperty categoryName = new SimpleStringProperty(); 
    final ListProperty<Contact> contactList = new SimpleListProperty<>(FXCollections.<Contact>observableArrayList()); 

    public Category(String name, List<Contact> contactList) { 
     this.categoryName.set(name); 
     this.contactList.setAll(contactList); 
    } 

    public StringProperty categoryNameProperty() { return this.categoryName; } 
    public ListProperty<Contact> contactListProperty() { return this.contactList; } 
} 

public class Contact { 
    final StringProperty contactName = new SimpleStringProperty(); 

    public Contact(String name) { 
     this.contactName.set(name); 
    } 

    public StringProperty contactNameProperty() { return this.contactName; } 
} 

같은 뭔가를 그리고 지금은 자동으로 자식 노드로 삽입 기본 연락처와 List<Category> 밖으로 트 리뷰를 구축하고자합니다. 이것이 가능한가? 가능한 경우 이 아닌 모델 자체를 수정하고 싶습니다. 나는 TreeItem<T>을 확장 할 것을 생각했으나 이것이 얼마나 나를 얻을 수 있을지는 확실치 않습니다.

답변

0

나는 이걸로 해결 했어, 누군가가 더 나은 해결책을 바꿀까?

public class Main extends Application { 
    @Override 
    public void start(Stage stage) {   
     Scene scene = new Scene(new Group()); 
     stage.setWidth(300); 
     stage.setHeight(500); 

     final TreeView<MailTreeItem> tree = new TreeView<>(); 
     final List<Category> categoryList = FXCollections.observableArrayList(); 

     tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<MailTreeItem>>() { 

      @Override 
      public void changed(
        ObservableValue<? extends TreeItem<MailTreeItem>> observable, 
        TreeItem<MailTreeItem> oldValue, 
        TreeItem<MailTreeItem> newValue) { 

       newValue.getValue(); 

      } 
     }); 

     final List<Contact> categoryContact1List = FXCollections.observableArrayList(); 
     categoryContact1List.add(new Contact("Hans")); 
     categoryContact1List.add(new Contact("Dieter")); 

     final List<Contact> categoryContact2List = FXCollections.observableArrayList(); 
     categoryContact2List.add(new Contact("Peter")); 

     categoryList.add(new Category("Freunde", categoryContact1List)); 
     categoryList.add(new Category("Feinde", categoryContact2List)); 

     final TreeItem<MailTreeItem> root = new TreeItem<MailTreeItem>(new RootTreeItem()); 
     root.setExpanded(true); 
     tree.setRoot(root); 
     for (Category category : categoryList) { 
      final List<Contact> contactList = category.contactListProperty().get(); 
      final TreeItem<MailTreeItem> categoryTreeItem = new TreeItem<MailTreeItem>(new CategoryTreeItem(category)); 

      for (Contact contact : contactList) { 
       categoryTreeItem.getChildren().add(new TreeItem<MailTreeItem>(new ContactTreeItem(contact))); 
      } 
      root.getChildren().add(categoryTreeItem); 
     } 

     final VBox vbox = new VBox(); 
     vbox.setSpacing(5); 
     vbox.setPadding(new Insets(10, 0, 0, 10)); 
     vbox.getChildren().addAll(tree); 

     ((Group) scene.getRoot()).getChildren().addAll(vbox); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

    public interface MailTreeItem { 
     public boolean isCategory(); 
     public boolean isContact(); 
     public Category getCategory(); 
     public Contact getContact(); 
    } 

    public class RootTreeItem implements MailTreeItem { 
     @Override public String toString() { return "root"; } 
     @Override public boolean isCategory() { return false; } 
     @Override public boolean isContact() { return false; } 
     @Override public Category getCategory() { return null; } 
     @Override public Contact getContact() { return null; }  
    } 

    public class CategoryTreeItem implements MailTreeItem { 
     private ObjectProperty<Category> category = new SimpleObjectProperty<>(); 

     public CategoryTreeItem(Category category) { 
      this.category.set(category); 
     } 

     public ObjectProperty<Category> categoryProperty() { return this.category; } 

     @Override public String toString() { return this.category.get().categoryNameProperty().get(); } 
     public boolean isCategory() { return true; } 
     public boolean isContact() { return false; } 
     public Category getCategory() { return this.category.get(); } 
     public Contact getContact() { return null; } 
    } 

    public class ContactTreeItem implements MailTreeItem { 
     private final ObjectProperty<Contact> contact = new SimpleObjectProperty<>(); 

     public ContactTreeItem(Contact contact) { 
      this.contact.set(contact); 
     } 

     public ObjectProperty<Contact> contactProperty() { return this.contact; } 

     @Override public String toString() { return this.contact.get().contactNameProperty().get(); } 
     public boolean isCategory() { return false; } 
     public boolean isContact() { return true; } 
     public Category getCategory() { return null; } 
     public Contact getContact() { return this.contact.get(); } 
    } 

    public class Category { 
     private final StringProperty categoryName = new SimpleStringProperty(); 
     private final ListProperty<Contact> contactList = new SimpleListProperty<>(FXCollections.<Contact>observableArrayList()); 

     public Category(String name, List<Contact> contactList) { 
      this.categoryName.set(name); 
      this.contactList.setAll(contactList); 
     } 

     public StringProperty categoryNameProperty() { return this.categoryName; } 
     public ListProperty<Contact> contactListProperty() { return this.contactList; } 
    } 

    public class Contact { 
     private final StringProperty contactName = new SimpleStringProperty(); 

     public Contact(String name) { 
      this.contactName.set(name); 
     } 

     public StringProperty contactNameProperty() { return this.contactName; } 
    } 
}