在JavaFX中,如何在TabTane内的TableView中添加TextField?

时间:2019-06-05 11:26:50

标签: javafx

到目前为止,我已经使用Scene Builder构建了这个GUI,我想在我制作的每个TabPane中向TableView添加空行(TextField)。任何帮助,将不胜感激。或者,如果您可以提出一种代替JavaFX的简便方法来完成此任务。

P.S。我是新手。 预先感谢。

GUI so far GUI so far (1)

到目前为止我的代码

Main.java

package sample;

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.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.Date;

public class Main extends Application {

    Stage window;
    Button button;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        window = primaryStage;
        window.setTitle("LOPA Software");

        window.setScene(new Scene(root, 800, 500));
        window.show();
    }
}

Controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ContextMenuEvent;

import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    TreeView<String> treeView;

    @FXML
    TableView tableView;

    Image icon = new Image(
            getClass().getResourceAsStream("/img/icon.png"));
    private MouseEvent mouseEvent;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        TreeItem<String> root = new TreeItem<>("Project", new ImageView(icon));
        root.setExpanded(true);

        TreeItem<String> compA = new TreeItem<>("Component A", new ImageView(icon));
        TreeItem<String> compB = new TreeItem<>("Component B", new ImageView(icon));
        TreeItem<String> compC = new TreeItem<>("Component C", new ImageView(icon));
        root.getChildren().addAll(compA, compB, compC);
        compA.setExpanded(true);

        TreeItem<String> compA1 = new TreeItem<>("High", new ImageView(icon));
        TreeItem<String> compA2 = new TreeItem<>("Low", new ImageView(icon));
        compA.getChildren().addAll(compA1, compA2);

        treeView.setRoot(root);
    }


    public void mouseClick(javafx.scene.input.MouseEvent mouseEvent) {
        if (mouseEvent.getClickCount() == 2) {
            TreeItem<String> item = treeView.getSelectionModel().getSelectedItem();
            System.out.println(item.getValue());
        }
    }
}

sample.fxml

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

<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                        <MenuItem mnemonicParsing="false" text="Open" />
                        <MenuItem mnemonicParsing="false" text="Save" />
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Copy" />
                        <MenuItem mnemonicParsing="false" text="Cut" />
                        <MenuItem mnemonicParsing="false" text="Paste" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <ToolBar>
              <items>
                <Button mnemonicParsing="false" text="Toolbar" />
              </items>
            </ToolBar>
         </children>
      </VBox>
   </top>
   <left>
      <TreeView fx:id="treeView" editable="true" onMouseClicked="#mouseClick" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
   <bottom>
      <HBox BorderPane.alignment="CENTER">
         <children>
            <Label text="Label" />
         </children>
      </HBox>
   </bottom>
   <center>
      <TabPane>
         <tabs>
            <Tab text="PHA">
               <content>
                  <TableView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                    <columns>
                      <TableColumn prefWidth="75.0" text="Cause" />
                      <TableColumn prefWidth="75.0" text="Consequence" />
                        <TableColumn prefWidth="75.0" text="Severity" />
                        <TableColumn prefWidth="75.0" text="Likelihood" />
                        <TableColumn prefWidth="75.0" text="Risk" />
                        <TableColumn prefWidth="75.0" text="Safeguard" />
                        <TableColumn prefWidth="75.0" text="Recommendation" />
                        <TableColumn prefWidth="75.0" text="LOPA" />
                    </columns>
                  </TableView>
               </content>
            </Tab>
            <Tab text="LOPA">
               <content>
                  <TableView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                     <columns>
                        <TableColumn prefWidth="75.0" text="Scenario" />
                        <TableColumn prefWidth="75.0" text="Consequence">
                           <columns>
                              <TableColumn prefWidth="75.0" text="Description" />
                              <TableColumn prefWidth="75.0" text="Severity" />
                           </columns>
                        </TableColumn>
                        <TableColumn prefWidth="75.0" text="Acceptable Risk Tolerance" />
                        <TableColumn prefWidth="75.0" text="Initiating Events">
                           <columns>
                              <TableColumn prefWidth="75.0" text="Description" />
                              <TableColumn prefWidth="75.0" text="Freq. (per year)" />
                           </columns>
                        </TableColumn>
                        <TableColumn prefWidth="75.0" text="Independent Protection Layers (IPLs)" />
                        <TableColumn prefWidth="75.0" text="Mitigated Cosequence" />
                        <TableColumn prefWidth="75.0" text="Recommendation" />
                     </columns>
                  </TableView>
               </content>
            </Tab>
         </tabs>
      </TabPane>
   </center>
</BorderPane>

0 个答案:

没有答案