- How To Set Border Color Of TextField in JavaFX?
- Set Border Color Of TextField using JavaFX Scene Builder 2.0
- Set Border Color Of TextField by edit *.fxml file
- Set Border Color Of TextField using javafx.scene.control.TextField.setStyle
- Full Source Code
- ExampleController.java
- JavaFX – Set focus border for Textfield using CSS
- Best Solution
- Related Solutions
- JavaFX – Set focus border for Textfield using CSS
- Best Solution
- Related Solutions
How To Set Border Color Of TextField in JavaFX?
Set Border Color Of TextField using JavaFX Scene Builder 2.0
If you are using JavaFX Scene Builder 2.0, go to the Style of the TextField in the Properties section of the Inspector panel. Add Css Style: -fx-text-box-border: #B22222; -fx-focus-color: #B22222
Set Border Color Of TextField by edit *.fxml file
Edit Example.fxml file, then Add CSS Style style=»-fx-text-box-border: #B22222; -fx-focus-color: #B22222;» to TextField tag.
Set Border Color Of TextField using javafx.scene.control.TextField.setStyle
@FXML private TextField txtDescription; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb)
Full Source Code
ExampleController.java
package com.jackrutorial; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TextField; public class ExampleController implements Initializable < @FXML private TextField txtDescription; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) < txtDescription.setStyle("-fx-text-box-border: #B22222; -fx-focus-color: #B22222;"); >>
JavaFX – Set focus border for Textfield using CSS
I want to have a textfield with three different borders for three cases:
- a white border when not hovered or focused
- a grey border when hovering
- a blue border when focused and typing
#custom-text-field < -fx-border-width: 2; -fx-border-color: white; >#custom-text-field:hover < -fx-border-width: 2; -fx-border-color: #909090; >#custom-text-field:focused
The problem is that the border for focusing never shows up. How do it set it correctly?
Best Solution
.custom-text-field < -fx-background-color: #FFFFFF, #FFFFFF; -fx-background-insets: 0, 2; -fx-background-radius: 0, 0; >.custom-text-field:focused < -fx-background-color: #0093EF, #FFFFFF; >.custom-text-field:hover < -fx-background-color: #909090, #FFFFFF; >.custom-text-field:focused:hover
Related Solutions
Java – Clear prompt text in JavaFX TextField only when user starts typing
This example will allow TextFields in JavaFX whose prompt behaviour is to show the prompt when the field is empty, even if the field has focus. The solution is a combination of custom CSS and a custom TextField class, which manipulates the css styles of the TextField.
.persistent-prompt:focused < -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); >.no-prompt
import javafx.scene.control.TextField; public class PersistentPromptTextField extends TextField < PersistentPromptTextField(String text, String prompt) < super(text); setPromptText(prompt); getStyleClass().add("persistent-prompt"); refreshPromptVisibility(); textProperty().addListener(observable ->refreshPromptVisibility()); > private void refreshPromptVisibility() < final String text = getText(); if (isEmptyString(text)) < getStyleClass().remove("no-prompt"); >else < if (!getStyleClass().contains("no-prompt")) < getStyleClass().add("no-prompt"); >> > private boolean isEmptyString(String text) < return text == null || text.isEmpty(); >>
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class PromptChanger extends Application < @Override public void start(Stage stage) throws Exception < TextField textField1 = new PersistentPromptTextField("", "First name"); TextField textField2 = new PersistentPromptTextField("", "Last name"); VBox layout = new VBox( 10, textField1, textField2 ); layout.setPadding(new Insets(10)); Scene scene = new Scene(layout); scene.getStylesheets().add( getClass().getResource( "persistent-prompt.css" ).toExternalForm() ); stage.setScene(scene); stage.show(); >public static void main(String[] args) < Application.launch(args); >>
How prompt handling is currently implemented in JavaFX 8
Default CSS for JavaFX 8 (modena.css) for controlling the prompt text is as follows:
.text-input < -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); >.text-input:focused
This will make the prompt text transparent whenever a field is focused, even if the field has no data in it.
In Comparison to HTML
HTML input has a placeholder, which is specified as follows:
User agents should present this hint to the user . . . when the element’s value is the empty string or the control is not focused (or both).
You can try this functionality in your browser at this test link.
I think the argument against this behaviour for JavaFX «Prompt text should be cleared when focus is grabbed to signal readiness to receiving user input» is moot because focused text fields get a clearly visible focus ring, so the user knows that the control is ready to receive input even when the prompt text is displayed.
I think JavaFX should by default operate the same way as most HTML user agents in this respect. Feel free to create a Tweak request in the JavaFX issue tracker to request that the input prompt in JavaFX work similarly to HTML implementations (if there isn’t an issue created for this already).
Alternative
The third party Gluon Glisten has a custom TextField control can have the following attributes:
- Float Text— A place holder text inside a TextField which is transitioned to the top of the TextField when focus is received by it.
The nice thing about the Glisten based TextField is that the prompt text is always visible, whether or not the user has typed anything into the control.
JavaFX – Set focus border for Textfield using CSS
I want to have a textfield with three different borders for three cases:
- a white border when not hovered or focused
- a grey border when hovering
- a blue border when focused and typing
#custom-text-field < -fx-border-width: 2; -fx-border-color: white; >#custom-text-field:hover < -fx-border-width: 2; -fx-border-color: #909090; >#custom-text-field:focused
The problem is that the border for focusing never shows up. How do it set it correctly?
Best Solution
.custom-text-field < -fx-background-color: #FFFFFF, #FFFFFF; -fx-background-insets: 0, 2; -fx-background-radius: 0, 0; >.custom-text-field:focused < -fx-background-color: #0093EF, #FFFFFF; >.custom-text-field:hover < -fx-background-color: #909090, #FFFFFF; >.custom-text-field:focused:hover
Related Solutions
Java – Clear prompt text in JavaFX TextField only when user starts typing
This example will allow TextFields in JavaFX whose prompt behaviour is to show the prompt when the field is empty, even if the field has focus. The solution is a combination of custom CSS and a custom TextField class, which manipulates the css styles of the TextField.
.persistent-prompt:focused < -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); >.no-prompt
import javafx.scene.control.TextField; public class PersistentPromptTextField extends TextField < PersistentPromptTextField(String text, String prompt) < super(text); setPromptText(prompt); getStyleClass().add("persistent-prompt"); refreshPromptVisibility(); textProperty().addListener(observable ->refreshPromptVisibility()); > private void refreshPromptVisibility() < final String text = getText(); if (isEmptyString(text)) < getStyleClass().remove("no-prompt"); >else < if (!getStyleClass().contains("no-prompt")) < getStyleClass().add("no-prompt"); >> > private boolean isEmptyString(String text) < return text == null || text.isEmpty(); >>
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class PromptChanger extends Application < @Override public void start(Stage stage) throws Exception < TextField textField1 = new PersistentPromptTextField("", "First name"); TextField textField2 = new PersistentPromptTextField("", "Last name"); VBox layout = new VBox( 10, textField1, textField2 ); layout.setPadding(new Insets(10)); Scene scene = new Scene(layout); scene.getStylesheets().add( getClass().getResource( "persistent-prompt.css" ).toExternalForm() ); stage.setScene(scene); stage.show(); >public static void main(String[] args) < Application.launch(args); >>
How prompt handling is currently implemented in JavaFX 8
Default CSS for JavaFX 8 (modena.css) for controlling the prompt text is as follows:
.text-input < -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); >.text-input:focused
This will make the prompt text transparent whenever a field is focused, even if the field has no data in it.
In Comparison to HTML
HTML input has a placeholder, which is specified as follows:
User agents should present this hint to the user . . . when the element’s value is the empty string or the control is not focused (or both).
You can try this functionality in your browser at this test link.
I think the argument against this behaviour for JavaFX «Prompt text should be cleared when focus is grabbed to signal readiness to receiving user input» is moot because focused text fields get a clearly visible focus ring, so the user knows that the control is ready to receive input even when the prompt text is displayed.
I think JavaFX should by default operate the same way as most HTML user agents in this respect. Feel free to create a Tweak request in the JavaFX issue tracker to request that the input prompt in JavaFX work similarly to HTML implementations (if there isn’t an issue created for this already).
Alternative
The third party Gluon Glisten has a custom TextField control can have the following attributes:
- Float Text— A place holder text inside a TextField which is transitioned to the top of the TextField when focus is received by it.
The nice thing about the Glisten based TextField is that the prompt text is always visible, whether or not the user has typed anything into the control.