簡體   English   中英

JavaFX TableView與SceneBuilder

[英]JavaFX TableView With SceneBuilder

我有一個非常簡單的類,它接收作業列表並嘗試用它們填充TableView。 我可以在不使用FXML的情況下進行此工作,但是現在我正在嘗試新的方法。 我的問題是;

tView.getItems().addAll(jobList);

返回一個空指針,但是,如果我創建一個新的TableView,表中根本沒有數據。

我相信我要出問題的地方是使用SceneBuilder中構造的TableView並沒有正確初始化它,但是我對如何執行此操作感到困惑。 這是我的InterFace方法類;

public class InterfaceMethods {

    @FXML TableView<Job> tView;

    @SuppressWarnings("unchecked")
    public void populateTableView(Connection connection, Stage stage) throws Exception {

        Parent root;
        root = FXMLLoader.load(getClass().getResource("TableViewInterface.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);

        JobDataAccessor jobAccessor = new JobDataAccessor();
        final String query = "SELECT * FROM progdb.adamJobs";
        List<Job> jobList = jobAccessor.getJobList(connection, query);
        //System.out.println(query);
        //System.out.println(connection);
        tView.getItems().addAll(jobList);
    }
}

這是我的MainApp類;

public class MainApp extends Application {

private CheckUserDetails checkDetails;
private String pString, username = System.getProperty("user.name");
private InterfaceMethods iMethods;
private Connection connection;
@FXML private Button submitButton;
@FXML private PasswordField passField;
@FXML private Label warnLabel;

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

@FXML
private void loginCheck() throws Exception {
    checkDetails = new CheckUserDetails();
    pString = new String();
    pString = passField.getText();
    System.out.println(pString);
    if (checkDetails.checkUserDetails(pString, username) != null) {
        setConnection(checkDetails.connection);
        handleTableView();
    } else {
        warnLabel.setVisible(true);
    }
}

@FXML
public void handleTableView() throws Exception {
    iMethods = new InterfaceMethods();

    Stage stage = (Stage) submitButton.getScene().getWindow();  

    connection = getConnection();

    iMethods.populateTableView(connection, stage);
}

public static void main(String[] args) {
    launch(args);
}

最后,這是我的兩個FXML文件之一,其中一個包含TableView。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.InterfaceMethods">
   <children>
      <TableView fx:id="tView" layoutX="17.0" layoutY="19.0" prefHeight="347.0" prefWidth="570.0">
        <columns>
          <TableColumn prefWidth="75.0" text="CaseNO." />
          <TableColumn prefWidth="75.0" text="Case Notes" />
            <TableColumn prefWidth="75.0" text="Date Created" />
            <TableColumn prefWidth="75.0" text="Deadline" />
            <TableColumn prefWidth="75.0" text="Priority" />
            <TableColumn prefWidth="75.0" text="Completed" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </children>
</AnchorPane>

getJobList方法(在另一個類中);

    public List<Job> getJobList(final Connection connection, final String query) throws SQLException {
    try (Statement stmnt = connection.createStatement(); ResultSet rs = stmnt.executeQuery(query);) {
        jobList = new ArrayList<>();
        System.out.println(connection);
        System.out.println(query);
        while (rs.next()) {
            caseNumber = rs.getString("CaseNO");
            caseNotes = rs.getString("CaseNotes");
            dateCreated = rs.getString("DateCreated");
            deadlineDate = rs.getString("Deadline");
            prioritySetting = rs.getInt("Priority");
            completedSetting = rs.getString("Completed");
            job = new Job(caseNumber, caseNotes, dateCreated, deadlineDate, prioritySetting, completedSetting);
            jobList.add(job);
        }
        return jobList;
    }
}

您需要為FXML定義控制器。

例如,您的FXML部分是:

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="288.0" prefWidth="293.0" xmlns:fx="http://javafx.com/fxml">
<children>
    <TableView fx:id="tableView" layoutX="35.0" layoutY="28.0" prefHeight="200.0" prefWidth="227.0">
    <columns>
        <!-- Columns for table view-->
    </columns>
    </TableView>
</children>
</AnchorPane>

1)為您的FXML頁面創建一個控制器,例如:

 fx:controller="com.code.Controller"

2)現在,在您的控制器中,您可以執行以下操作:

package com.code;

    public class Controller implements Initializable {

        @FXML private TableView<User> tableView;

控制器類的所有@FXML注釋字段都由FXMLLoader實例化,同時加載具有與該控制器類或您通過fxmlLoader.setController()設置的類相等的fx:controller的fxml文件。 如果您自己實例化控制器類,則這些@FXML注釋字段將為null。 因此,這里是在fxml文件中使用setController()代替fx:controller的示例方法:

主程序

public class MainApp extends Application
{
    @Override
    public void start( Stage stage ) throws Exception
    {
        MainController mainController = new MainController(getConnection());

        Scene scene = new Scene( mainController.getLayout() );

        stage.setScene( scene );
        stage.show();
    }


    public static void main( String[] args )
    {
        launch( args );
    }
}

主控制器

public class MainController implements Initializable
{

    @FXML
    private Label label;

    private Parent layout;

    private Connection connection;


    public MainController(Connection connection)
    {
        this.connection = connection;

        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Main.fxml" ) );
        fxmlLoader.setController( this );
        try
        {
            layout = ( Parent ) fxmlLoader.load();
        }
        catch ( IOException exception )
        {
            throw new RuntimeException( exception );
        }
    }


    public Parent getLayout()
    {
        return layout;
    }


    @FXML
    private void handleTableView( ActionEvent event )
    {
        InterfaceMethods iMethods = new InterfaceMethods( connection );
        label.getScene().setRoot(iMethods.getLayout());
        label.getScene().getWindow().sizeToScene();
    }


    @Override
    public void initialize( URL url, ResourceBundle rb )
    {
        // do work with @FXML annotated fields in here
        label.setText( "Initial data!");
    }

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleTableView" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

接口方法

public class InterfaceMethods implements Initializable
{

    @FXML
    TableView<Job> tView;

    private Parent layout;

    private Connection connection;


    public MainController( Connection connection )
    {
        this.connection = connection;

        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "TableViewInterface.fxml" ) );
        fxmlLoader.setController( this );
        try
        {
            layout = ( Parent ) fxmlLoader.load();
        }
        catch ( IOException exception )
        {
            throw new RuntimeException( exception );
        }
    }


    public Parent getLayout()
    {
        return layout;
    }

    @Override
    public void initialize( URL url, ResourceBundle rb )
    {
        // do work with @FXML annotated fields in here
        tView.setItems(...);
    }

}

請注意,Main.fxml和TableViewInterface.fxml中沒有fx:controller

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM