0

I'm trying to use a searchbox in another stage by using fxml for the layout. It it showing absolutely correct,but it doesn't react to any events.It seems that the elements are not found.

What is wrong with my code?

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class SearchBox{


    //tabs

    @FXML
    private static TabPane frepl_tabPane=new TabPane();

    @FXML
    private static Tab frepl_tabReplace=new Tab();
    @FXML
    private static Tab frepl_tabSearch=new Tab();

    //search

    //search Controls

    @FXML
    private static TextField fr_searchInput=new Textfield();

    @FXML
    private static Button fr_find=new Button();


    private static Stage stage = new Stage();



    public static void display(String title) throws IOException {

        Parent root = FXMLLoader.load(SearchBox.class.getResource("find_replace.fxml"));

        Scene scene = new Scene(root);

        stage.setTitle(title);


        stage.setScene(scene);
        stage.showAndWait();

        frepl_tabPane.getSelectionModel().selectedItemProperty().addListener((obs,ov,nv)->{
            //no reaction here
            stage.setTitle(nv.getText());
            System.out.println("pane was tabbed"); 
        });

        fr_find.setOnMouseClicked(e ->{
            //no reaction here 
            System.out.println("search");
        });
    }

To open it i use (inside of the "initialize" function of another FXMLDocumentControler):

btn_search.setOnMouseClicked(e->{
        try {
            SearchBox.display("search");
        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }

    });

Here is the stack trace(when everything is non static e.g. the class,methods and fields). it appears,when i close the window.

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at SearchBox.display(SearchBox.java:133)
    at FXMLDocumentController.lambda$A_table_buttons$22(FXMLDocumentController.java:1309)
    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:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    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$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

FXML file:

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

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

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TabPane fx:id="frepl_tabPane" layoutX="-1.0" layoutY="2.0" prefHeight="400.0" prefWidth="508.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab fx:id="frepl_tabSearch" text="Search">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="fr_Stableview" layoutY="87.0" prefHeight="287.0" prefWidth="508.0">
                          <columns>
                            <TableColumn fx:id="fr_Sline" editable="false" prefWidth="75.0" sortable="false" text="Line" />
                            <TableColumn fx:id="fr_SColumn" editable="false" prefWidth="61.0" sortable="false" text="Column" />
                              <TableColumn fx:id="fr_SValue" editable="false" prefWidth="371.0" sortable="false" text="Value" />
                          </columns>
                        </TableView>
                        <TextField fx:id="fr_searchInput" layoutX="66.0" layoutY="13.0" prefHeight="25.0" prefWidth="250.0" />
                        <Label layoutX="3.0" layoutY="17.0" text="Search for:" />
                        <Button fx:id="fr_find" layoutX="324.0" layoutY="13.0" mnemonicParsing="false" text="Find" />
                        <Button fx:id="fr_findAll" layoutX="372.0" layoutY="13.0" mnemonicParsing="false" text="Find All" />
                        <CheckBox fx:id="fr_matchCaseS" layoutX="324.0" layoutY="53.0" mnemonicParsing="false" text="Match case (Aa)" />
                        <Label fx:id="fr_foundS" layoutX="8.0" layoutY="70.0" text="found: 5" underline="true">
                           <font>
                              <Font name="System Bold Italic" size="12.0" />
                           </font>
                        </Label>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab fx:id="frepl_tabReplace" text="Replace">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="fr_tableviewR" layoutY="107.0" prefHeight="267.0" prefWidth="508.0">
                           <columns>
                              <TableColumn fx:id="fr_lineR" prefWidth="75.0" text="Line" />
                              <TableColumn fx:id="fr_ColumnR" prefWidth="61.0" text="Column" />
                              <TableColumn fx:id="fr_ValueR" prefWidth="371.0" text="Value" />
                           </columns>
                        </TableView>
                        <CheckBox fx:id="fr_matchCaseR" layoutX="347.0" layoutY="59.0" mnemonicParsing="false" text="Match case (Aa)" />
                        <Label layoutX="3.0" layoutY="17.0" text="Search for:" />
                        <Button fx:id="fr_replace" layoutX="347.0" layoutY="11.0" mnemonicParsing="false" text="Replace" />
                        <TextField fx:id="fr_replaceInput" layoutX="82.0" layoutY="13.0" prefHeight="25.0" prefWidth="250.0" />
                        <Button fx:id="fr_replaceAll" layoutX="412.0" layoutY="11.0" mnemonicParsing="false" text="Replace All" />
                        <TextField fx:id="fr_replaceAllInput" layoutX="82.0" layoutY="55.0" prefHeight="25.0" prefWidth="250.0" />
                        <Label layoutX="3.0" layoutY="59.0" text="Replace with:" />
                        <Label fx:id="fr_replacedR" layoutX="3.0" layoutY="90.0" text="replaced: 5" underline="true">
                           <font>
                              <Font name="System Bold Italic" size="12.0" />
                           </font>
                        </Label>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</Pane>
user3776738
  • 214
  • 2
  • 10
  • 27
  • What reaction are you looking for? – ItachiUchiha May 10 '16 at 10:03
  • for the reaction of the fr_find button,when clicked. – user3776738 May 10 '16 at 10:08
  • I mean if the search box is displayed, what else are you looking for? Do you want the text typed on the box to be copied back to the main stage? – ItachiUchiha May 10 '16 at 10:13
  • no,i know that it i have to do this with interfaces.the point is that the DOM elements like buttons don't react or better: i can't catch the events.So "System.out.println("search");" doesn't show up – user3776738 May 10 '16 at 10:24
  • I am not sure if I understand your question. As far as I have understood, `setOnMouseClicked()` should have worked. Can you try [setOnAction()](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ButtonBase.html#setOnAction-javafx.event.EventHandler-). – ItachiUchiha May 10 '16 at 10:31
  • no it doesn't work,too.but i think i have to implment an initialize method.but i don't know where. – user3776738 May 10 '16 at 10:45
  • Ohh, now I see the problem. You are re-initializing the fxml elements when defining them. You shouldn't do it. Remove `=new ..();` and `static` from all your UI elements which are annotated by `@FXML`. For example, `@FXML private static Button fr_find=new Button();` should be actually `@FXML private Button fr_find;` – ItachiUchiha May 10 '16 at 10:47
  • hmm.this doesn't help.i even get an nullpointer exception. – user3776738 May 10 '16 at 10:58
  • Add the stack trace to the question. – ItachiUchiha May 10 '16 at 10:59
  • Adding the FXML will also help to provided a concrete solution. – ItachiUchiha May 10 '16 at 11:07
  • Well, the exception makes sense. You should not define a static method and rather pass the initialized controller to wherever `btn_search` exists. – ItachiUchiha May 10 '16 at 11:12
  • yes,i have had a static method.but i don't know how to initialize a controler in here.do i need a fx:controler in the fxml file? – user3776738 May 10 '16 at 11:16
  • 1
    Read through [Pass parameters while instantiating of the controller class in JavaFX](http://stackoverflow.com/questions/30814258/pass-parameters-while-instantiating-of-the-controller-class-in-javafx) and [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). It will help you under how does controllers interact and how can you pass data between them. – ItachiUchiha May 10 '16 at 11:20
  • this did the trick – user3776738 May 10 '16 at 11:48
  • Glad, it helped :) – ItachiUchiha May 10 '16 at 11:50
  • but only in combination with your tip to use non static variables ;) – user3776738 May 10 '16 at 11:53

0 Answers0