在tableview javafx中加载数据后没有显示内容

mwngjboj  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(364)

我是javafx新手,我试着学习一些教程,但是我不知道问题出在哪里。我正试图将一些数据加载到tableview中,但它总是显示一个空白表,尽管当我插入system.out.print进行测试时,它显示数据已成功加载到表中。
编辑时间:
这是duration类:

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>

下面是setupdurationsviewcontroller类:

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);
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题