Java FXML将ObservableList添加到TableView

时间:2015-08-20 13:55:45

标签: javafx fxml

我无法在表格视图中显示我的Observable List。我没有收到任何错误。我检查Observable List和List是否有价值,一切看起来都很好。我不知道问题出在哪里。我试图在FXML代码中添加ValueFactory,它仍然没有工作

FXML
 <TableView fx:id="laczenie" layoutX="641.0" layoutY="52.0" prefHeight="415.0" prefWidth="438.0">
        <columns>
          <TableColumn prefWidth="75.0" text="Wezly" fx:id="C1" >
          </TableColumn>    
          <TableColumn prefWidth="361.0" text="MozliwePolaczenia" fx:id="C2">
          </TableColumn>
        </columns>
      </TableView>
JavaFx Controller 

public class FXMLDocumentController implements Initializable {
     private JavaFXApplication4 mainApp;
    public List<Krawendzie> list = new ArrayList<Krawendzie>();
     @FXML
    private TableColumn C1;
    @FXML
     private TableColumn C2;
    @FXML
    private TableView<Krawendzie> laczenie;
    @FXML
    private Label label;
    @FXML
    private ComboBox<String> combo;
    @FXML
    private GridPane Scena;
    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        C1.setCellValueFactory(new PropertyValueFactory<Krawendzie,Integer>("Wezel"));
        C2.setCellValueFactory(new PropertyValueFactory<Krawendzie,Integer>("Mozliwosci"));
        combo.getItems().addAll("2","3","4","5","6","7","8");
        list.removeAll(list);
    }
    @FXML
    private void itemselected(ActionEvent event){
    }


     @FXML
    private void rysuj(ActionEvent event) {
        rysuj2(Integer.parseInt(combo.getValue()));

    }
    private void rysuj2(Integer Ilosc){
        list.removeAll(list);
        Scena.getChildren().clear();
        laczenie = new TableView<Krawendzie>(); 
        File f = new File("././Image/Imapges1.jpg");
        Image image = new Image(f.toURI().toString());
        Integer ilosc = 0;
        for (int i = 0; i<=Ilosc;i++)
        {
            for(int k = 1;k<Scena.getColumnConstraints().size();k=k+2)
            {   
                if(k%2 != 0)
                {
                    Circle circle = new Circle(20, 20, 20);
                    circle.setFill(new ImagePattern(image));
                    Scena.add(circle,k,i);
                    list.add(new Krawendzie(ilosc,ilosc));
                    ilosc++;
                }
                if(ilosc == Ilosc)
                    break;
            }
        if(ilosc == Ilosc)
                break;
        }
        getData();
    }
      public void getData()
    {
        ObservableList<Krawendzie> obsList = FXCollections.observableArrayList(list);
        laczenie.setItems(obsList);
        //return obsList;
    }
}


And Main Application 


public class JavaFXApplication4 extends Application {

    @Override
    public void start(Stage stage) throws Exception {        
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show(); 
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

1 个答案:

答案 0 :(得分:0)

抱歉,我有一个答案。我只需要删除  laczenie = new TableView(); 因为我创造了一个新的talbeview对象所以我无法工作