TableView,使用' setCellValueFactory'方法不起作用

时间:2015-04-03 14:09:00

标签: java user-interface javafx

此人定义了他们想要的行数,例如2.然后它创建2行对象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);
}

这是控制器内我的Table View类的代码。它根据用户输入的其他信息设置表的内容。

这是ItemSet模型

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];
    }
}

最近我很难掌握这个设置内容的概念,因为我刚从Swing移植到JavaFX。我需要指导或帮助解决这个问题。

1 个答案:

答案 0 :(得分:0)

以下是工作&#34; setCellValueFactory&#34;的完整示例。与你的比较,并尝试在你的代码中找不到工作代码:

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];
        }
    }
}