【问题标题】:show TableView data into another Window contains TextField in JavaFx将 TableView 数据显示到另一个窗口中包含 JavaFx 中的 TextField
【发布时间】:2018-06-02 03:24:31
【问题描述】:

我制作了两个 Fxml 文件,一个包含 TextField,另一个包含 TableView。它有自己的 Controller 类。我想在执行鼠标单击动作事件时将数据从 TableView 显示到 TextField。但是我们没有得到结果,它显示了很多错误,例如:

Jun 02, 2018 8:33:36 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
Jun 02, 2018 8:33:44 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not find stylesheet: file:/C:/Users/MdAzaz/IdeaProjects/JavaFxProject3/out/production/Stylesheet/style.css
Jun 02, 2018 8:33:46 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
Jun 02, 2018 8:33:50 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
java.lang.NullPointerException
    at Company.CompanyTableController.Clicked(CompanyTableController.java:123)
    at Company.CompanyTableController.access$100(CompanyTableController.java:35)
    at Company.CompanyTableController$2.handle(CompanyTableController.java:108)
    at Company.CompanyTableController$2.handle(CompanyTableController.java:105)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0

第一个 Fxml 文件名 CompanyLayout.fxml 包含 TextFields,并且它有其 Controller 类,如 CompanyController.java。当我单击该按钮时,它具有按钮名称 Find,其中包含 TableView 的 CompanyTable.fxml 已打开。当我单击 TableView 的特定行时,该行的数据将显示在包含文本字段的 CompanyLayout.fxml 上。

CompanyController.java 的查找按钮代码是这样的:

 btnFind.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    Stage primaryStage = new Stage();
                    FXMLLoader loader = new FXMLLoader();
                    Parent root = loader.load(getClass().getClassLoader().getResource("Company\\CompanyTable.fxml"));
                    Scene scene=new Scene(root);
                    scene.getStylesheets().add(getClass().getClassLoader().getResource("Stylesheet\\style.css").toExternalForm());
                    primaryStage.setTitle("Company Table");
                    primaryStage.setScene(scene);
                    primaryStage.show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

CompanyTableController.java 的 TableView 代码如下:

CompanyTable.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Clicked();
            }
        });

 private void Clicked()
    {
        try
        {
            CompanyData categoryData=CompanyTable.getItems().get(CompanyTable.getSelectionModel().getSelectedIndex());
           CategoryID.setText(categoryData.getCompanyID());
            CategoryName.setText(categoryData.getCompanyName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

我希望你能理解这段代码,所以请帮助我!

【问题讨论】:

    标签: javafx tableview


    【解决方案1】:

    我创建了一个示例应用来演示您的问题。

    关键是将数据从一个Controller 传递到另一个。你可以找到信息here

    这里是关键代码:

    @FXML
    private void handleButtonAction(ActionEvent event) {
            try
            {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("SearchPanel.fxml"));
                Parent root = loader.load();
                SearchPanelController searchPanelController = loader.getController();//Get access to the Controller
                Stage stage = new Stage();
                stage.setScene(new Scene(root));
                stage.showAndWait();//Wait for the Search Controller to close.
                Customer tempCustomer = searchPanelController.getCustomer();//Get the selected customer from the Search Controller. HAVE A LOOK AT THE SearchPanelController!
                //Set the selected customer to the TextFields
                tfFirstName.setText(tempCustomer.getFirstName());
                tfLastName.setText(tempCustomer.getLastName());
                tfEmail.setText(tempCustomer.getEmail());
            } catch (IOException e)
            {
                e.printStackTrace();
            }
    }
    

    完整的应用程序 - 主类:

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    /**
     *
     * @author sedri
     */
    public class JavaFXApplication3 extends Application {
    
        @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);
        }
    
    }
    

    客户类别:

    /**
     *
     * @author sedrick
     */
    public class Customer {
        private String firstName;
        private String lastName;
        private String email;
    
        public Customer(String firstName, String lastName, String email) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.email = email;
        }
    
    
        /**
         * @return the firstName
         */
        public String getFirstName() {
            return firstName;
        }
    
        /**
         * @param firstName the firstName to set
         */
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        /**
         * @return the lastName
         */
        public String getLastName() {
            return lastName;
        }
    
        /**
         * @param lastName the lastName to set
         */
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        /**
         * @return the email
         */
        public String getEmail() {
            return email;
        }
    
        /**
         * @param email the email to set
         */
        public void setEmail(String email) {
            this.email = email;
        }
    
    
    }
    

    FXMLDocumentController 类:

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.stage.Stage;
    
    /**
     *
     * @author sedri
     */
    public class FXMLDocumentController implements Initializable {    
    
        @FXML TextField tfFirstName, tfLastName, tfEmail;
    
        @FXML
        private void handleButtonAction(ActionEvent event) {
                try
                {
                    FXMLLoader loader = new FXMLLoader(getClass().getResource("SearchPanel.fxml"));
                    Parent root = loader.load();
                    SearchPanelController searchPanelController = loader.getController();
                    Stage stage = new Stage();
                    stage.setScene(new Scene(root));
                    stage.showAndWait();
                    Customer tempCustomer = searchPanelController.getCustomer();
                    tfFirstName.setText(tempCustomer.getFirstName());
                    tfLastName.setText(tempCustomer.getLastName());
                    tfEmail.setText(tempCustomer.getEmail());
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
    
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
    
    }
    

    FXMLDocument FXML:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.Menu?>
    <?import javafx.scene.control.MenuBar?>
    <?import javafx.scene.control.MenuItem?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.StackPane?>
    <?import javafx.scene.layout.VBox?>
    
    <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="484.0" prefWidth="667.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication3.FXMLDocumentController">
       <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>
          <StackPane prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
             <children>
                <VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity">
                   <children>
                      <HBox maxHeight="-Infinity" maxWidth="-Infinity">
                         <children>
                            <Label maxHeight="1.7976931348623157E308" text="First Name:" />
                            <TextField fx:id="tfFirstName" />
                         </children>
                      </HBox>
                      <HBox maxHeight="-Infinity" maxWidth="-Infinity">
                         <children>
                            <Label maxHeight="1.7976931348623157E308" maxWidth="-Infinity" prefWidth="60.0" text="Last Name:" />
                            <TextField fx:id="tfLastName" />
                         </children>
                      </HBox>
                      <HBox maxHeight="-Infinity" maxWidth="-Infinity">
                         <children>
                            <Label alignment="CENTER_RIGHT" maxHeight="1.7976931348623157E308" prefWidth="60.0" text="Email: " />
                            <TextField fx:id="tfEmail" />
                         </children>
                      </HBox>
                      <Button alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="Search" />
                   </children>
                </VBox>
             </children>
          </StackPane>
       </children>
    </VBox>
    

    SearchPanelController 类:

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    
    /**
     * FXML Controller class
     *
     * @author sedri
     */
    public class SearchPanelController implements Initializable {
    
        @FXML TableView<Customer> tvSearch;
        @FXML TableColumn tcFirstName, tcLastName, tcEmail;
    
        Customer selectedCustomer;
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
            tvSearch.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection)->{
                if(newSelection != null)
                {
                    selectedCustomer = newSelection;        
                    tvSearch.getScene().getWindow().hide();
                }
            });
            tcFirstName.setCellValueFactory(new PropertyValueFactory("firstName"));
            tcLastName.setCellValueFactory(new PropertyValueFactory("lastName"));
            tcEmail.setCellValueFactory(new PropertyValueFactory("email"));
    
            ObservableList<Customer> data =
            FXCollections.observableArrayList(
                new Customer("Jacob", "Smith", "jacob.smith@example.com"),
                new Customer("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Customer("Ethan", "Williams", "ethan.williams@example.com"),
                new Customer("Emma", "Jones", "emma.jones@example.com"),
                new Customer("Michael", "Brown", "michael.brown@example.com")
            );
            tvSearch.setItems(data);
    
    
        }    
    
        public Customer getCustomer()
        {
            return selectedCustomer;
        }
    }
    

    搜索面板 FXML:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.layout.StackPane?>
    
    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication3.SearchPanelController">
       <children>
          <TableView fx:id="tvSearch" prefHeight="200.0" prefWidth="200.0">
            <columns>
              <TableColumn fx:id="tcFirstName" prefWidth="153.0" text="First Name" />
              <TableColumn fx:id="tcLastName" prefWidth="182.0" text="Last Name" />
                <TableColumn fx:id="tcEmail" prefWidth="263.0" text="Email" />
            </columns>
          </TableView>
       </children>
    </StackPane>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多