ygetarts
ygetarts

Reputation: 944

fxml tableview just showing blank rows

I have a FXML file that looks like this:

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="GUI.Controller">
<padding>
    <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>

<HBox spacing="10" alignment="bottom_right"
      GridPane.columnIndex="1" GridPane.rowIndex="4">
    <Button text="Sign In"
            onAction="#handleSubmitButtonAction"/>
</HBox>

<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="0">
    <columns>
        <TableColumn fx:id="nameColumn" text="Event Name">
        </TableColumn>
    </columns>
</TableView>

and a controller class like this:

public class Controller implements Initializable {

@FXML
private TableView<String> tableView;

@FXML
private TableColumn<String, String> nameColumn;

@FXML
private ObservableList <String> dataArray;

@Override
public void initialize(URL location, ResourceBundle resources) {


    dataArray = FXCollections.observableArrayList();


    nameColumn.setCellValueFactory(
            new PropertyValueFactory<String, String>("nameColumn")
    );


    dataArray.add("hello");
    dataArray.add("goodbye");


    tableView.setItems(dataArray);
}

When I run this, I get two columns(not sure why - one is called "Event Name" and the other is blank) and two rows, but the rows are blank. You can click on the rows, but there is no data in them. When I remove the "hello" entry or the "goodbye" entry from the code, the table just displays one blank row instead of two.

I'm new to Java and not exactly sure why this is happening. Why are they rows showing up, but the data isn't displaying?

Upvotes: 0

Views: 922

Answers (1)

BadVegan
BadVegan

Reputation: 494

The best way to use table is use class for representing data in table. In your example I changed String to TableModel where you have one PropertyString name greetings. Now you can easy use this class to show data in tabe.

Controller:

public class Controller implements Initializable {

  @FXML private TableView<TableModel> tableView;

  @FXML private TableColumn<TableModel, String> nameColumn;

  @FXML private ObservableList<TableModel> dataArray;

  @Override public void initialize(URL location, ResourceBundle resources) {

    //add data to Observable List
    dataArray = FXCollections.observableArrayList(new TableModel("hello"), new TableModel("goodbye"));

    //crete one column, name "greetings" is the same name property which is in TableModel
    nameColumn.setCellValueFactory(new PropertyValueFactory<TableModel, String>("greetings"));

    tableView.setItems(dataArray);
  }

  public static class TableModel {

    StringProperty greetings = new SimpleStringProperty();

    public TableModel(String greetings) {
      this.greetings = new SimpleStringProperty(greetings);
    }

    public String getGreetings() {
      return greetings.get();
    }

    public StringProperty greetingsProperty() {
      return greetings;
    }

    public void setGreetings(String greetings) {
      this.greetings.set(greetings);
    }
  }

}

You must add this code to resolve problem with two columns.

<columnResizePolicy>
     <TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
 </columnResizePolicy>

Here is your FXML with small changes.

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml" fx:controller="GUI.Controller">
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
    </padding>
    <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="0">
        <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
        </columnResizePolicy>
        <columns>
            <TableColumn fx:id="nameColumn" text="Event Name"/>
        </columns>
    </TableView>
    <HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="1" GridPane.rowIndex="1">
        <Button text="Sign In" onAction="#handleSubmitButtonAction"/>
    </HBox>
</GridPane>

Upvotes: 2

Related Questions