2016-12-24 41 views
-1

我似乎無法找到填充我的表:(如果我看他們seperatly他們播種給我即時我的控制檯,即使我添加手冊反對我observebleList它的工作,它只是不加我的數據inported ....我不能填充我的表格視圖在Java FX

This is my Controller: 
package Interface; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.ResourceBundle; 
import javafx.application.Platform; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.control.cell.TextFieldTableCell; 
public class ControllerForFinalPoint implements Initializable { 
    String id ; 
    String x ; 
    String y ; 
    String z ; 
    public void exitApplication(ActionEvent exitApplication) { 
     Platform.exit(); 
    } 
    public void intoarcere (String id, String x, String y, String z) { 
     this.id = id; 
     System.out.println(id); 
     this.x = x; 
     System.out.println(x); 
     this.y = y; 
     System.out.println(y); 
     this.z = z; 
     System.out.println(z); 
    } 

    @FXML 
    private TableView<MyPointFinal> tableCoord2; 
    @FXML 
    private TableColumn<MyPointFinal, String> Id; 
    @FXML 
    private TableColumn<MyPointFinal, String> X; 
    @FXML 
    private TableColumn<MyPointFinal, String> Y; 
    @FXML 
    private TableColumn<MyPointFinal, String> Z; 

    ObservableList<MyPointFinal> data = FXCollections.observableArrayList(
        new MyPointFinal(id, x, y, z)); 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

     Id.setCellValueFactory(new PropertyValueFactory<MyPointFinal, String>("Id")); 
     Id.setCellFactory(TextFieldTableCell.forTableColumn()); 
     X.setCellValueFactory(new PropertyValueFactory<MyPointFinal, String>("X")); 
     X.setCellFactory(TextFieldTableCell.forTableColumn()); 
     Y.setCellValueFactory(new PropertyValueFactory<MyPointFinal, String>("Y")); 
     Y.setCellFactory(TextFieldTableCell.forTableColumn()); 
     Z.setCellValueFactory(new PropertyValueFactory<MyPointFinal, String>("Z")); 
     Z.setCellFactory(TextFieldTableCell.forTableColumn()); 
     tableCoord2.setItems(data); 

    } 

} 
<?xml version="1.0" encoding="UTF-8"?> 

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

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Interface.ControllerForFinalPoint"> 
    <children> 
     <TableView fx:id="tableCoord2" editable="true" prefHeight="558.0" prefWidth="600.0"> 
     <columns> 
      <TableColumn fx:id="Id" prefWidth="75.0" text="Name" /> 
      <TableColumn fx:id="X" prefWidth="150.0" text="X" /> 
      <TableColumn fx:id="Y" prefWidth="150.0" text="Y" /> 
      <TableColumn fx:id="Z" prefWidth="150.0" text="Z" /> 
     </columns> 
     </TableView> 
     <AnchorPane prefHeight="59.0" prefWidth="700.0"> 
     <children> 
      <Button layoutX="118.0" layoutY="34.0" mnemonicParsing="false" text="Save" AnchorPane.bottomAnchor="17.0" AnchorPane.leftAnchor="325.0" AnchorPane.rightAnchor="325.0" AnchorPane.topAnchor="17.0" /> 
     </children> 
     </AnchorPane> 
     <AnchorPane prefHeight="38.0" prefWidth="600.0" /> 
    </children> 
</VBox> 

回答

0

我認爲這個問題是方法不是在你認爲的順序調用。首先JavaFX的創建實例,並因此使用變量'id','x','y','z'初始化ObservableList data字段,之後javafx將調用initialize()方法,該方法使用了上述的ObservableList,然後您可以調用intoarcere()設置先前用於構建Obse的字段rvableList內容....(哎呀)。

所以實際的MyPointFinal實例在那裏(當你記錄它的內容時已經確認),儘管沒有任何東西可見,因爲每個值都是'null'。

如果您將intoarcere()更改爲以下內容,您應該會看到結果。

public void intoarcere (String id, String x, String y, String z) 
{ 
    this.data.add(new MyPointFinal(id, x, y, z)); 
} 

Ps。目前您使用大寫和小寫'x','y'和'z'的變量名稱。不僅是'PascalCased'變量名稱與一般命名約定相反,它也不會讓人混淆(對於其他人而言,也可能會在一段時間之後)。請嘗試將它們重命名爲你自己。

+0

tx,對於第一個問題,它不是那個問題。我發現我的數據被列入我的列表中,爲空,我需要調用另一方向的方法或做其他事情,至於「PS」謝謝你,我會牢記在心。 –