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.
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.
can you show us how you did that ??
thankss
You can see the code in the jfxtras project.
Thanks,
could you please give a link…I could not find it in jfxtras website
Hi,
Here: https://github.com/JFXtras/jfxtras-styles
Regards,
I am not getting it right even with your update can you answer that please……..http://stackoverflow.com/questions/28363311/how-to-add-custom-skin-for-textfield-in-javafx-using-css
Are you adding the stylesheet to the scene object?
how???
scene.getStylesheets().add(“xpto.css”)
i have connected the style sheet with my interface using scene builder every other thing in JMetroDarkTheme.css is working except this.
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.
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?
Thanks Subhranil! Maybe increase the right padding on the node of styleclass “text”.
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);}
}
Yep that should do it.