他们是否可以使用按钮添加"记录"到表视图?我在网上看到的大多数例子都是从文件读取数据并将硬编码数据读入代码。我正在制作一个基于记录的程序,并且无法想出一个可靠的方法来做到这一点。
有没有办法创建一个向列添加数据的数组?所以当我添加一条记录时,我将它添加到一个数组然后刷新列?
我已经看到了这种做法:
final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob", "Smith"), new Person("Isabella", "Johnson"), new Person("Ethan", "Williams"),
new Person("Emma", "Jones"), new Person("Michael", "Brown"));
有人可以更好地解释这个吗?每次用户点击&#34;保存按钮&#34;时,如何实时添加记录?它在表格中添加了一条记录。
控制器:
package javafxapplication1;
import javafxapplication1.JavaFXApplication1;
import java.net.URL;
import static java.util.Collections.list;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javax.lang.model.element.Element;
/**
*
* @author KJ4CC
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private TextField dateText;
@FXML
private TextField time;
@FXML
private ComboBox<String> band;
@FXML
private ComboBox<String> mode;
@FXML
private Button save;
@FXML
private Button Clear;
@FXML
private TableView logTable;
@FXML
private TableColumn dateTime,user,callSign,logBand,logMode,logLocation,logSent,logRx,logComment;
public void saveButtonClick(){
logTable.getItems().add(dateText.getText());
}
public void clearButtonClick(){
dateText.setText("");
time.setText("");
}
public void setTimeDate(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
dateText.setText(javaFXApp.getDate());
}
public void setTime(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
time.setText(javaFXApp.getTime());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> bands =
FXCollections.observableArrayList(
"70CM",
"2M",
"6M",
"10M",
"11M",
"12M",
"15M",
"20M",
"30M",
"40m",
"60M",
"80M",
"160M"
);
ObservableList<String> modes =
FXCollections.observableArrayList(
"Phone",
"SSB",
"CW",
"PSK31",
"Dgital Voice",
"HellSchriber",
"RTTY",
"SSTV",
"Satelight",
"Packet",
"Frequency Shift",
"FM",
"AM"
);
band.setItems(bands);
band.setValue("70CM");
mode.setItems(modes);
mode.setValue("Phone");
}
}
主:
package javafxapplication1;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafxapplication1.FXMLDocumentController;
/**
*
* @author KJ4CC
*/
public class JavaFXApplication1 extends Application {
private ObservableList<String> data = FXCollections.observableArrayList();
private FXMLDocumentController initScene;
private DateFormat dtf;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.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);
}
public String getDate(){
dtf = new SimpleDateFormat("dd/MM/yy");
Date dateobj = new Date();
return dtf.format(dateobj);
}
public String getTime(){
dtf = new SimpleDateFormat("HH:mm;ss");
Date dateobj = new Date();
return dtf.format(dateobj);
}
public void addTable(TableView table,TextField dateText){
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="412.0" prefWidth="683.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<top>
<VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TableView fx:id="logTable" prefHeight="210.0" prefWidth="599.0">
<columns>
<TableColumn fx:id="dateTIme" prefWidth="75.0" text="Date/Time" />
<TableColumn fx:id="user" prefWidth="72.0" text="User" />
<TableColumn fx:id="callSign" prefWidth="73.0" text="Call" />
<TableColumn fx:id="logBand" prefWidth="64.0" text="Band" />
<TableColumn fx:id="logMode" prefWidth="63.0" text="Mode" />
<TableColumn fx:id="logLocation" prefWidth="112.0" text="Location" />
<TableColumn fx:id="logSent" minWidth="0.0" prefWidth="55.0" text="Sent " />
<TableColumn fx:id="logRx" minWidth="0.0" prefWidth="63.0" text="Recived" />
<TableColumn fx:id="logComment" prefWidth="103.0" text="Comment" />
</columns>
<VBox.margin>
<Insets left="2.0" right="2.0" />
</VBox.margin>
</TableView>
</children>
</VBox>
</top>
<bottom>
<VBox prefHeight="188.0" prefWidth="673.0" BorderPane.alignment="CENTER">
<children>
<HBox prefHeight="22.0" prefWidth="625.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625">
<HBox.margin>
<Insets left="5.0" right="20.0" />
</HBox.margin>
</Text>
<Label onMouseClicked="#setTimeDate" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="77.40625" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="96.443359375" />
<Label />
<Text layoutX="325.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Mode:" wrappingWidth="99.443359375" />
<Text layoutX="524.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Power: " wrappingWidth="55.724609375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" />
</children>
</HBox>
<HBox prefHeight="35.0" prefWidth="625.0">
<children>
<TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
<TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0">
<padding>
<Insets left="5.0" />
</padding>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</TextField>
<TextField fx:id="call" layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<ComboBox fx:id="band" prefHeight="25.0" prefWidth="87.0">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</ComboBox>
<ComboBox fx:id="mode" prefHeight="25.0" prefWidth="93.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</ComboBox>
<TextField fx:id="power" layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="47.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<TextField fx:id="location" layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" />
</children>
</HBox>
<HBox prefHeight="30.0" prefWidth="580.0">
<children>
<Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</Text>
<Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" />
</children>
</HBox>
<HBox prefHeight="18.0" prefWidth="737.0">
<children>
<Label prefHeight="17.0" prefWidth="48.0" text="Sent">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<Label layoutX="10.0" layoutY="10.0" prefHeight="17.0" prefWidth="46.0" text="Recived ">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<Label layoutX="10.0" layoutY="10.0" prefHeight="17.0" prefWidth="66.0" text="Comment" />
</children>
</HBox>
<HBox prefHeight="42.0" prefWidth="737.0">
<children>
<TextField id="dateText" fx:id="sent" prefHeight="25.0" prefWidth="43.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<TextField fx:id="rx" prefHeight="25.0" prefWidth="48.0">
<HBox.margin>
<Insets right="15.0" />
</HBox.margin>
</TextField>
<TextField fx:id="comment" layoutX="63.0" layoutY="10.0" prefHeight="25.0" prefWidth="557.0" />
</children>
</HBox>
<HBox prefHeight="29.0" prefWidth="673.0">
<children>
<Region prefHeight="29.0" prefWidth="504.0" />
<Button fx:id="save" mnemonicParsing="false" onAction="#saveButtonClick" prefHeight="25.0" prefWidth="81.0" text="Save">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
<Button fx:id="clear" mnemonicParsing="false" onAction="#clearButtonClick" prefHeight="25.0" prefWidth="78.0" text="Clear" />
</children>
</HBox>
</children>
<BorderPane.margin>
<Insets left="5.0" right="5.0" />
</BorderPane.margin>
</VBox>
</bottom>
</BorderPane>
我在控制器类中添加数据。在saveButtonClick下