2016-11-15 77 views
0

在這個簡單的JavaFX應用程序中,當添加一個新用戶時,會在第一個選項卡的文本區域中打印顯示「添加新用戶」的文本。每次添加新用戶時,如何添加附加選項卡並在其中的文本區域中打印「添加新用戶」文本?JavaFX:添加新用戶時如何添加另一個選項卡(包含textarea)?

任何幫助將不勝感激。

查看> 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> 

查看> 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

在你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的很好的例子。

+0

有沒有一種方法來命名其中包含TextArea的添加Tab或命名TextArea,然後使用該名稱從線程訪問該TextArea? – 65535

+1

你總是可以通過變量'ta'訪問文本區域,一個想法是,如果你想訪問它,後者是將其添加到私有全局ArrayList或類似的東西。 @ 65535 – flaimme

相關問題