JavaFX:从ChoiceBox检索值时,我无法检测到null

时间:2018-08-09 00:30:59

标签: java javafx fxml

我有一个简单的功能,该功能应该遍历网格窗格中的所有文本字段,选择框和复选框。我面临的主要问题是,当我从选择框中检索一个空白值时,我得到的是空值,而我正试图通过 if(str!= null &&!str。 isEmpty())。但是我仍然得到 null 输出到屏幕上。我试图弄清楚为什么仍将其打印到控制台。 我从选择框中检索值错误吗?还是我做的 if陈述错误?

更新:我要发布控制器代码,fxml代码和输出代码。我不想显示所有代码,因为我知道对于许多人来说,它将不胜枚举。本质上,我试图创建一个可以创建SQL表结构并将该表结构输出为字符串的简单应用程序。应用程序上有两个按钮,一个用于生成另一列,该列将具有用于列名的文本字段,一个用于选择其值类型的选择框,另一个用于确定该列是否将成为索引或字段的选择框。一个主键值,以及一个确定该列是否将自动递增的复选框。第二个按钮是将值转换为字符串。

CreateTable.fxml

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="489.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.CreateTableController">
   <children>
      <Label layoutX="216.0" layoutY="20.0" text="Create Table">
         <font>
            <Font size="36.0" />
         </font>
      </Label>
      <Label layoutX="157.0" layoutY="90.0" text="Table :">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <TextField fx:id="tb_name" layoutX="226.0" layoutY="91.0" />
      <ScrollPane layoutX="61.0" layoutY="167.0" prefHeight="200.0" prefWidth="479.0">
         <content>
            <GridPane fx:id="fx_grid" hgap="10.0" maxHeight="1.7976931348623157E308" minHeight="100.0" prefWidth="477.0" vgap="10.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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>
               <children>
                  <TextField fx:id="fx_text" />
                  <ChoiceBox fx:id="fx_type" prefWidth="150.0" GridPane.columnIndex="1" />
                  <ChoiceBox fx:id="fx_index" prefWidth="150.0" GridPane.columnIndex="2" />
                  <CheckBox mnemonicParsing="false" GridPane.columnIndex="3" GridPane.halignment="CENTER" />
               </children>
               <padding>
                  <Insets left="10.0" right="10.0" />
               </padding>
            </GridPane>
         </content>
      </ScrollPane>
      <Button layoutX="275.0" layoutY="420.0" mnemonicParsing="false" onAction="#onSubmit" text="Create" />
      <Label layoutX="78.0" layoutY="141.0" text="Name" />
      <Label layoutX="183.0" layoutY="141.0" text="Type" />
      <Label layoutX="312.0" layoutY="141.0" text="Index" />
      <Label layoutX="456.0" layoutY="141.0" text="AI" />
      <Button layoutX="363.0" layoutY="420.0" mnemonicParsing="false" onAction="#getInfo" text="Button" />
   </children>
</AnchorPane>

CreateTableController.java

package controllers;

import models.SQL;
import choices.TableType;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

public class CreateTableController implements Initializable {

    private static List<TableType> typeList = new ArrayList();
    private static List<TableType> indexList = new ArrayList();
    private static int row = 1;

    @FXML
    private ChoiceBox<TableType> fx_type;

    @FXML
    private TextField tb_name;

    @FXML
    private TextField fx_text;

    @FXML
    private ChoiceBox<TableType> fx_index;

    @FXML
    private GridPane fx_grid;

    public void log(String txt) {
        System.out.println(txt);
    }

    private int getInt(Node node) {
        String id = node.getId();
        int start = 1 + id.indexOf("-");
        int end = id.length();
        int value = Integer.valueOf((String) id.subSequence(start, end));

        return value;
    }

    private String compileTableStructure(List<String> text_array , List<String> type_array, List<String> index_array, List<String> auto_array, String indexes){

        String table = "create table if not exists " + tb_name.getText() + " ( \n", text, index = "", type = "",
                auto = "", lastList;

        lastList = getIndexList(indexes);

        for (int x = 0; x < text_array.size(); x++) {

            text = text_array.get(x);
            if (x < type_array.size())
                type = type_array.get(x);
            if (x < index_array.size())
                index = index_array.get(x);
            if (x < auto_array.size())
                auto = auto_array.get(x);

           if(x == text_array.size()-1  && lastList.length() <= 0)
               table += text + " " + type + " " + index + " " + auto ;
           else
               table += text + " " + type + " " + index + " " + auto + ",\n";


            // System.out.println("column:"+x+" "+column);
        }

        if(lastList.length() > 0){

            table += lastList+" \n )";
        }else{

            //table = getIndexList(table);
            table += " \n )";
        }

        return table;
    }


    private String getIndexList(String list) {
        String value = "";
        if (list.length() > 0) {
            int lastNum = list.length() - 1;
            value = (String) list.subSequence(0, lastNum);
        }

        return value;
    }

    @FXML
    void getInfo(ActionEvent event) {

        Set<Node> nodes = fx_grid.lookupAll(".text-field");

        List<String> text_array = new ArrayList();
        List<String> type_array = new ArrayList();
        List<String> index_array = new ArrayList();
        List<String> auto_array = new ArrayList();
        String str = "", indexes = "";

        for (Node node : fx_grid.getChildren()) {
            String s = node.getId();

            if (node instanceof TextField) {
                str = ((TextField) node).getText();
                text_array.add(str);
                // String v =
                // "#text-"+String.valueOf(fx_grid.getRowIndex(node));
                // TextField c = (TextField)fx_grid.lookup(v);
                // System.out.println(c.getText());

            }

            if (node instanceof ChoiceBox && s.contains("type")) {
                str = "" + ((ChoiceBox) node).getValue();
                type_array.add(str);
            }

            if (node instanceof ChoiceBox && s.contains("index")) {
                str = "" + ((ChoiceBox) node).getValue();

                // String rowNum = String.valueOf(fx_grid.getRowIndex(node));
                // System.out.println(fx_grid.getRowIndex(node));
                // rowNum = (rowNum == null)? rowNum:"0";
                // log(str);
                if (str.equals("index")) {
                    String id = "#text-" + getInt(node);
                    TextField txt = (TextField) fx_grid.lookup(id);
                    indexes += "index(" + txt.getText() + "),";

                }else if (str != null && !str.isEmpty()){
                    str = String.valueOf(str);
                    str = str.trim();
                    log(str);
                    if(str == "null")
                        log("this is a null value");

                        index_array.add(str);
                }

                else{
                    index_array.add("not null");
                }



                // log(node.getId());

                // ChoiceBox cc = (ChoiceBox)fx_grid.lookup("#"+node.getId());
                // log(cc.getValue().toString());
            }

            if (node instanceof CheckBox) {
                str = (((CheckBox) node).isSelected() == true) ? "autoincrement" : "";
                auto_array.add(str);
            }
        }


        String table = compileTableStructure(text_array, type_array, index_array, auto_array, indexes);
        log(table);
        //SQL.createTable(table);

    }

    @FXML
    void onSubmit(ActionEvent event) {

        TextField column = new TextField("column-" + row);
        column.setId("text-" + row);

        ChoiceBox typeBox = new ChoiceBox();
        ChoiceBox indexBox = new ChoiceBox();

        typeBox.setId("type-" + row);
        typeBox.setMaxWidth(200);
        indexBox.setId("index-" + row);
        indexBox.setMaxWidth(200);

        typeBox.getItems().addAll(typeList);
        indexBox.getItems().addAll(indexList);

        CheckBox ai = new CheckBox();

        // fx_grid.setHgap(10);

        fx_grid.add(column, 0, row);
        fx_grid.add(typeBox, 1, row);
        fx_grid.add(indexBox, 2, row);
        fx_grid.add(ai, 3, row);
        fx_grid.setHalignment(ai, HPos.CENTER);

        fx_grid.setVgap(10);
        // double height = fx_grid.getHeight()+25;
        // fx_grid.setPrefHeight(height);
        // System.out.println(height);

        row++;

    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

        typeList.add(new TableType("text"));
        typeList.add(new TableType("numeric"));
        typeList.add(new TableType("blob"));
        typeList.add(new TableType("integer"));
        typeList.add(new TableType("real"));
        typeList.add(new TableType("none"));

        indexList.add(new TableType("index"));
        indexList.add(new TableType("primary key"));

        fx_type.getItems().addAll(typeList);
        fx_index.getItems().addAll(indexList);

        fx_type.setId("type-0");
        fx_index.setId("index-0");
        fx_text.setId("text-0");

    }

}

输出

create table if not exists  ( 
column text primary key ,
column-1 text null ,
column-2 numeric null ,
index(column-2) 
 )

1 个答案:

答案 0 :(得分:0)

您的代码

str = "" + ((ChoiceBox) node).getValue();

在str中解析为以下结果

"" + "null"

这既不是str == null也不是str.isEmpty。 将上面的行替换为:

Result r = ((ChoiceBox) node).getValue();
if(r != null)
    str = r;
else str = null;

当然,您必须用ChoiceBox中的任何内容替换Result。如果它也是一个字符串,您还可以编写:

str = ((ChoiceBox) node).getValue();