1

I have a question about the TableView element in JavaFX.

When I try to use it and add columns to it, it works fine, but if I set it to static (need to access it from another stage) the columns are not appearing at all.

My code looks like this (extracts):

@FXML
private static TableView<Datensatz> table = new TableView();

@Override
public void initialize(URL url, ResourceBundle rb) {
erstelleTabelle();
}

private static void erstelleTabelle() {
    TableColumn nameColumn = new TableColumn("Name");
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));

    TableColumn handyNrColumn = new TableColumn("Handynummer");
    handyNrColumn.setCellValueFactory(new PropertyValueFactory<>("Handynummer"));

    TableColumn rechnungsDatumColumn = new TableColumn("Rechnungsdatum");
    rechnungsDatumColumn.setCellValueFactory(new PropertyValueFactory<>("Rechnungsdatum"));

    TableColumn abrechnungsBeginnColumn = new TableColumn("Abrechnungsbeginn");
    abrechnungsBeginnColumn.setCellValueFactory(new PropertyValueFactory<>("Von"));

    TableColumn abrechnungsEndeColumn = new TableColumn("Abrechnungsende");
    abrechnungsEndeColumn.setCellValueFactory(new PropertyValueFactory<>("Bis"));

    TableColumn rechnungsnummerColumn = new TableColumn("Rechnungsnummer");
    rechnungsnummerColumn.setCellValueFactory(new PropertyValueFactory<>("Rechnungsnummer"));

    TableColumn betragColumn = new TableColumn("Betrag");
    betragColumn.setCellValueFactory(new PropertyValueFactory<>("Betrag"));

    TableColumn kostenstelleColumn = new TableColumn("Kostenstelle");
    kostenstelleColumn.setCellValueFactory(new PropertyValueFactory<>                                ("Kostenstelle"));

    TableColumn telefonKostenColumn = new TableColumn("Telefon-Kosten");
    telefonKostenColumn.setCellValueFactory(new PropertyValueFactory<>("telefon"));

    TableColumn smsKostenColumn = new TableColumn("SMS-Kosten");
    smsKostenColumn.setCellValueFactory(new PropertyValueFactory<>("sms"));

    TableColumn internetKostenColumn = new TableColumn("Internet-Kosten");
    internetKostenColumn.setCellValueFactory(new PropertyValueFactory<>("internet"));

    TableColumn vertragsDetailsColumn = new TableColumn("Vertragsdetails");
    vertragsDetailsColumn.setCellValueFactory(new PropertyValueFactory<>("details"));
    vertragsDetailsColumn.setMinWidth(100);

    table.getColumns().addAll(
            nameColumn,
            handyNrColumn,
            rechnungsDatumColumn,
            abrechnungsBeginnColumn,
            abrechnungsEndeColumn,
            rechnungsnummerColumn,
            betragColumn,
            kostenstelleColumn,
            telefonKostenColumn,
            smsKostenColumn,
            internetKostenColumn,
            vertragsDetailsColumn);

}

public static void setTable(ObservableList datensatz) {
    table.setItems(datensatz);
}

So when I change all static methods/variables to non-statics, everything just works fine.

Has anyone an idea what I could try to do? Thank you!

1 Answers1

0

Your problem is that your static variable is not the same Table View that you see in your UI - FXMLLoader can only inject instance members, not static members. At any rate, it's not good design to have a UI element as a static member, because it can only appear once in the application scene graph anyway.

Change your TableView member back to a non-static member, don't initialize it (the FXMLLoader will inject the actual control in the UI assuming it has the proper fx:id attribute). To access it from another controller see this question

Community
  • 1
  • 1
Itai
  • 6,641
  • 6
  • 27
  • 51