I'm trying to find a way to add an image to a JavaFx TableView column that has data in other columns populated from a H2 database via hibernate. The TableView has been designed in JavaFx Scene Builder.
This is what I've managed to put together so far:
The Controller Class:
public class HomeController implements Initializable {
@FXML
private TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Image> KiwiId;
@FXML
private TableColumn<NewBeautifulKiwi, String> Kiwi;
public ObservableList<NewBeautifulKiwi> data;
// Initializes the controller class.
@Override
public void initialize(URL url, ResourceBundle rb) {
KiwiId.setCellFactory(new Callback<TableColumn<NewBeautifulKiwi, Image>, TableCell<NewBeautifulKiwi, Image>>() {
@Override
public TableCell<NewBeautifulKiwi, Image> call(TableColumn<NewBeautifulKiwi, Image> param) {
//Set up the ImageView
final ImageView imageview = new ImageView();
imageview.setFitHeight(50);
imageview.setFitWidth(50);
//Set up the Table
TableCell<NewBeautifulKiwi, Image> cell = new TableCell<NewBeautifulKiwi, Image>() {
public void updateItem(NewBeautifulKiwi item, boolean empty) {
if (item != null) {
imageview.setImage("arrow.png");
}
}
};
// Attach the imageview to the cell
cell.setGraphic(imageview);
return cell;
}
});
Kiwi.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, String>("Kiwi"));
KIWI_TABLE.setItems(gobbledyGook());
}
private ObservableList<NewBeautifulKiwi> gobbledyGook() {
data = FXCollections.observableArrayList();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
List courses = session.createQuery("from KIWI_TABLE").list();
for (Iterator iterator = courses.iterator(); iterator.hasNext();) {
NewBeautifulKiwi course = (NewBeautifulKiwi) iterator.next();
System.out.println(course.getKiwi());
data.add(course);
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return data;
}
}
I get an error at imageview.setImage("arrow.png");
that says Incopatibletypes: String cannot be converted to Image
.
This is my very first time trying to add an image into a TableView.
I've looked around since yesterday, but I now seem to be stuck. I was hoping for some help. I would really appreciate some help.
This is the class that creates the database pojos:
@Entity(name = "KIWI_TABLE")
public class NewBeautifulKiwi implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int KiwiId;
private String Kiwi;
public int getKiwiId() {
return KiwiId;
}
public void setKiwiId(int KiwiId) {
this.KiwiId = KiwiId;
}
public String getKiwi() {
return Kiwi;
}
public void setKiwi(String Kiwi) {
this.Kiwi = Kiwi;
}
}
Thank you all in advance.