I am learning JavaFX and FXML. I am using the tutorials available on the Oracle website.
In particular, I am developing an application using the Oracle example of an FXML Address Book as a starting point (https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI). What I want to do now, is to add a column with a button associated with each entry of the phone book. For example, a button that when clicked shows, on another table, more data regarding that name entry. I want to do this using FXML. Searching the web I found some resources on how to create a button column, but they look kind of confusing and it seems they don't use FXML, which is what I need.
Anybody can suggest me some tutorial or hint?
Edit: I started my work following this tutorial (https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI). Part of what I want to do, is described here: JavaFX table- how to add components?. However, this example doesn't make use of fxml. What I am trying to do is mixing the codes. So, I have a fxml file (taken from the oracle tutorial linked above) where the table is declared, this way:
<TableView fx:id="tableView" GridPane.columnIndex="0"
GridPane.rowIndex="0">
<columns>
<TableColumn fx:id="Names_column" text="Table" prefWidth="100">
<cellValueFactory><PropertyValueFactory property="name" />
</cellValueFactory>
<cellFactory>
<FormattedTableCellFactory alignment="center">
</FormattedTableCellFactory>
</cellFactory>
</TableColumn>
<TableColumn text="Color" prefWidth="100">
<cellValueFactory><PropertyValueFactory property="color" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Button" prefWidth="100">
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<ColTab name="Table 1" color="1" />
<ColTab name="Table 2" color="2" />
<ColTab name="Table 3" color"1" />
</FXCollections>
</items>
</TableView>
Then, I took a look at the other example (JavaFX table- how to add components?). In this example, everything is made only using Java, but for my purpose, I need to use fxml. In my .java main file, I did this:
TableColumn<ColTab, ColTab> btnCol = new TableColumn<>("pulsanti");
btnCol.setMinWidth(150);
btnCol.setCellValueFactory(new Callback<CellDataFeatures<ColTab, ColTab>, ObservableValue<ColTab>>() {
@Override public ObservableValue<ColTab> call(CellDataFeatures<ColTab, ColTab> features) {
return new ReadOnlyObjectWrapper(features.getValue());
}
});
btnCol.setCellFactory(new Callback<TableColumn<ColTab, ColTab>, TableCell<ColTab, ColTab>>() {
@Override public TableCell<ColTab, ColTab> call(TableColumn<ColTab, ColTab> btnCol) {
return new TableCell<ColTab, ColTab>() {
final Button button = new Button(); {
button.setMinWidth(130);
}
@Override public void updateItem(final ColTab ColTab, boolean empty) {
super.updateItem(ColTab, empty);
if (ColTab != null) {
button.setText("Do something");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
//do something
}
});
}
}
};
}
});
//table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, btnCol);
This code is copied from the second example I linked, and actually is not too clear to me. What I see is that it creates a new column and sets some properties. The last line of code (commented out) adds this column, and some other ones, to a table, which was previously declared in the original .java file and which I didn't declare, since I already declared the table in the fxml file. Now I am looking for a way to integrate this column that I am defining in the .java file, in the table that I declare in my .fxml file. I know that what i am saying might look very messy, but I am really new to JavaFX and fxml, and not all the details are clear to me.
Edit2: My code for the controller looks like this:
public class FXMLDocumentController implements Initializable {
@FXML
private TableView<ColTab> tableView;
tableView.getColumns().add(btnCol);
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Now I am getting an error that says:
package tableView does not exist
identifier expected
';' expected
I am using Netbeasn as ide.