在TableView JavaFX中加载数据后没有显示内容
我是javafx新手,我试着学习一些教程,但我不知道问题出在哪里。我试图将一些数据加载到TableView中,但它始终显示一个空白表,尽管在插入System.out.print进行测试时,它显示数据已成功加载到表中 编辑: 这是持续时间类:在TableView JavaFX中加载数据后没有显示内容,java,javafx,tableview,Java,Javafx,Tableview,我是javafx新手,我试着学习一些教程,但我不知道问题出在哪里。我试图将一些数据加载到TableView中,但它始终显示一个空白表,尽管在插入System.out.print进行测试时,它显示数据已成功加载到表中 编辑: 这是持续时间类: package test; import javafx.beans.property.*; /** * * @author MohammedAli */ public class Duration { private Simple
package test;
import javafx.beans.property.*;
/**
*
* @author MohammedAli
*/
public class Duration {
private SimpleIntegerProperty durationMonth;
private SimpleDoubleProperty fee;
public Duration(int d, double f) {
this.durationMonth = new SimpleIntegerProperty(d);
this.fee = new SimpleDoubleProperty(f);
}
public int getDurationMonth() {
return durationMonth.get();
}
public void setDurationMonth(int duration_month) {
this.durationMonth.set(duration_month);
}
public double getFee() {
return fee.get();
}
public void setFee(double fee) {
this.fee.set(fee);
}
@Override
public String toString() {
return "Duration{" + "duration_month=" + durationMonth + ", fee=" + fee + '}';
}
}
这是SetupDurationsView.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.SetupDurationsViewController">
<children>
<VBox layoutX="67.0" layoutY="224.0" spacing="25.0">
<children>
<Button layoutX="67.0" layoutY="224.0" mnemonicParsing="false" onAction="#addDuration" prefHeight="39.0" prefWidth="79.0" text="Add" textFill="#0707ff">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Button>
</children>
<HBox.margin>
<Insets left="45.0" />
</HBox.margin>
</VBox>
<TableView fx:id="tableDuration" editable="true" fixedCellSize="1.0" layoutX="211.0" layoutY="214.0" prefHeight="230.0" prefWidth="323.0">
<columns>
<TableColumn fx:id="durationMonth" prefWidth="170.0" text="Duratin (months)" />
<TableColumn fx:id="fee" prefWidth="151.0" text="Fee (BD)" />
</columns>
</TableView>
</children>
</AnchorPane>
不要认为有什么明显的错误。。因此,没有办法进行一轮良好的调试:a)要确保您在这里提供的代码与出现问题的代码完全一致,请将其更改为a(您几乎做到了:删除所有不相关的ui-仅删除表和一个按钮以添加数据,添加应用程序样板。导入和包语句(或将所有内容放在同一个包中).BTW:不要使用静态作用域(尽管它不会出现在修改后的示例中,因为它是不相关的:),不要为自定义类使用框架类名..也不要使用PropertyValueFactory:您的数据类公开属性,使用它们:)谢谢@kleopatra,我用源代码更新了我的帖子,使其尽可能简单,在补充中,我添加了程序开始的主类。**我不明白你不使用PropertyValue工厂是什么意思?如何替换它?我感觉问题来自于加载fxml文件的主类!谢谢你的好意,太完美了:)我还是有点犹豫,因为一切看起来都很好,除了。。fxml:fixedCellSize中tableView的错误配置被设置为1,因此数据/单元格/行在那里,但无法看到,因为它们太小
package test;
import java.net.URL;
import java.util.*;
import javafx.collections.*;
import javafx.fxml.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
/**
* FXML Controller class
*
* @author MohammedAli
*/
public class SetupDurationsViewController implements Initializable {
@FXML private TableView<Duration> tableDuration;
@FXML private TableColumn<Duration, Integer> durationMonth;
@FXML private TableColumn<Duration, Double> fee;
private final ObservableList data = FXCollections.observableArrayList();
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
tableDuration.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
durationMonth.setCellValueFactory(new PropertyValueFactory<Duration, Integer>("durationMonth"));
fee.setCellValueFactory(new PropertyValueFactory<Duration, Double>("fee"));
data.add(new Duration(1, 30));
data.add(new Duration(3, 50));
data.add(new Duration(6, 90));
tableDuration.setItems(data);
}
@FXML
private void addDuration() {
tableDuration.getItems().add(new Duration(2, 50));
for(int i = 0; i < tableDuration.getItems().size(); i++) {
System.out.println(tableDuration.getItems().get(i).toString());
}
}
}
package test;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;
/**
*
* @author MohammedAli
*/
public class Test extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("SetupDurationsView.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Durations And Fees");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}