JavaFX:添加新用户时如何添加其他选项卡(包含textarea)?

时间:2016-11-15 20:13:55

标签: javafx tabs

在这个简单的JavaFX应用程序中,当添加一个新用户时,文本会显示"新用户添加"打印到第一个选项卡中的文本区域。如何添加附加选项卡和文本"新用户添加"每次添加新用户时都会在其中打印一个文本区域?

非常感谢任何帮助。

查看> PersonOverview.fxml

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

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

<HBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.makery.address.PersonOverviewController">
   <children>
      <TableView fx:id="personTable" prefHeight="298.0" prefWidth="246.0">
        <columns>
          <TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="First Name" />
          <TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Last Name" />
        </columns>
      </TableView>
      <VBox prefHeight="298.0" prefWidth="271.0">
         <children>
          <AnchorPane prefHeight="284.0" prefWidth="227.0">
               <children>
                  <Label layoutX="9.0" layoutY="4.0" prefHeight="19.0" prefWidth="96.0" text="Person Details" />
                  <GridPane layoutX="108.0" layoutY="121.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0">
                    <columnConstraints>
                      <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 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>
                        <Label text="First Name" />
                        <Label text="Last Name" GridPane.rowIndex="1" />
                        <Label text="Street" GridPane.rowIndex="2" />
                        <Label text="City" GridPane.rowIndex="3" />
                        <Label text="Postal Code" GridPane.rowIndex="4" />
                        <Label text="Birthday" GridPane.rowIndex="5" />
                        <Label fx:id="firstNameLabel" text="Label" GridPane.columnIndex="1" />
                        <Label fx:id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <Label fx:id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                        <Label fx:id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                        <Label fx:id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                        <Label fx:id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
                     </children>
                  </GridPane>
                  <ButtonBar buttonMinWidth="50.0" layoutX="14.0" layoutY="244.0" prefHeight="40.0" prefWidth="200.0">
                    <buttons>
                      <Button mnemonicParsing="false" onAction="#handleNewPerson" text="New" />
                        <Button mnemonicParsing="false" onAction="#handleEditPerson" text="Edit" />
                        <Button mnemonicParsing="false" onAction="#handleDeletePerson" text="Delete" />
                    </buttons>
                  </ButtonBar>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </AnchorPane>
         </children>
      </VBox>
      <TabPane prefHeight="296.0" prefWidth="337.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab>
               <content>
                  <TextArea fx:id="textArea" prefHeight="264.0" prefWidth="302.0" />
               </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</HBox>

查看&gt; PersonOverviewController

package ch.makery.address;

import ch.makery.address.util.DateUtil;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import ch.makery.address.MainApp;
import ch.makery.address.model.Person;

public class PersonOverviewController {
    @FXML
    private TableView<Person> personTable;
    @FXML
    private TableColumn<Person, String> firstNameColumn;
    @FXML
    private TableColumn<Person, String> lastNameColumn;

    @FXML
    private Label firstNameLabel;
    @FXML
    private Label lastNameLabel;
    @FXML
    private Label streetLabel;
    @FXML
    private Label postalCodeLabel;
    @FXML
    private Label cityLabel;
    @FXML
    private Label birthdayLabel;
    @FXML
    private TextArea textArea;

    // Reference to the main application.
    private MainApp mainApp;

    /**
     * The constructor.
     * The constructor is called before the initialize() method.
     */
    public PersonOverviewController() {
    }

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        // Initialize the person table with the two columns.
        firstNameColumn.setCellValueFactory(
                cellData -> cellData.getValue().firstNameProperty());
        lastNameColumn.setCellValueFactory(
                cellData -> cellData.getValue().lastNameProperty());

        // Clear person details.
        showPersonDetails(null);

        // Listen for selection changes and show the person details when changed.
        personTable.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> showPersonDetails(newValue));
    }

    /**
     * Is called by the main application to give a reference back to itself.
     *
     * @param mainApp
     */
    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;

        // Add observable list data to the table
        personTable.setItems(mainApp.getPersonData());
    }

    /**
     * Fills all text fields to show details about the person.
     * If the specified person is null, all text fields are cleared.
     *
     * @param person the person or null
     */
    private void showPersonDetails(Person person) {
        if (person != null) {
            // Fill the labels with info from the person object.
            firstNameLabel.setText(person.getFirstName());
            lastNameLabel.setText(person.getLastName());
            streetLabel.setText(person.getStreet());
            postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
            cityLabel.setText(person.getCity());

            birthdayLabel.setText(DateUtil.format(person.getBirthday()));
            // birthdayLabel.setText(...);
        } else {
            // Person is null, remove all the text.
            firstNameLabel.setText("");
            lastNameLabel.setText("");
            streetLabel.setText("");
            postalCodeLabel.setText("");
            cityLabel.setText("");
            birthdayLabel.setText("");
        }
    }


    public void print(String message) {

        textArea.appendText(message);
    }

    /**
     * Called when the user clicks on the delete button.
     */
    @FXML
    private void handleDeletePerson() {
        int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
        if (selectedIndex >= 0) {
            personTable.getItems().remove(selectedIndex);
        } else {
            // Nothing selected.
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.initOwner(mainApp.getPrimaryStage());
            alert.setTitle("No Selection");
            alert.setHeaderText("No Person Selected");
            alert.setContentText("Please select a person in the table.");

            alert.showAndWait();
        }
    }

    /**
     * Called when the user clicks the new button. Opens a dialog to edit
     * details for a new person.
     */
    @FXML
    private void handleNewPerson() {
        Person tempPerson = new Person();
        boolean okClicked = mainApp.showPersonEditDialog(tempPerson);
        if (okClicked) {
            mainApp.getPersonData().add(tempPerson);
            print("New User added");
        }
    }

    /**
     * Called when the user clicks the edit button. Opens a dialog to edit
     * details for the selected person.
     */
    @FXML
    private void handleEditPerson() {
        Person selectedPerson = personTable.getSelectionModel().getSelectedItem();
        if (selectedPerson != null) {
            boolean okClicked = mainApp.showPersonEditDialog(selectedPerson);
            if (okClicked) {
                showPersonDetails(selectedPerson);
            }

        } else {
            // Nothing selected.
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.initOwner(mainApp.getPrimaryStage());
            alert.setTitle("No Selection");
            alert.setHeaderText("No Person Selected");
            alert.setContentText("Please select a person in the table.");

            alert.showAndWait();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在你的handleNewPerson方法中,您必须添加以下内容:

Tab tab = new Tab();
tab.setText("TabText");
TextArea ta = new TextArea("New User added");
tab.setContent(ta);
tabPan.getTabs().add(tab);

添加添加:

@FXML
private TabPane tabPan;

并在fxml中添加

fx:id="tabPan"

到TabPane

现在我无法测试你的程序,因为你的程序需要很多其他类。但你也可以看到类似her的一个很好的例子。

相关问题