How To Set Border Color Of TextField in JavaFX?

Overview

In this tutorial, we show you how to set border color of TextField in JavaFX.
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
JavaFX Scene Builder 2.0

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.
<?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane id="AnchorPane" fx:id="main" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #FFE4C4;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jackrutorial.ExampleController">
   <children>
      <TextField layoutX="150.0" layoutY="169.0" prefHeight="50.0" prefWidth="300.0" promptText="This is JackRutorial.Com" style="-fx-text-box-border: #B22222; -fx-focus-color: #B22222;" />
      <Button layoutX="150.0" layoutY="253.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Go" />
   </children>
</AnchorPane>

Set Border Color Of TextField using javafx.scene.control.TextField.setStyle

Using method javafx.scene.control.TextField.setStyle
    @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;");
    }


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;");
    }
}

Example.fxml

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

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

<AnchorPane id="AnchorPane" fx:id="main" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #FFE4C4;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jackrutorial.ExampleController">
   <children>
      <TextField fx:id="txtDescription" layoutX="150.0" layoutY="169.0" prefHeight="50.0" prefWidth="300.0" promptText="This is JackRutorial.Com" />
      <Button layoutX="150.0" layoutY="253.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Go" />
   </children>
</AnchorPane>

Previous Post
Next Post

post written by: