2013-11-19 1 views
1

Я пытаюсь создать простую таблицу с помощью FXML. Таблица отображается хорошо, но она не отображает мои данные. Вот моя основная программа.Таблица JavaFX FXML; почему мои данные не отображаются

public final class TableTest extends Application { 

    @Override 
    public void start(final Stage primaryStage) { 
     URL fxml = ClassLoader.getSystemClassLoader().getResource("Table.fxml"); 
     FXMLLoader fxmlLoader = new FXMLLoader(fxml); 
     TableController tableController = new TableController(); 
     fxmlLoader.setController(tableController); 
     try { 
      Pane rootPane = (Pane) fxmlLoader.load(); 
      Scene scene = new Scene(rootPane); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

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

Вот FXML.

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

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.paint.*?> 

<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml"> 
    <center> 
    <TableView fx:id="table"> 
     <columns> 
     <TableColumn text="Column 1" fx:id="col1" minWidth="80.0" /> 
     <TableColumn text="Column 2" fx:id="col2" minWidth="80.0" /> 
     </columns> 
    </TableView> 
    </center> 
</BorderPane> 

И вот контроллер.

public final class TableController implements Initializable { 

    @FXML 
    private TableView<TableData> table; 
    @FXML 
    private TableColumn<TableData, String> col1; 
    @FXML 
    private TableColumn<TableData, String> col2; 

    @Override 
    public void initialize(final URL url, final ResourceBundle resourceBundle) { 

     final ObservableList<TableData> data = FXCollections.observableArrayList(
       new TableData("C1R1", "C2R1"), 
       new TableData("C1R2", "C2R2") 
     ); 
     table.setItems(data); 

     col1.setCellValueFactory(
       new PropertyValueFactory<TableData, String>("col1Property")); 
     col2.setCellValueFactory(
       new PropertyValueFactory<TableData, String>("col2Property")); 
     table.getColumns().setAll(col1, col2); 
    } 

    private final class TableData { 

     private StringProperty col1Property = new SimpleStringProperty(); 
     private StringProperty col2Property = new SimpleStringProperty(); 

     public TableData(final String col1, final String col2) { 
      col1Property.set(col1); 
      col2Property.set(col2); 
     } 

     public void setCol1(final String col1) { 
      col1Property.set(col1); 
     } 

     public String getCol1() { 
      return col1Property.get(); 
     } 

     public StringProperty col1Property() { 
      return col1Property; 
     } 

     public void setCol2(final String col2) { 
      col2Property.set(col2); 
     } 

     public String getCol2() { 
      return col2Property.get(); 
     } 

     public StringProperty col2Property() { 
      return col2Property; 
     } 
    } 
} 

Может ли кто-нибудь сказать мне, что мне не хватает?

ответ

1

Вы ничего не пропустили! На самом деле вы, что слишком много чего-то:

new PropertyValueFactory<TableData, String>("col1Property")); 

Параметр конструктора должна быть строковым значение переменного имени соответствующего свойства без «Свойства» в конце - то есть просто «col1» в этом примере.

См. Также Can't fill Table with items with javafx, а также Javadoc для PropertyValueFactory.

+0

Спасибо! Так оно и было. –