Issue
The person defines the amount of rows they want, for e.g. 2. Then it creates 2 rows of object ItemSet.
public void initialize(URL location, ResourceBundle resources) {
btnCreateShop.setOnAction(e -> validate(0));
ObservableList<String> options =
FXCollections.observableArrayList(
"False", "True"
);
cbPermission.setItems(options);
cbPermission.getSelectionModel().selectFirst();
tbShopMaker.setDisable(true);
}
private void validate(int idValidate) { //This method basically ALLOWS the table to be intialized IF some random conditions are met.
if(idValidate == 0){
if(tfShopName.getText().trim().length() == 0){
//stuff occurs... ignore this line and if statement
}
tbShopMaker.setDisable(false);
initializeTableView();
}
}
@FXML TableColumn tcRow1, tcRow2, tcRow3, tcRow4, tcRow5, tcRow6, tcRow7, tcRow8, tcRow9;
private void initializeTableView() {
lblShopName.setText("Shop Name: " + tfShopName.getText());
lblShopRows.setText("Shop Rows: " + tfShopRows.getText());
final ObservableList<ItemSet> data = FXCollections.observableArrayList();
for(int i=0;i<Integer.parseInt(tfShopRows.getText()); i++){
data.add(new ItemSet());
}
tcRow1.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item1")
);
tcRow2.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item2")
);
tcRow3.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item3")
);
tcRow4.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item4")
);
tcRow5.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item5")
);
tcRow6.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item6")
);
tcRow7.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item7")
);
tcRow8.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item8")
);
tcRow9.setCellValueFactory(
new PropertyValueFactory<ItemSet, Integer>("item9")
);
tblShop.getItems().setAll(data);
tblShop.setEditable(true);
}
That is the code for my Table View class inside the controller. It sets the Table's contents based on the other information inputted by the user.
Here is the ItemSet model
public class ItemSet {
int[] items = new int[9];
public ItemSet(int... args){
for(int i = 0; i<args.length; i++){
items[i] = args[i];
}
}
public int getItem1(){
return items[0];
}
public int getItem2(){
return items[1];
}
public int getItem3(){
return items[2];
}
public int getItem4(){
return items[3];
}
public int getItem5(){
return items[4];
}
public int getItem6(){
return items[5];
}
public int getItem7(){
return items[6];
}
public int getItem8(){
return items[7];
}
public int getItem9(){
return items[8];
}
}
It has been tough for me lately to grasp this concept of setting contents, considering I've just immigrated from Swing to JavaFX. I need guidance, or help on how this.
Solution
Here is a full example with working "setCellValueFactory". Compare it with yours, and try to find not working code in yours:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ExampleApp extends Application
{
@Override
public void start( Stage stage )
{
TableView<ItemSet> tableView = initializeTableView();
VBox grid = new VBox( 20 );
grid.getChildren().addAll( tableView );
Scene scene = new Scene( grid, 450, 250 );
stage.setScene( scene );
stage.show();
}
private TableView<ItemSet> initializeTableView()
{
final ObservableList<ItemSet> data = FXCollections.observableArrayList();
for ( int i = 0; i < 4; i++ )
{
data.add( new ItemSet( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) );
}
TableColumn<ItemSet, Integer> tcRow1 = new TableColumn<>( "tcRow1" );
tcRow1.setCellValueFactory(
new PropertyValueFactory<>( "item1" )
);
TableColumn<ItemSet, Integer> tcRow2 = new TableColumn<>( "tcRow2" );
tcRow2.setCellValueFactory(
new PropertyValueFactory<>( "item2" )
);
TableColumn<ItemSet, Integer> tcRow3 = new TableColumn<>( "tcRow3" );
tcRow3.setCellValueFactory(
new PropertyValueFactory<>( "item3" )
);
TableColumn<ItemSet, Integer> tcRow4 = new TableColumn<>( "tcRow4" );
tcRow4.setCellValueFactory(
new PropertyValueFactory<>( "item4" )
);
TableColumn<ItemSet, Integer> tcRow5 = new TableColumn<>( "tcRow5" );
tcRow5.setCellValueFactory(
new PropertyValueFactory<>( "item5" )
);
TableColumn<ItemSet, Integer> tcRow6 = new TableColumn<>( "tcRow6" );
tcRow6.setCellValueFactory(
new PropertyValueFactory<>( "item6" )
);
TableColumn<ItemSet, Integer> tcRow7 = new TableColumn<>( "tcRow7" );
tcRow7.setCellValueFactory(
new PropertyValueFactory<>( "item7" )
);
TableColumn<ItemSet, Integer> tcRow8 = new TableColumn<>( "tcRow8" );
tcRow8.setCellValueFactory(
new PropertyValueFactory<>( "item8" )
);
TableColumn<ItemSet, Integer> tcRow9 = new TableColumn<>( "tcRow9" );
tcRow9.setCellValueFactory(
new PropertyValueFactory<>( "item9" )
);
TableView<ItemSet> tableView = new TableView();
tableView.getColumns().addAll( tcRow1, tcRow2, tcRow3, tcRow4, tcRow5, tcRow6, tcRow7, tcRow8, tcRow9 );
tableView.getItems().setAll( data );
tableView.setEditable( true );
return tableView;
}
public static void main( String[] args )
{
launch( args );
}
public static class ItemSet
{
int[] items = new int[9];
public ItemSet( int... args )
{
for ( int i = 0; i < args.length; i++ )
{
items[i] = args[i];
}
}
public int getItem1()
{
return items[0];
}
public int getItem2()
{
return items[1];
}
public int getItem3()
{
return items[2];
}
public int getItem4()
{
return items[3];
}
public int getItem5()
{
return items[4];
}
public int getItem6()
{
return items[5];
}
public int getItem7()
{
return items[6];
}
public int getItem8()
{
return items[7];
}
public int getItem9()
{
return items[8];
}
}
}
Answered By - Uluk Biy
Answer Checked By - David Marino (JavaFixing Volunteer)