0

I want to add image to my tableview colomn. I was try various methods but i was not successful. (Read articles and some stackoverflow questions). My code is below. How can i do it?

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);
    }


}

}

main class

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);
    }
}

}

Thanks.

enginna
  • 3
  • 2
  • There are dozens of examples of this on SO. Try http://stackoverflow.com/questions/18402234/imageview-in-tableview-javafx-cant-explain-what-is-this or http://stackoverflow.com/questions/22005115/how-to-add-an-image-into-a-javafx-tableview-column for example. If you still can't get it to work, update your question with your `cellFactory`, which you haven't shown (or maybe haven't implemented). – James_D Nov 10 '15 at 16:39
  • @James_D TableData class my `cellFactory` have shown in controller class – enginna Nov 10 '15 at 16:56
  • Where? I only see a `cellValueFactory`, not a `cellFactory` – James_D Nov 10 '15 at 16:57
  • Sorry i don't have cellFactory. My complete code is above and i use fxml file. – enginna Nov 10 '15 at 17:14
  • Did you read the other examples I linked? You need a cell factory. – James_D Nov 10 '15 at 17:16
  • Yes i read examples. Trying but not successful yet. – enginna Nov 10 '15 at 17:58
  • @James_D my question not duplicate. linked answer not solve my problem. i try this link http://stackoverflow.com/questions/18402234/imageview-in-tableview-javafx-cant-explain-what-is-this but don't write CarPhoto class and getPhoto() method. i can not solve this. – enginna Nov 10 '15 at 18:57
  • I don't understand why that makes a difference. In that example the table cell has access to the image url. In your example, the data in the table cell is the `icon` property which I assume is the url of the image. If you really think there is a substantial difference between your question and the linked one, you need to edit your question to show that the solution there won't work: i.e. ***include your cell factory implementation and explain why the equivalent implementation won't work*** (hint: the equivalent implementation will work just fine). – James_D Nov 10 '15 at 19:10
  • i solve my problem to read this link http://stackoverflow.com/questions/24933164/javafx-adding-image-in-tableview thanks @James_D – enginna Nov 11 '15 at 14:10

0 Answers0