2016-11-30 20 views
0

既存のアプリケーションにTableViewコントロールを追加しようとしています。私は(完全に実行されます)次のサンプルコードをコピーしようとしています:fxmlで作成されたTableViewは作成されません

public class FxTableViewExample1 extends Application { 

    private TableView<TransitionRow> outputTable; 
    private ObservableList<TransitionRow> data; 

    public static void main(String[] args) { 
     Application.launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Table View Example 1"); 

     // Table view, data, columns and properties 
     outputTable = new TableView<TransitionRow>(); 
     data = getInitialTableData(); 
     outputTable.setItems(data); 

     TableColumn col1 = new TableColumn("Harland"); 
     col1.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("scaleY")); 

     TableColumn col2 = new TableColumn("Gradstein"); 
     col2.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("gradsteinAge")); 

     TableColumn col3 = new TableColumn("Label"); 
     col3.setCellValueFactory(new PropertyValueFactory<TransitionRow, String>("oldName")); 

     outputTable.getColumns().setAll(col1, col2, col3); 
     outputTable.setPrefWidth(450); 
     outputTable.setPrefHeight(300); 

     // Vbox 
     VBox vbox = new VBox(20); 
     vbox.setPadding(new Insets(25, 25, 25, 25));; 
     vbox.getChildren().addAll(outputTable); 

     // Scene 
     Scene scene = new Scene(vbox, 500, 475); // w x h 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     // Select the first row 
     outputTable.getSelectionModel().select(0); 
     TransitionRow tr = outputTable.getSelectionModel().getSelectedItem(); 
     System.out.println(tr); 
    } 

    private ObservableList<TransitionRow> getInitialTableData() { 
     List<TransitionRow> list = new ArrayList<>(); 

     TransitionRow tr1 = new TransitionRow(); 
     tr1.setScaleY((Double) 124.567d); 
     tr1.setGradsteinAge((Double) 130.001d); 
     tr1.setOldName("Stuff"); 

     TransitionRow tr2 = new TransitionRow(); 
     tr2.setScaleY((Double) 456.546d); 
     tr2.setGradsteinAge((Double) 123.768d); 
     tr2.setOldName("Other stuff"); 

     list.add(tr1); 
     list.add(tr2); 

     ObservableList<TransitionRow> data = FXCollections.observableList(list); 
     return data; 
    } 
} 

マイアプリは別々のコントローラクラスでSceneBuilderを介して行われます。

[Test1Run.java]

public class Test1Run extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("Test1.fxml")); 
     stage.setTitle("Test1"); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

[Test1.fxml]

:私は上記の例を統合しようとしたときの表では、私は私の問題を示すために、次の最小限の例を作成しているので、移入されませんでした
<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="experimental.tableview.Test1Controller"> 
    <TableView fx:id="outputTable" layoutX="14.0" layoutY="14.0" prefHeight="371.0" prefWidth="569.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="17.0"> 
     <columns> 
      <TableColumn fx:id="col1" prefWidth="75.0" text="C1" /> 
      <TableColumn fx:id="col2" prefWidth="75.0" text="C2" /> 
      <TableColumn fx:id="col3" prefWidth="75.0" text="C3" /> 
     </columns> 
    </TableView> 
</AnchorPane> 

[Test1Controller.java]

public class Test1Controller implements Initializable { 
    @FXML private TableView<TransitionRow> outputTable; 
    @FXML private TableColumn<TransitionRow, Double> col1; 
    @FXML private TableColumn<TransitionRow, Double> col2; 
    @FXML private TableColumn<TransitionRow, String> col3; 

    private ObservableList<TransitionRow> data; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     outputTable = new TableView<TransitionRow>(); 

     col1 = new TableColumn<TransitionRow,Double>("Harland"); 
     col1.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("scaleY")); 

     col2 = new TableColumn<TransitionRow,Double>("Gradstein"); 
     col2.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("gradsteinAge")); 

     col3 = new TableColumn<TransitionRow,String>("Label"); 
     col3.setCellValueFactory(new PropertyValueFactory<TransitionRow, String>("oldName")); 

     // This line should cause the column names on the GUI to change. They don't. 
     outputTable.getColumns().addAll(col1, col2, col3); 

     data = getInitialTableData(); 

     // This line should cause rows of data to appear on the TableView. It doesn't. 
     outputTable.setItems(data); 

    }  

    private ObservableList<TransitionRow> getInitialTableData() { 
     List<TransitionRow> list = new ArrayList<>(); 

     TransitionRow tr1 = new TransitionRow(); 
     tr1.setScaleY((Double) 124.567d); 
     tr1.setGradsteinAge((Double) 130.001d); 
     tr1.setOldName("Stuff"); 

     TransitionRow tr2 = new TransitionRow(); 
     tr2.setScaleY((Double) 456.546d); 
     tr2.setGradsteinAge((Double) 123.768d); 
     tr2.setOldName("Other stuff"); 

     list.add(tr1); 
     list.add(tr2); 

     ObservableList<TransitionRow> results = FXCollections.observableList(list); 
     return results; 
    } 
} 

私はそれがFxTableViewEx見えるようにしたいですample1: Screenshot of working example

しかし、その代わりに、それは次のようになります。 Result of broken code

答えて

1

initializeあなたが最初の文で作成した新しいTableViewで作業中。あなたががシーンにこのテーブルを追加することはありません

...

次のコードは、TransitionRowクラスは、仕事へのPropertyValueFactoryに適した方法が含まれていると仮定すると、動作するはずです。

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    col1.setText("Harland"); 
    col1.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("scaleY")); 

    col2.setText("Gradstein"); 
    col2.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("gradsteinAge")); 

    col3.setText("Label"); 
    col3.setCellValueFactory(new PropertyValueFactory<TransitionRow, String>("oldName")); 

    data = getInitialTableData(); 

    outputTable.setItems(data); 
} 
+0

私があなたが示唆したように「初期化」を編集したところ、違いはありませんでしたか? また、「シーンに追加する」という意味を理解していません。確かに、SceneBuilder/fxmlを介してtableviewが作成され、私はそれを設定したいだけです。 – Ralph

+0

また、私はTransitionRowが最初のコードスニペットのPropertyValueFactoryで完全に機能するので、適切なメソッドを持っていると仮定します:FxTableViewExample1 – Ralph

+0

@Ralph私の修正でコードをテストしただけで動作します...何か間違ったabout ... by * "シーンに追加する" *フィールドの値をオーバーライドするときに実行していない、シーンのルートまたはそのルート自体の子孫である親にコードを追加する。 – fabian

関連する問題