将ObservableList中的内容添加到TableView FXML文件

时间:2018-04-19 20:25:34

标签: java javafx netbeans fxml

我在向TableView添加内容时遇到了困难 这是代码。我想我需要用TableView-elemnt改变一些东西.. 我不知道我卡住了。如果有人能帮助我,我将不胜感激。

谢谢。

FXML:

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


<?import com.jfoenix.controls.JFXButton?> 
<?import com.jfoenix.controls.JFXComboBox?> 
<?import com.jfoenix.controls.JFXTextField?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.SplitPane?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="429.0"
    prefWidth="677.0" xmlns="http://javafx.com/javafx/8.0.141"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="registerapp.FXMLDocumentController">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16"
            minWidth="69" />
        <SplitPane dividerPositions="0.29797979797979796"
            layoutX="-1.0" prefHeight="429.0" prefWidth="745.0">
            <items>
                <AnchorPane fx:id="Anchorepane" minHeight="0.0"
                    minWidth="0.0" prefHeight="427.0" prefWidth="247.0">
                    <children>
                        <Label fx:id="lable" layoutX="14.0" layoutY="14.0"
                            prefHeight="24.0" prefWidth="103.0" text="Register Details"
                            textFill="#0080ff">
                            <font>
                                <Font name="System Bold" size="12.0" />
                            </font>
                        </Label>
                        <JFXTextField fx:id="firstName" layoutX="14.0"
                            layoutY="63.0" onAction="#firstName" promptText="First Name" />
                        <JFXTextField fx:id="lastName" layoutX="14.0"
                            layoutY="111.0" onAction="#lastName" promptText="Last Name" />
                        <JFXTextField fx:id="address" layoutX="14.0"
                            layoutY="162.0" onAction="#address" promptText="Address" />
                        <JFXComboBox fx:id="contractBox" layoutX="14.0"
                            layoutY="226.0" onAction="#contractBox" prefHeight="25.0"
                            prefWidth="135.0" promptText="Duration of Contract" />
                        <JFXComboBox fx:id="speedBox" layoutX="14.0"
                            layoutY="273.0" onAction="#speedBox" prefHeight="25.0"
                            prefWidth="135.0" promptText="Internet Speed" />
                        <JFXComboBox fx:id="flowBox" layoutX="14.0"
                            layoutY="323.0" onAction="#flowBox" prefHeight="25.0"
                            prefWidth="135.0" promptText="Internet Flow" />
                        <JFXButton fx:id="registerBtn" layoutX="117.0"
                            layoutY="388.0" onAction="#registerBtn" prefHeight="25.0"
                            prefWidth="69.0" style="-fx-background-color: #0080ff;"
                            text="Register" textFill="#fffbfb" />
                        <JFXButton fx:id="clearAll" layoutX="14.0"
                            layoutY="388.0" onAction="#clearAll"
                            style="-fx-background-color:
#0080ff;" text="Clear All"
                            textFill="#fffafa" />
                    </children>
                </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0"
                    prefHeight="427.0" prefWidth="495.0">
                    <children>
                        <TableView fx:id="tabela" prefHeight="429.0"
                            prefWidth="519.0">
                            <columns>
                                <TableColumn prefWidth="81.0" text="First Name" />
                                <TableColumn prefWidth="79.0" text="Last Name" />
                                <TableColumn prefWidth="123.0" text="Address" />
                                <TableColumn prefWidth="55.0" text="DC" />
                                <TableColumn prefWidth="101.0" text="Internet Speed" />
                                <TableColumn prefWidth="79.0" text="Internet Flow" />
                            </columns>

                        </TableView>
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
    </children>
</AnchorPane> 

FXMLDocumentController:

package application;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class FXMLDocumentController implements Initializable {

    @FXML
    private JFXTextField firstName;
    @FXML
    private JFXTextField lastName;
    @FXML
    private JFXTextField address;
    @FXML
    private JFXComboBox<String> contractBox;
    @FXML
    private JFXComboBox<String> speedBox;
    @FXML
    private JFXComboBox<String> flowBox;
    @FXML
    private JFXButton registerBtn;
    @FXML
    private JFXButton clearAll;

    @FXML
    TableView<Person> tabela = new TableView<>();

    ObservableList<Person> persons = FXCollections.<Person>observableArrayList();

    Person person;

    public FXMLDocumentController()// ***
    {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        person = new Person();

        firstName.textProperty().bindBidirectional(person.firstNameProperty());
        lastName.textProperty().bindBidirectional(person.lastNameProperty());
        address.textProperty().bindBidirectional(person.addressProperty());

        ObservableList<String> list = FXCollections.observableArrayList("", "One Year", "Two Year");
        ObservableList<String> list1 = FXCollections.observableArrayList("", "2Mbit", "5Mbit", "10Mbit", "20Mbit",
                "50Mbit", "100Mbit");
        ObservableList<String> list2 = FXCollections.observableArrayList("", "1GB", "5GB", "10GB", "100GB", "Flat");

        contractBox.setItems(list);
        speedBox.setItems(list1);
        flowBox.setItems(list2); // ***

        contractBox.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String selected = contractBox.getSelectionModel().getSelectedItem();
                person.contractBoxProperty().set(selected);
            }

        }); // ***

        speedBox.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String selected = speedBox.getSelectionModel().getSelectedItem();
                person.speedBoxProperty().set(selected);
            }

        }); // ***

        flowBox.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String selected = flowBox.getSelectionModel().getSelectedItem();
                person.flowBoxProperty().set(selected);
            }

        });

    }

    @FXML
    private void firstName(ActionEvent event) {
    }

    @FXML
    private void lastName(ActionEvent event) {
    }

    @FXML
    private void address(ActionEvent event) {
    }

    @FXML
    private void contractBox(ActionEvent event) {
    }

    @FXML
    private void speedBox(ActionEvent event) {
    }

    @FXML
    private void flowBox(ActionEvent event) {
    }

    @FXML
    private void registerBtn(ActionEvent event) {
        if (person.isValid()) {
            persons = tabela.getItems();

            persons.add(new Person(firstName.getText(), lastName.getText(), address.getText(), contractBox.getValue(),
                    speedBox.getValue(), flowBox.getValue()));// ***
            // ***
            tabela.setItems(persons);

        } else {
            StringBuilder errMsg = new StringBuilder();

            ArrayList<String> errList = person.errorsProperty().get();
            for (String errList1 : errList)
                errMsg.append(errList1);

            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Person can not register!");
            alert.setHeaderText(null);
            alert.setContentText(errMsg.toString());
            alert.showAndWait();
            errList.clear();
        }

    }

    @FXML
    private void clearAll(ActionEvent event) {

        person.firstNameProperty().set("");
        person.lastNameProperty().set("");
        person.addressProperty().set("");

        contractBox.getSelectionModel().clearSelection();
        flowBox.getSelectionModel().clearSelection();
        speedBox.getSelectionModel().clearSelection();
    }

}

PersonClass:

package registerapp;

import java.util.ArrayList; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty;

class Person {
    private final StringProperty firstName = new SimpleStringProperty(this, "firstName", "");
    private final StringProperty lastName = new SimpleStringProperty(this, "lastName", "");
    private final StringProperty address = new SimpleStringProperty(this, "addres", "");
    private final StringProperty contractBox = new SimpleStringProperty(this, "contractBox", "");
    private final StringProperty speedBox = new SimpleStringProperty(this, "speedBox", "");
    private final StringProperty flowBox = new SimpleStringProperty(this, "flowBox", "");

    public Person()
    {

    }

    public Person(String firstName)
    {   this.firstName.set(firstName);
    }

    public Person(String firstName, String lastName)
    {   this.firstName.set(firstName);  this.lastName.set(lastName);
    }

    public Person(String firstName, String lastName, String address, String contractBox, String speedBox, String flowBox)
    {   this.firstName.set(firstName);  this.lastName.set(lastName);    this.address.set(address);  this.contractBox.set(contractBox);  this.speedBox.set(speedBox);    this.flowBox.set(flowBox);
    }

    public String getFirstName()
    {   return firstName.get();
    }

    public void setFirstName(String firstName)
    {   this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty()
    {   return firstName;
    }

    public String getLastName()
    {   return lastName.get();
    }

    public void setLastName(String lastName)
    {   this.lastName.set(lastName);
    }

    public StringProperty lastNameProperty()
    {   return lastName;
    }

    public String getAddress()
    {   return address.get();
    }

    public void setAddress(String address)
    {   this.address.set(address);
    }

    public StringProperty addressProperty()
    {   return address;
    }

    public String getContract()
    {   return contractBox.get();
    }

    public void setContract(String contractBox)
    {   this.contractBox.set(contractBox);
    }

    public StringProperty contractBoxProperty()
    {   return contractBox;
    }

    public String getSpeed()
    {   return speedBox.get();
    }

    public void setSpeed(String speedBox)
    {   this.speedBox.set(speedBox);
    }

    public StringProperty speedBoxProperty()
    {   return speedBox;
    }

    public String getFlow()
    {   return flowBox.get();
    }

    public void setFlow(String flowBox)
    {   this.flowBox.set(flowBox);
    }

    public StringProperty flowBoxProperty()
    {   return flowBox;
    }

    private final ObjectProperty<ArrayList<String>> errorList = new SimpleObjectProperty<>(this, "errorList", new ArrayList<>());

    public ObjectProperty<ArrayList<String>> errorsProperty()
    {   return errorList;
    }

    public boolean isValid()
    {   boolean isValid = true;

    if(firstName.get() != null && firstName.get().equals(""))   {
        errorList.getValue().add("First name can't be empty!");
        isValid = false;    }   if(lastName.get().equals(""))   {
        errorList.getValue().add("Last name can't be empty!");
        isValid = false;    }   if(address.get().equals(""))    {
        errorList.getValue().add("Address can't be empty!");
        isValid = false;    }

    return isValid;
    }

}

如果您复制此代码,则需要将此文件添加到库:jfoenix-8.0.1

这是下载链接---- https://ufile.io/ineld

由于

0 个答案:

没有答案