0

let's consider this We have an article and an article may be stored in many magasins(Stores) and vice versa we can store many articles in one Store.

lets say we have

class Article{
    int id_article;
    String name;
    List<Magasin> magasins;

    List<Magasin> getMagasins{
    return magasins;
}

lets say that Magasin class look like this

class Magasin{
int id_magasin;
String name;

//getters and setters
}

And let's say I have My tableView in javafx : TableView tblArticles;

first column contain article name and second column Want it to contain name of all magasins (stores ) that the articles is stored in ( magasin-1, magasin-2 .. magasin-N)

I want also to ask if I can make the cell as a column that means that every name of song will be display vertically just like a list view and all of them for sure for one specif artist ..

UPDATE : this is the code I have added to my controller :

//FRom FXML file
@FXML
TableColumn<Article, List<Magasin>> colMag ;


PropertyValueFactory<Article, List<Magasin>> mag = new PropertyValueFactory<>("magasins");

//CellValueFactory
colMag.setCellValueFactory(mag);

//cell Factory 
    colMag2.setCellFactory( col -> {
    ListView<Magasin> listView = new ListView<>();
    listView.setCellFactory(lv -> new ListCell<Magasin>() {
        @Override
        public void updateItem(Magasin magasin, boolean empty) {
            super.updateItem(magasin, empty);
            if (empty) {
                setText(null);
            } else {
                setText(magasin.getLibelle());
            }
        }
    });
    return new TableCell<Article, List<Magasin>>() {
        @Override
        public void updateItem(List<Magasin> mags, boolean empty) {
            super.updateItem(mags, empty);
            if (empty) {
                setGraphic(null);
            } else {
                listView.getItems().setAll(mags);
                setGraphic(listView);
            }
        }
    };
});

When I run this I got the error :

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
    at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:99)
    at javafx.collections.ModifiableObservableListBase.setAll(ModifiableObservableListBase.java:88)
    at controllers.DashboardController$6.updateItem(DashboardController.java:1180)
    at controllers.DashboardController$6.updateItem(DashboardController.java:1173)
    at javafx.scene.control.TableCell.updateItem(TableCell.java:663)
    at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
    at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
    at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
    at javafx.scene.control.Control.impl_processCSS(Control.java:872)
khalid tounoussi
  • 499
  • 5
  • 21
  • 2
    You could definitely set a `ListView` as the `graphic` of the `TableCell` (via a custom cell factory). However, you may want to consider a `TreeTableView`. – Slaw Sep 20 '19 at 03:22
  • Can you please provide some code of the first solution using ListView ? For the second solution, there are two things to consider , if the singer has only one song I want to set exactly the name of the song, if he has many so I can make it as a Tree and the name in the top is "Songs" , is using a TreeTableView can do this ? – khalid tounoussi Sep 20 '19 at 09:50
  • For the first solution, check out the code in [this question](https://stackoverflow.com/questions/35608027/javafx-tableview-containing-listview-updates-cellvaluefactory), specifically their `PartsTableCell` implementation; however, don't forget to call `super.updateItem(item, empty)`—doing so is critical. As for your question regarding the second question, I'm not sure I understand what you're asking. A `TreeTableView` is just like a `TreeView` except it has columns. – Slaw Sep 20 '19 at 10:33
  • I have seen the code, and I think it doesn't work anymore – khalid tounoussi Sep 20 '19 at 11:19
  • 2
    Why don't you think it works anymore? If you've attempted to implement the behavior you want but are encountering problems, please [edit] your question to provide a [mre] and explain what is _actually_ happening versus what _should be_ happening. – Slaw Sep 20 '19 at 11:44
  • SimpleStringProperty can not be converted to Observable ! – khalid tounoussi Sep 20 '19 at 11:48
  • 1
    Well, a `SimpleStringProperty` _is-an_ `Observable` (and an `ObservableValue)`. Please provide a [mre] demonstrating the issue. – Slaw Sep 20 '19 at 11:54
  • I have added An update , please check . – khalid tounoussi Sep 20 '19 at 12:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199755/discussion-between-slaw-and-outreagous-one). – Slaw Sep 21 '19 at 05:51

0 Answers0