我试图创建一个从textfield获取数据的程序,将其添加到tableview然后再添加到DB。问题是,我还需要一个tableview来接受一个空的文本字段值。
这就是我向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
是某种方式。我真的不知道。
答案 0 :(得分:0)
我不知道究竟想要什么!但我为你做了这个例子,似乎解释了你的需要
模特课:
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);
}
}