Javafx:TextField焦点丢失了

时间:2018-02-09 18:25:36

标签: javafx textfield menuitem

正如标题所说:

我在CustomMenuItem中有一个TextField,由于MenuItem的onHover,TextField总是失去对最小鼠标移动的关注。在API的源代码中,我在MenuBarSkin中看到了受保护的setHover方法:(

示例:

Main.java

 public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

sample.fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <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" />
                  <CustomMenuItem mnemonicParsing="false" text="Unspecified Action">
                     <content>
                        <HBox>
                           <children>
                              <Label text="Label" />
                              <TextField text="Test" />
                           </children>
                        </HBox>
                     </content>
                  </CustomMenuItem>
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

好的,我明白了:

它有点脏......

Controller.java

public class Controller implements Initializable{

    @FXML
    TextField txtField;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
       txtField.setOnMouseMoved(event -> {
            txtField.requestFocus();
           });
       }

}

sample.fxml

<BorderPane fx:controller="Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <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" />
                  <CustomMenuItem mnemonicParsing="false" text="Unspecified Action">
                     <content>
                        <HBox>
                           <children>
                              <Label text="Label" />
                              <TextField fx:id="txtField" text="Test" />
                           </children>
                        </HBox>
                     </content>
                  </CustomMenuItem>
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>