0

I'd like to have a tableView like below:

id | b | a | choiceBox
--------------------------
0 | 0 | 0 | 0/1
1 | 0 | 1 | 0/1
2 | 1 | 0 | 0/1
3 | 1 | 1 | 0/1

I get the IDs depending on a Choice Box value into my table. But i dont know how to assign a my List bits to multiple columns (i.e a,b,c....

I tried using the example given here:How can I associate data (List<SimpleStringProperty> with the table columns of a table view But didn't work.

Also how do i add the Choice Boxes to a last column.

MWE: TestClass:

package test;

import java.util.ArrayList;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewTest extends Application {

    private ObservableList<TableWrapper> items = FXCollections.observableArrayList();
    private int count;
    private TableView tv;

    @Override
    public void start(Stage primaryStage) {
        ChoiceBox cb = new ChoiceBox();
        cb.setItems(FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"));
        cb.getSelectionModel().select(0);
        cb.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue ov, Number value, Number new_value) {
                count = new_value.intValue();
                createTableData();
                createTableView();
            }
        });

        VBox root = new VBox();
        root.getChildren().add(cb);
        tv = new TableView();
        createTableData();
        createTableView();
        root.getChildren().add(tv);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Nested Obs list test");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void createTableView() {
        tv.setEditable(false);
        tv.getColumns().clear();
        tv.setPlaceholder(new Label("No truthtable"));
        tv.getColumns().clear();
        ArrayList<TableColumn> columns = new ArrayList<>();
        columns.clear();
        TableColumn<String, String> tc;
        if (count > 0) {
            tc = new TableColumn<>("#");
            tc.setResizable(false);
            tc.setSortable(false);
            tc.setCellValueFactory(
                    new PropertyValueFactory<>("id"));
            double tmp = 1 / (count + 2);
            tc.widthProperty().multiply(tmp);
            tc.setStyle("-fx-alignment: CENTER-RIGHT;");
            columns.add(tc);

        }
        for (int i = count; i > 0; i--) {
            tc = new TableColumn(Character.toString((char) (i + 96)));
            tc.setResizable(false);
            tc.setSortable(false);
            int index = i;
            /*
            tc.setCellValueFactory(cellData
                    -> new SimpleStringProperty(cellData.getValue().getBits())[index]));
             */
            double tmp = 1 / (count + 2);
            tc.widthProperty().multiply(tmp);
            columns.add(tc);
        }
        tv.getColumns().addAll(columns);
        tv.setItems(items);

    }

    private void createTableData() {
        ArrayList<TableWrapper> tmpData = new ArrayList<>();
        int max = (int) Math.pow(2, count);
        for (int i = 0; i < max; i++) {
            tmpData.add(new TableWrapper(i, count));
        }
        items = FXCollections.observableArrayList(tmpData);
    }
}

Wrapper:

package test;

import java.util.List;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;

public class TableWrapper {

    private final SimpleStringProperty id;
    private final List<SimpleStringProperty> bits;
    private final ChoiceBox cb;

    public TableWrapper(int id, int length) {
        this.id = new SimpleStringProperty(String.valueOf(id));
        this.cb = new ChoiceBox();
        this.bits = FXCollections.observableArrayList();
        String str = Integer.toBinaryString(id);
        for (int i = 0; i < length - str.length(); i++) {
            bits.add(new SimpleStringProperty("0"));
        }
        for (int i = 0; i < str.length(); i++) {
            bits.add(new SimpleStringProperty(String.valueOf(str.charAt(i))));
        }
        cb.setItems(FXCollections.observableArrayList("0", "1"));
        cb.getSelectionModel().select(0);
    }

    public StringProperty idProperty() {
        return id;
    }

    public String[] getBits() {
        String[] tmp = new String[bits.size()];
        for (int i = 0; i < bits.size(); i++) {
            tmp[i] = bits.get(i).toString();
        }
        return tmp;
    }
}
Avinta
  • 678
  • 1
  • 9
  • 26
  • 2
    Can you get rid of all the raw types in the code? (And it's not at all clear why you are using `TableColumn`.) – James_D Oct 20 '17 at 18:23
  • I just dont get the concept of the TableView... so i assume TableColumn might be completly wrong. What raw types? – Avinta Oct 20 '17 at 19:07
  • You have `TableView` instead of, I guess, `TableView` and `ChoiceBox` instead of `ChoiceBox` (though I have no idea why you are putting strings in the choice box, when you seem to want to treat them as integers anyway). – James_D Oct 20 '17 at 19:18
  • There is a tutorial [here](http://code.makery.ch/library/javafx-8-tutorial/part1/) that many people seem to like, that does a lot of examples with `TableView`. – James_D Oct 20 '17 at 19:22
  • I will have a look and reply i i can't solve my problem. I am very new to javaFx thats why i though i have to populate a ChoiceBox with Strings – Avinta Oct 20 '17 at 19:36
  • Got it. Thanks for the tutorial, was very helpful. – Avinta Oct 21 '17 at 11:02
  • Are you trying to do this: https://stackoverflow.com/questions/46171183/javafx-tableview-data-from-list/46173351#46173351 ? – Sunflame Oct 21 '17 at 11:20

0 Answers0