2

enter image description here

As you can see, the first text field (username) is selected, and this happens all the times I open the app. This is not good, because I want the person who opens the app to actually see "username" text field, and then let him click and then write.

How to do that with Scenebuilder? or maybe via java code?

This is the fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.vipagepharma.farmacia.autenticazione.login.SchermataLogin">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="467.0" prefWidth="711.0" VBox.vgrow="ALWAYS">
         <children>
            <ImageView fitHeight="121.0" fitWidth="122.0" layoutX="30.0" layoutY="-12.0">
               <image>
                  <Image url="@../../../22b6dd45a8834880a7e4b3e0d2d6645e-removebg-preview.png" />
               </image>
            </ImageView>
            <TextField id="IdTextField" fx:id="username" layoutX="249.0" layoutY="125.0" promptText="ID" style="-fx-background-radius: 5; -fx-border-radius: 5;" AnchorPane.leftAnchor="249.0" AnchorPane.rightAnchor="227.0">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></TextField>
            <TextField fx:id="password" layoutX="249.0" layoutY="161.0" promptText="Password" style="-fx-background-radius: 5; -fx-border-radius: 5;" AnchorPane.leftAnchor="249.0" AnchorPane.rightAnchor="227.0">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></TextField>
            <Button fx:id="login" alignment="CENTER" layoutX="279.0" layoutY="200.0" mnemonicParsing="false" onMouseClicked="#onLoginClicked" prefHeight="27.0" prefWidth="105.0" style="-fx-background-color: #CFF3F2 #CFF3F2; -fx-border-color: #b9b9b9; -fx-background-radius: 5; -fx-border-radius: 5;" text="Login" AnchorPane.leftAnchor="279.0" AnchorPane.rightAnchor="266.0">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></Button>
            <Button id="bottoneRegistrati" alignment="CENTER" layoutX="311.0" layoutY="313.0" mnemonicParsing="false" onMouseClicked="#onRegistrazioneClicked" prefHeight="25.0" prefWidth="95.0" style="-fx-background-radius: 5; -fx-border-radius: 5; -fx-background-color: cff3f2; -fx-border-color: b9b9b9;" text="Registrati" AnchorPane.leftAnchor="311.0" AnchorPane.rightAnchor="234.0">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></Button>
            <Text layoutX="127.0" layoutY="291.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Hai dimenticato la password?" AnchorPane.leftAnchor="127.0" AnchorPane.rightAnchor="354.06982421875">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></Text>
            <Button id="bottoneReimposta" alignment="CENTER" layoutX="339.0" layoutY="272.0" mnemonicParsing="false" style="-fx-background-radius: 5; -fx-border-radius: 5; -fx-background-color: cff3f2; -fx-border-color: b9b9b9;" text="Reimposta password" AnchorPane.leftAnchor="339.0" AnchorPane.rightAnchor="169.0">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></Button>
            <Text layoutX="162.0" layoutY="332.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Non sei registrato?" AnchorPane.leftAnchor="162.0" AnchorPane.rightAnchor="389.10400390625">
               <font>
                  <Font name="Arial" size="15.0" />
               </font></Text>
         </children>
         <VBox.margin>
            <Insets />
         </VBox.margin>
    </AnchorPane>
  </children>
</VBox>
Allexj
  • 1,375
  • 6
  • 14
  • 29
  • 4
    Just call `requestFocus()` on whatever node you want to have the focus, in the controller's `initialize()` method. – James_D Aug 05 '22 at 18:27
  • I don't want the Focus on anything, though – Allexj Aug 05 '22 at 18:41
  • 1
    I think you should allow the focus to occur but display the prompt text when the field is empty. The problem is the [default css](https://gist.github.com/maxd/63691840fc372f22f470#file-modena-css-L1300), makes the prompt text transparent on focus. Instead modify the app so the prompt text is transparent only if focused and non-empty. – jewelsea Aug 05 '22 at 18:51
  • 1
    Another option I have seen is an animation on focus that moves the prompt text in a small font above the field when it is focused so that it is always visible. Then the behaviour is quite different however. See [MaterialsFX](https://github.com/palexdev/MaterialFX#preview-gifs) for variations on this theme, click on the “Fields” demo option. If choosing this option, I recommend using MaterialsFX rather than hacking your own solution. – jewelsea Aug 05 '22 at 18:56
  • If you really “don’t want the focus on anything”, just put it in something that doesn’t respond to keyboard input in any way, such as the `VBox` or `AnchorPane` – James_D Aug 05 '22 at 18:56
  • 4
    I think this is an [xyproblem](https://xyproblem.info/), your problem is not with focus but instead with default behaviour on focus. – jewelsea Aug 05 '22 at 19:02
  • 4
    An implementation of @jewelsea’s suggestion is given as an answer to [this question](https://stackoverflow.com/questions/25125563/clear-prompt-text-in-javafx-textfield-only-when-user-starts-typing) – James_D Aug 05 '22 at 19:30
  • Thanks! I did: `textField.setStyle("-fx-prompt-text-fill: derive(-fx-control-inner-background, -30%);");` and it fixed it!! – Allexj Aug 06 '22 at 07:57

0 Answers0