The Metro Text Box control is the usual input text field from which developers can get text input from users. This control has a particular trait usually present in control library designed to be used through touch: whenever the user enters text a clear button appears on the right most position of the control.

metroTextBox

Windows 8 Text input control

 

This particular feature isn’t available in the text input control from the JavaFX library, so the use of CSS won’t be enough to achieve a “metro” style.

For this reason I’ve created a new control: ErasableTextField (Yes, I know, not the best choice of a name for a control 🙂 ), which extends from TextField and adds this missing feature: a button which appears inside the text input control to clear it whenever there is text inside. Then through the usual use of CSS I style the control to make it look like the Windows 8 Text Box.

JMetro TextBox Dark theme - mouse pressed

JMetro Dark Theme – mouse pressed

JMetro TextBox Dark theme

JMetro Dark Theme

JMetro TextBox Light Theme - text selected

JMetro Light Theme – text highlighted

JMetro TextBox Light theme

JMetro TextBox Light Theme

14 thoughts on “Metro style Text Box control for java (JMetro)

  1. i have connected the style sheet with my interface using scene builder every other thing in JMetroDarkTheme.css is working except this.

  2. They are connected.All calls are going correctly to event handlers but just that right button is not showing up.i have uploaded all my code to the link, i just gave you above.

  3. Hi. First of all nice work, and hats off. I’m using your stylesheet for quite a few of my applications, though I’ve tweaked it. Like, implemented Light and Dark in a single stylesheet using looked up colors for runtime swapping of base theme, implemented accent color to all control via looked up colors for the same reason. However, I’ve noticed that the text is overlapping the cross button instead of being padded by it. Any solutions?

      • Hey, it’s not so complicated. Just change the layoutChildren() in TextFieldWithButtonSkin to the following :
        @Override
        protected void layoutChildren(double x, double y, double w, double h) {
        super.layoutChildren(x, y, w, h);

        final double clearGraphicWidth = snapSize(rightButtonGraphic.prefWidth(-1));
        final double clearButtonWidth = rightButton.snappedLeftInset() + clearGraphicWidth + rightButton.snappedRightInset();
        rightButton.resize(clearButtonWidth, h);
        //don’t abuse unecessary space, particularly useful when the button is hidden
        if(rightButton.isVisible()){
        //Resize Content to prevent text overlap on the button
        getChildren().get(0).resize(w – clearButtonWidth-5,h);

        positionInArea(rightButton,
        (x + w) – clearButtonWidth, y,
        clearButtonWidth, h, 0, HPos.CENTER, VPos.CENTER);}
        }

Leave a Reply

Your email address will not be published. Required fields are marked *