0

I have an application that I am building that has a table in it, I'm not using a tableview to build this table because I need each row to be able to expand similar to an accordion. I was able to achieve what I need by using a timeline and looping through the data and building each row (its kind of crude right now since I'm still working with dummy data eventually it will be a list iterator and not just a for loop) but I'm not happy with how its done. There are a lot of default values that will never change so I don't really need to set them in my worker class every time, I decided to just add them to the object class that I put together. So basically, at a high level it looks something like this:

for (int i = 0; i < 30; i++) {
    RowBuilder builder = new RowBuilder(tableBox, i);
    try {
    builder.run();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

I'm passing it the parent which is a VBox - tableBox, then I'm passing the count for later use.

Inside the RowBuilder I'm getting a new instance of the object DashboardRow which has all the defaults set in it, then I'm setting the data for each row and returning the DashboardRow.

Here is an example of a getter setting values in the DashboardRow

public HBox getMainRow() {
    mainRow.setAlignment(Pos.CENTER_LEFT);
    mainRow.setPrefHeight(60);
    mainRow.setMinHeight(60);
    mainRow.setMaxHeight(60);
    mainRow.setPrefWidth(-1);
    mainRow.setStyle("-fx-background-color:#FFFFFF;");
    return mainRow;
}

Inside the DashboardRow class I have a ton of new objects being created for every element I need in the row. There are 21 for each row, mostly VBox, HBox and StackPane to build the actual row, the rest are just labels and buttons.

This is what is looks like so far. Opened and closed states.


Is there a better way to dynamically build things like this in javafx? I'm basically pulling data from a database and looping through that data to populate a row.

1 Answer 1

0

I can't comment but it may be an answer anyway. Why can't you use the setGraphic method of a custom table cell and put the accordion node in a table. setGraphic takes any kind of node.

It sounds simpler than what you're doing.

I just tried it out with the Oracle sample and it works great. I added

    Callback<TableColumn<Person, String>, TableCell<Person, String>> accCellFactory
            = new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                @Override
                public TableCell call(TableColumn p) {
                    TitledPane t1 = new TitledPane("T1", new Button("B1"));
                    TitledPane t2 = new TitledPane("T2", new Button("B2"));
                    TitledPane t3 = new TitledPane("T3", new Button("B3"));
                    Accordion accordion = new Accordion();
                    accordion.getPanes().addAll(t1, t2, t3);
                    TableCell tc = new TableCell();
                    tc.setGraphic(accordion);
                    return tc;
                }
            };

and changed this line firstNameCol.setCellFactory(accCellFactory);

and I get

Table accordion sample

Of course you might want something other than buttons but I just copied the Accordion sample from the javadoc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.