我在JavaFX中的表格视图未填充数据。显示的空白行和列。空行数恰好是我拥有的行数。使用Java8。
主类
package application;
import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Test Data Generator");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
控制器类
package application;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
public class MainController implements Initializable {
@FXML
private TableView<TestSuite> table;
//@FXML private TableColumn<TestSuite, String> testClassCl;
@FXML private TableColumn<TestSuite, String> PName;
@FXML private TableColumn<TestSuite, String> CDate;
ObservableList<TestSuite> list = FXCollections.observableArrayList(new TestSuite("A", "2017-01-10"));
@Override
public void initialize(URL location, ResourceBundle resources) {
PName.setCellValueFactory(new PropertyValueFactory<TestSuite,String>("projectName"));
CDate.setCellValueFactory(new PropertyValueFactory<TestSuite,String>("createdDate"));
table.setItems(list);
table.setEditable(true);
}
}
TestSuite类
package application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
public class TestSuite {
private SimpleStringProperty projectName;
private SimpleStringProperty createdDate;
public TestSuite(String projectName, String createdDate) {
this.projectName = new SimpleStringProperty(projectName);
this.createdDate = new SimpleStringProperty(createdDate);
}
public String getprojectName() {
return projectName.get();
}
public String getcreatedDate() {
return createdDate.get();
}
}
感谢任何指针