I am quite new to Java FX and the scenebuilder.

I have a table view, filled with data coming from my database I got this working already, here is the code:

public class GridPaneController implements Initializable {

// Table
@FXML
TableView<Box> table;
@FXML
TableColumn<Box, Integer> boxId;
@FXML
TableColumn<Box, String> name;
@FXML
TableColumn<Box, Integer> groesse;
@FXML
TableColumn<Box, String> einstreu;
@FXML
TableColumn<Box, Boolean> fenster;
@FXML
TableColumn<Box, String> standort;
@FXML
TableColumn<Box, Integer> tagessatz;

ObservableList<Box> data = FXCollections.observableArrayList();

@Override
public void initialize(URL location, ResourceBundle resources) {
    boxId.setCellValueFactory(new PropertyValueFactory<Box, Integer>("boxId"));
    name.setCellValueFactory(new PropertyValueFactory<Box, String>("name"));
    groesse.setCellValueFactory(new PropertyValueFactory<Box, Integer>("groesse"));
    einstreu.setCellValueFactory(new PropertyValueFactory<Box, String>("einstreu"));
    fenster.setCellValueFactory(new PropertyValueFactory<Box, Boolean>("fenster"));
    standort.setCellValueFactory(new PropertyValueFactory<Box, String>("standort"));
    tagessatz.setCellValueFactory(new PropertyValueFactory<Box, Integer>("tagessatz"));

    List<Box> boxen = serv.zeigeAlleBoxen();    
    for( Box b : boxen ) {
        data.add(b);
    }
    table.setItems(data);
}

}

What I want to do now:

I have a "details" section in my scene which should show details of the currently selected table entry in this table. What I do not know is how to get the values of the currently selected table entry into my code.

To make it a bit more clear: In my details section I got two labels:

@FXML
String label_name;

@FXML
String label_tagessatz;

When I click on a specific table entry, label_name and label_tagessatz should be updated to the values of the clicked table entry. I assume I should define a ethod which is invoked on a onclick Event of the table view, but how to I get the values of the selected table entry?

I figured and even better method to solve this: I would have to bind the textproperty of the label to to selected table element. Now I still need to know how to get this.

有帮助吗?

解决方案

table.getSelectionModel().selectedItemProperty().addListener(
            (ObservableValue<? extends Box> ov, Box b, Box b1) -> {
    label_name.textProperty().bind(b1.nameProperty());
});

You need to have a nameProperty() method in the Box class.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top