2014-10-07 47 views
0

我想嵌入刷新圖標前面的每個treeItem的標籤在javafx中。 我該怎麼做?如何在javafx treeItem的標籤前嵌入一個圖標(除了它本身的圖形)?

private final Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("topic.png"))); 

TreeItem<String> rootNode = new TreeItem<String>("root",rootIcon); 

例如:

private final Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("topic.png"))); 
    ImageView icon = new ImageView(new Image(getClass().getResourceAsStream("refresh.png"))); 
    TreeItem<String> rootNode = new TreeItem<String>("root" + icon ,rootIcon); 

回答

0

HEJ侯賽因,

我想你想建立這樣的事情:

Example

我帶着兩個小圖片和創建一個新的類TopicPanel,你可以擴展Panel並添加兩個ImageView s到,例如HBox,現在你有一個主題形象和刷新圖像..

這裏是類

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 


public class TopicPanel extends Pane { 

    private ImageView topicImage; 
    private ImageView refreshImage; 
    private HBox pane; 


    public TopicPanel(final String topicImageUrl) { 
     try { 

      pane = new HBox(5.0); 
      this.refreshImage = new ImageView(new Image(new FileInputStream(new File("/Users/ottp/MyCloud/TreeNodeExample/src/main/resources/refresh.png")))); 

      this.topicImage = new ImageView(new Image(new FileInputStream(new File(topicImageUrl)))); 


      pane.getChildren().add(this.topicImage); 
      pane.getChildren().add(this.refreshImage); 

      this.getChildren().add(pane); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(TopicPanel.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 


} 

這是一個非常簡單的例子,你怎麼能使用這個TopicPanel類在App

import de.professional_webworkx.treenodeexample.domain.Person; 
import de.professional_webworkx.treenodeexample.panes.TopicPanel; 
import java.io.File; 
import java.io.FileInputStream; 
import java.util.ArrayList; 
import java.util.List; 
import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeView; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage; 


public class MainApp extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 

     final List<Person> persons = new ArrayList<>(); 



     Person p = new Person("Patrick", "Tester", "[email protected]", null); 
     Person p1 = new Person("Hans", "Meier", "[email protected]", p); 
     Person p2 = new Person("Karl", "Mueller", "[email protected]", p); 
     Person p3 = new Person("Heinz", "Glas", "[email protected]", null); 
     Person p4 = new Person("Beate", "Kriegtkeinab", "[email protected]", p3); 

     persons.add(p); 
     persons.add(p1); 
     persons.add(p2); 
     persons.add(p3); 
     persons.add(p4); 

     Node images = new ImageView(new Image(new FileInputStream(new File("/Users/ottp/MyCloud/TreeNodeExample/src/main/resources/alert.png")))); 
     TreeItem<Person> rootNode = new TreeItem<>(p, images); 
     for(Person person : persons) { 
      if(person.getSuperior() != null && person.getSuperior().equals(p)) { 
       Node img = new TopicPanel("/Users/ottp/MyCloud/TreeNodeExample/src/main/resources/alert.png"); 
       rootNode.getChildren().add(new TreeItem<>(person, img)); 
      } 
     } 
     TreeView<Person> treeView = new TreeView<>(rootNode); 
     Scene scene = new Scene(treeView, 1024, 768); 



     stage.setTitle("JavaFX and Maven"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

如果需要域類Person

import java.util.Objects; 

public class Person { 

    private String firstName; 
    private String lastName; 
    private String email; 

    private Person superior; 

    public Person(String firstName, String lastName, String email, Person superior) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.email = email; 
     this.superior = superior; 
    } 

    /** 
    * @return the firstName 
    */ 
    public String getFirstName() { 
     return firstName; 
    } 

    /** 
    * @param firstName the firstName to set 
    */ 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    /** 
    * @return the lastName 
    */ 
    public String getLastName() { 
     return lastName; 
    } 

    /** 
    * @param lastName the lastName to set 
    */ 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 5; 
     hash = 23 * hash + Objects.hashCode(this.getFirstName()); 
     hash = 23 * hash + Objects.hashCode(this.getLastName()); 
     hash = 23 * hash + Objects.hashCode(this.getEmail()); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Person other = (Person) obj; 
     if (!Objects.equals(this.firstName, other.firstName)) { 
      return false; 
     } 
     if (!Objects.equals(this.lastName, other.lastName)) { 
      return false; 
     } 
     if (!Objects.equals(this.email, other.email)) { 
      return false; 
     } 
     return true; 
    } 


    @Override 
    public String toString() { 
     return "Person{" + "firstName=" + getFirstName() + ", lastName=" + getLastName() + ", email=" + getEmail() + '}'; 
    } 

    /** 
    * @return the superior 
    */ 
    public Person getSuperior() { 
     return superior; 
    } 

    /** 
    * @param superior the superior to set 
    */ 
    public void setSuperior(Person superior) { 
     this.superior = superior; 
    } 
} 

我希望它會幫助你...

帕特里克