我想在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 :(得分:0)
Hej hossein,
我想你想要建立这样的东西:
我拍了两张小图片并创建了一个新课程TopicPanel
,你可以扩展Panel
并添加两个ImageView
,例如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);
}
}
}
这是一个非常简单的示例,您可以在App中使用此TopicPanel
类
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", "patrick@example.com", null);
Person p1 = new Person("Hans", "Meier", "hans@example.com", p);
Person p2 = new Person("Karl", "Mueller", "karl@example.com", p);
Person p3 = new Person("Heinz", "Glas", "heinzk@example.com", null);
Person p4 = new Person("Beate", "Kriegtkeinab", "beate@example.com", 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;
}
}
我希望它能帮到你......
帕特里克