我想将图片添加到我的tableview colomn中。我尝试了各种方法,但我没有成功。 (阅读文章和一些stackoverflow问题)。我的代码如下。我该怎么办?
Controller.java
public class Controller implements Initializable {
@FXML private TableView curTable;
@FXML private TableColumn icon;
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<TableData> data = FXCollections.observableArrayList();
try {
// some codes
data.add(new TableData("images") );
} catch (IOException | ParserConfigurationException | SAXException | DOMException e) { // Exception e
e.printStackTrace();
}
curTable.getItems().clear();
curTable.setItems(data);
icon.setCellValueFactory(new PropertyValueFactory<>("icon"));
}
public class TableData {
private final SimpleStringProperty icon;
public TableData(String curIcon) {
this.icon = new SimpleStringProperty(curIcon);
}
public String getIcon() {
return icon.get();
}
public void setIcon(String icon) {
this.icon.set(icon);
}
}
}
主要班级
public class Currency extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Application.launch(Currency.class, (java.lang.String[])null);
}
@Override
public void start(Stage primaryStage) {
try {
Image icon = new Image(getClass().getResourceAsStream("images/favicon.png"));
VBox page = (VBox) FXMLLoader.load(Currency.class.getResource("Currency.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.getIcons().add(icon);
primaryStage.setTitle("Currency");
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Currency.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
感谢。