使用SceneBuilder8构建的ListView不显示插入的列表

时间:2016-02-04 23:31:06

标签: javafx scenebuilder

我正在尝试为将在我的办公室中使用的程序的前端创建GUI。我的任务是使用Scene Builder 8来帮助创建所述GUI。这是我使用JavaFX和Scene Builder的第一个项目,所以我必须从头开始学习所有东西。我的问题是,虽然代码不会给我带来任何错误,但是我看不到我已经在程序中添加的示例数据。它让我对整个程序提出质疑。

如果以下某些代码看起来多余,可能是因为程序后端部分的开发是由另一个人同时进行的。

这是Mainapp:

package simit.gui;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import simit.gui.launch.model.LocalLaunch;
import simit.gui.launch.model.TextAreaOutputStream;
import simit.gui.launch.view.CompleteList;
import simit.gui.launch.view.LocalLaunchController;
import simit.gui.launch.view.MonitorController;


public class Main extends Application {
    //Hold onto the main state
    Stage primaryStage;

    private ObservableList<simit.gui.launch.view.CompleteList> listData = FXCollections.observableArrayList(); 

    public Main(){
        listData.add(new CompleteList("sample1"));
        listData.add(new CompleteList("sample2"));
    }

    public ObservableList<CompleteList> getListData(){
        return listData;
    }

    //Run simulation
    public void runSimulation(File inputDeck, int procs, int mem){

        try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("launch/view/Monitor.fxml"));
        AnchorPane rootLayout = (AnchorPane) loader.load();

        //Get the controller
        MonitorController controller = loader.getController();

        //Setup the view
        List<TextAreaOutputStream> textAreas = controller.SetUpTextAreas(procs);



        //Try to launch the code
        LocalLaunch launch  = new LocalLaunch(inputDeck, procs, mem, textAreas);

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
//      try {
//          BorderPane root = new BorderPane();
//          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();
//      }

         try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(Main.class.getResource("launch/view/LocalLaunch.fxml"));
                AnchorPane rootLayout = (AnchorPane) loader.load();

                // Show the scene containing the root layout.
                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();

                //Load up the LocalLaunch Controller
                LocalLaunchController controller = loader.getController();
                controller.SetMain(this);

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

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

这是FXML控制器:

package simit.gui.launch.view;

import java.io.File;

import org.controlsfx.control.CheckListView;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import simit.gui.Main;
import simit.gui.launch.model.LocalLaunch;

public class LocalLaunchController {

    //Hold onto the required text fields
    @FXML
    private TextField path;

    @FXML
    private TextField procs;

    @FXML
    private TextField mem;

    final ObservableList<CompleteList> items = FXCollections.observableArrayList();

    @FXML
    final public CheckListView<CompleteList> listItems = new CheckListView<CompleteList>();


    private Main mainProgram;



    public void SetMain(Main mainProgram){
        this.mainProgram = mainProgram;
        items.addAll(mainProgram.getListData());
        listItems.setItems(items);
    }

    @FXML
    private void handleSelect() {
        FileChooser fileChooser = new FileChooser();
         fileChooser.setTitle("Open Resource File");
         fileChooser.getExtensionFilters().addAll(
                 new ExtensionFilter("SMT File", "*"+LocalLaunch.PaserExt),
                 new ExtensionFilter("Resume File", "*"+LocalLaunch.RsmExp),
                 new ExtensionFilter("All Files", "*.*"));
         File selectedFile = fileChooser.showOpenDialog(null);
         if (selectedFile != null) {
            path.setText(selectedFile.getAbsolutePath());
         }
    }

    @FXML
    private void handleRun() {
        //Make sure input is valid
        if(!isInputValid())
            return;

        //Get the data
        File tmpFile = new File(path.getText());
        int proc = Integer.parseInt(procs.getText());
        int mem = Integer.parseInt(this.mem.getText());

        mainProgram.runSimulation(tmpFile, proc, mem);
    }

       /**
     * Validates the user input in the text fields.
     * 
     * @return true if the input is valid
     */
    private boolean isInputValid() {
        String errorMessage = "";

        if(path.getText().isEmpty()){
            errorMessage += "The input deck cannot be empty!\n"; 
        }

        //Create a test file
        File testFile = new File(path.getText());
        if(!testFile.exists())
            errorMessage += "The input deck must exisit!\n"; 

        if (procs.getText() == null || procs.getText().length() == 0) {
            errorMessage += "No valid procs specified!\n"; 
        } else {
            // try to parse the postal code into an int.
            try {
                int p =Integer.parseInt(procs.getText());
                if(p <= 0)
                    throw new NumberFormatException();
            } catch (NumberFormatException e) {
                errorMessage += "No valid procs specified (must be a positive integer)!\n"; 
            }
        }
        if (mem.getText() == null || mem.getText().length() == 0) {
            errorMessage += "No valid mem specified!\n"; 
        } else {
            // try to parse the postal code into an int.
            try {
                int p =Integer.parseInt(mem.getText());
                if(p <= 0)
                    throw new NumberFormatException();
            } catch (NumberFormatException e) {
                errorMessage += "No valid mem specified (must be a positive integer)!\n"; 
            }
        }


        if (errorMessage.length() == 0) {
            return true;
        } else {
            // Show the error message.
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Invalid Fields");
            alert.setHeaderText("Please correct invalid fields");
            alert.setContentText(errorMessage);

            alert.showAndWait();

            return false;
        }
    }


    @FXML
    private void initialize() {  
    }
}

这是我为输入变量制作的课程:

package simit.gui.launch.view;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class CompleteList {

    private final StringProperty listDetails;

    public CompleteList(){
        this(null);
    }
    public CompleteList(String listDetails){
        this.listDetails = new SimpleStringProperty(listDetails);
    }
    public String getListDetails() {
        return listDetails.get();
    }
    public void setListDetails(String listDetails) {
           this.listDetails.set(listDetails);
       }
    public StringProperty listDetailsProperty(){
        return listDetails;
    }

}

最后这里是FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import org.controlsfx.control.*?>

<AnchorPane prefHeight="600.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simit.gui.launch.view.LocalLaunchController">
   <children>
      <SplitPane dividerPositions="0.5" layoutX="120.0" layoutY="41.0" orientation="VERTICAL" prefHeight="600.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
            <ImageView fitWidth="600.0" pickOnBounds="true" preserveRatio="true" smooth="false">
               <image>
                  <Image url="@../../resources/SIMITBlastLogo.png" />
               </image>
               <cursor>
                  <Cursor fx:constant="DEFAULT" />
               </cursor>
            </ImageView>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <GridPane layoutX="50.0" layoutY="58.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columnConstraints>
                      <ColumnConstraints fillWidth="false" hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" />
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints fillWidth="false" hgrow="SOMETIMES" minWidth="10.0" prefWidth="0.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Input Deck">
                           <GridPane.margin>
                              <Insets left="10.0" />
                           </GridPane.margin></Label>
                        <Label text="Processors" GridPane.rowIndex="1">
                           <GridPane.margin>
                              <Insets left="10.0" />
                           </GridPane.margin></Label>
                        <TextField fx:id="procs" text="1" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <TextField fx:id="path" editable="false" GridPane.columnIndex="1" />
                        <Button alignment="CENTER_RIGHT" mnemonicParsing="false" onAction="#handleSelect" text="Select" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <GridPane.margin>
                              <Insets />
                           </GridPane.margin>
                        </Button>
                        <Label text="Mem/Proc" GridPane.rowIndex="2">
                           <GridPane.margin>
                              <Insets left="10.0" />
                           </GridPane.margin></Label>
                        <Label text="GB" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                        <TextField fx:id="mem" promptText="1" text="1" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                     </children>
                  </GridPane>
                  <ButtonBar layoutX="129.0" layoutY="248.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0">
                    <buttons>
                      <Button defaultButton="true" mnemonicParsing="false" onAction="#handleRun" text="Run Simulation" />
                    </buttons>
                  </ButtonBar>
                  <CheckListView fx:id="listItems" layoutX="200.0" layoutY="117.0" prefHeight="200.0" prefWidth="200.0" />
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

以下是我在运行程序时看到的内容(减去顶部的图像,我已经将其删除了一点匿名):

Image of the program when ran.

我需要看到的是在中间填充白色方块的列表项。

任何人都可以给予我任何帮助将不胜感激。

0 个答案:

没有答案