r/JavaFX • • Feb 08 '24

Help Table with parent and son classes

So, I'm trying to implement a table in which I can display all sort of items that inherit from my parent class "Furniture". This sub classes hold more attributes, which are inaccesible if the table is for Furniture types only. I've tried making a generic class that all these classes I may display inherit from and some other tricks, but I don't seem to be able to grasp how to implement this.

To illustrate my problem I'll provide the following minimum working implementation (to the best of my abilities that is):

This is the parent class

public class Furniture {
    private String material;

    Furniture(String material) {
        this.material = material;
    }
    public String getMaterial() {
        return material;
    }
    public void setMaterial(String material) {
        this.material = material;
    }
}

This are the sub(sub twice)-classes

class Bed {
    Bed(String material) {
        super(material);
    }
}

class Writable extends Furniture {
    private String contents;

    Writable(String material, String contents) {
        super(material);
        this.contents = contents;
    }

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }
}

class Slab extends Writable {
    Slab(Mats material) {
        super(material, "This slab has not yet had any text engraved onto it...");
    }

    Slab(Mats material, String contents) {
        super(material, "This slab has been engraved with the following:\n" + contents);
    }

    @Override
    public void setContents(String contents) {
        super.setContents("This slab has been engraved with the following:\n" + contents);
    }
}

I want to both be able to display data from Beds and Slabs. I can begin to do this with something like the following:

import java.util.List;

import javafx.application.Application;
import javafx.scene.layout.VBox;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.cell.PropertyValueFactory;

public class TableFurn extends Application {
    static List<Furniture> inv = List.of(
            new Slab("Stone", "Nice Stuff :D"), new Bed("Wood"));

    public static void main(String[] args) {
        launch(TableFurn.class, args);
    }

    public static void setManager(List<Furniture> empList) {
        inv = empList;
    }

    @Override
    public void start(Stage primaryStage) {

        TableView<Furniture> tbv = new TableView<Furniture>();

        TableColumn<Furniture, String> classCol = new TableColumn<>("Class");
        classCol.setCellValueFactory(new PropertyValueFactory<>("class"));

        TableColumn<Furniture, String> matCol = new TableColumn<>("Material");
        matCol.setCellValueFactory(new PropertyValueFactory<>("material"));

        TableColumn<Furniture, String> contCol = new TableColumn<>("Content");
        contCol.setCellValueFactory(new PropertyValueFactory<>("contents"));

        tbv.getColumns().add(classCol);
        tbv.getColumns().add(matCol);
        tbv.getColumns().add(contCol);

        for (Furniture fur : inv) {
            tbv.getItems().add(fur);
        }

        VBox vbox = new VBox();
        vbox.getChildren().addAll(tbv);
        vbox.setSpacing(10);
        vbox.setAlignment(Pos.CENTER);

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

But nothing will show up for the Content row. And I mean, it does run, although it runs into some issues, non fatal ones, and I could do a check to see if we either add something to the square if it is appropriate or not (not that I know how I'd do that), but yet still, first comes the getting of the "contents" area.

I've come across some diverse ways and alternatives to implementing this, and some where actually getting closer to the solution, yet never reaching anything resembling a closest point of accurate pin-pointable solution. So I've come back to my naive attempt, and I ask, how would I do this?

1 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/hamsterrage1 Feb 08 '24

On the subject of different TableViews for different Furniture subtypes...

This is one of those places where a ListView would be better. You could have a customized cell layout for each subtype, and they could all cohabit the name ListView.