【问题标题】:How to add data to tableView even if the textfield is empty?即使文本字段为空,如何向 tableView 添加数据?
【发布时间】:2017-10-14 15:58:50
【问题描述】:

我正在尝试制作一个程序,它从文本字段中获取数据,将其添加到 tableview,然后添加到 DB。问题是,我还需要一个表格视图来接受一个空的文本字段值。

这就是我向 tableview 添加值的方式:

 public void pievButtonClicked() {
    int kods = Integer.parseInt(kodsT.getText());
    String nosaukums = nosaukumsT.getText();
    int inventars = Integer.parseInt(iegadesT.getText());
    double uzskaite = Double.parseDouble(uzskaitesT.getText());
    double iegade = Double.parseDouble(iegadesT.getText());
    data.addAll(new Interjers(kods, nosaukums, inventars, uzskaite, iegade));
}

也许我需要更改“Interijers”类或者我需要更改setCellValueFactory 是某种方式。我真的不知道。

【问题讨论】:

  • 你的堆栈跟踪是什么?

标签: javafx tableview textfield


【解决方案1】:

我不知道到底想要什么!但我为你做了这个例子,它似乎解释了你的需要

模型类:

package javafxapplication4;


public class Model {

    String name;
    String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public Model(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public void setAge(String age) {
        this.age = age;
    }

}

Fxml:

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

<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication4.HomeController">
    <stylesheets>
        <URL value="@home.css" />
    </stylesheets>
   <children>
      <TextField fx:id="nameField" layoutY="30.0" />
      <TextField fx:id="ageField" layoutX="226.0" layoutY="30.0" />
      <Button layoutX="451.0" layoutY="30.0" mnemonicParsing="false" onAction="#addLine" text="Button" />
      <TableView fx:id="view" layoutX="52.0" layoutY="100.0" prefHeight="200.0" prefWidth="506.0">
        <columns>
          <TableColumn fx:id="nameCo" prefWidth="75.0" text="Name" />
          <TableColumn fx:id="ageCo" prefWidth="75.0" text="Age" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

控制器类:

package javafxapplication4;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * FXML Controller class
 *
 * @author Ala_Eddine
 */
public class HomeController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    public TableView<Model> view;

    @FXML
    public TableColumn<Model, String> nameCo;

    @FXML
    public TableColumn<Model, String> ageCo;

    @FXML
    public TextField nameField;

    @FXML
    public TextField ageField;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    nameCo.setCellValueFactory(new PropertyValueFactory<>("name"));
    ageCo.setCellValueFactory(new PropertyValueFactory<>("age"));
    }

    @FXML
    public void addLine() {
        String name = nameField.getText();
        String age = ageField.getText();
        Model model = new Model(name, age);
        view.getItems().add(model);

    }

}

主类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication4;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Ala_Eddine
 */
public class JavaFXApplication4 extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

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

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

结果

【讨论】:

  • 问题是当我试图将textfield 留空时,我总是得到java.lang.NumberFormatException: For input string: ""。我可以抓住NumberFormatException,如果发生这种情况,只需将值设置为 null 或其他东西,但我稍后也需要将它添加到数据库中,所以我认为这个解决方案不会起作用。这是我的代码的链接:mainDataAccessorProduct。这里是程序 UI 的链接,看看我现在在哪里:image.
  • 因为你试图解析空值,你需要做一个空测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多