我正在尝试将数据添加到使用场景构建器创建的tableview中 当我尝试运行程序时,tableview中只显示一列 我能错过什么我已经查看了可用的示例,我看不出哪里出错了。 这是我的Fxml文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="352.0" prefWidth="423.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TableController">
<children>
<TableView fx:id="tableview" layoutX="16.0" layoutY="48.0" prefHeight="200.0" prefWidth="392.0">
<columns>
<TableColumn fx:id="firstcol" prefWidth="108.0" text="first" />
<TableColumn fx:id="lastcol" prefWidth="157.0" text="lastame" />
<TableColumn fx:id="phonecol" prefWidth="130.0" text="phone" />
</columns>
</TableView>
</children>
</AnchorPane>
我的控制器类
package application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class TableController {
@FXML private TableView <Person>tableview;
@FXML private TableColumn<Person,String> firstcol;
@FXML private TableColumn <Person,String>lastcol;
@FXML private TableColumn <Person,String>phonecol;
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Bob", "Smith","77777"),
new Person("Bob", "Smith","999"),
new Person("george", "Smith","263737")
);
@FXML
protected void initialize() {
//setting up the columns
firstcol.setCellValueFactory( new PropertyValueFactory<Person, String>("first"));
lastcol.setCellValueFactory( new PropertyValueFactory<Person, String>("last") );
phonecol.setCellValueFactory( new PropertyValueFactory<Person, String>("phone") );
//setting up the table data source
tableview.setItems( data );
}
}
我的主要课程
package application;
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) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("Table.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我的班级人员
package application;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private final SimpleStringProperty first;
private final SimpleStringProperty last;
private final SimpleStringProperty phone;
public Person(
String first,
String last,
String phone
) {
this.first = new SimpleStringProperty( first );
this.last = new SimpleStringProperty( last );
this.phone = new SimpleStringProperty( phone );
}
public String getFirstName() {
return first.get();
}
public void setFirstName(String first) {
this.first.set( first );
}
public String getLastName() {
return last.get();
}
public void setLastName(String last) {
this.last.set( last );
}
public String getPhone() {
return phone.get();
}
public void setPhone(String phone) {
this.phone.set( phone );
}
}
电话栏是显示
的列