Home > Default > How to set alignment in a TextField?

How to set alignment in a TextField?

November 30Hits:0
Advertisement
I need to set allignment of the text in a TextField to right. There is no such under TextField.
Anybody knows how to set alignment in a textfield in javaFX 2.0 ?
Thanks

Answers

It may be worth filing a feature request jira to get platform native support for this.
You might think that textField.setStyle("-fx-text-alignment: right;") would work, but it does not.
I guess that the -fx-text-alignment css property only right aligns multi-line text, not text within a text field.
The following code uses binding to keep Text in a TextField right aligned.
The translations place the text visually right and keyboard editing is OK.
Mouse transparency is enabled because mouse based selections still act as though the text is left aligned.
So it's not a complete solution.
public void start(final Stage stage) throws Exception {
  // create a text field.
  final TextField textField = new TextField("This is right aligned text");
  textField.setStyle("-fx-text-alignment: right;");
  textField.setMinWidth(400);
  textField.setMouseTransparent(true);
  // layout the scene.
  final VBox layout = new VBox();
  layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-alignment: center;");
  layout.getChildren().addAll(textField);
  Scene scene = new Scene(layout);
  stage.setScene(scene);
  stage.show();
  // keep the text in the textbox right aligned.
  final Text text = (Text) textField.lookup("Text");
  rightAlign(text, textField);
  textField.textProperty().addListener(new InvalidationListener() {
    @Override public void invalidated(Observable observable) {
      rightAlign(text, textField);
private void rightAlign(Text text, TextField textField) {
  text.getParent().translateXProperty().bind(textField.widthProperty().subtract(text.xProperty().multiply(2)).subtract(text.getLayoutBounds().getWidth()));
}

Read other 3 answers

Tags:

Related Articles

  • How to set alignment in a TextField?November 30

    I need to set allignment of the text in a TextField to right. There is no such under TextField. Anybody knows how to set alignment in a textfield in javaFX 2.0 ? ThanksIt may be worth filing a feature request jira to get platform native support for t

  • How to set alignment of the TextFieldNovember 30

    What method I shall use to set is the alignment of the TextField to have is Right Aligned? Regds JayI've found: public void setComponentOrientation(ComponentOrientation o)Sets the language-sensitive orientation that is to be used to order the element

  • Right aligned text in textfieldNovember 30

    Hi, I'm trying to set the text in a textfield to be right-aligned, as it is a number textfield. I'm trying to do this with awt only (as opposed to Swing). As our number system is Arabic, I figured on doing the following: <code> txtfld.setComponentOr

  • Vertically align top on textFieldNovember 30

    I have dynamic textField and need to align my text vertically top on textField. How can I set my text vertically top ?I don't understand your question... do you want the text on just one line or do you want the text to go like: a b c d Please explain

  • Align outputtext in textFields.November 30

    How can I align (horizontal) text in a textField when I use the textField for output. Default the text is put left, but when I use numbers (as string), I 'd like to put the numbers right for a good review. J.W.Applet (java.applet.Applet) is in the AW

  • Alignment of a TextFieldNovember 30

    Hello out there: I have a Problem: when I click on a button OK a text from a Jlist must be put into a Text field. When the text is too long for the text Field The user cannot se the beginning of the word. That's not what I want to have. I want to hav

  • Center Text in TextField DynamicallyOctober 11

    How on earth do you center text within a text field dynamically....e.g. textField.align = "center" or textField.autoSize = "center" ....nothing works? BTW...is it just me or is the adobe site just plain slow and almost unusable to ever

  • Blurry text when centering textfield in datagridOctober 11

    Gidday I'm have a TextFormatAlign.CENTER alignment on the textfields in my datagrid column headers. The text sometimes gets blurry, depending on how the window is resized. I normally use Math.round() when positioning text fields, and I'm applying the

  • Layout for editing database recordNovember 30

    Hi! I'm trying to make a JPanel display a database record for editing in a nice way, but I'm kinda stuck. Example:         Id: 10 First name: Peter Last name: Johnson        Age: 20 and so on. The problem is that I don't know what strategy to use for

  • Struts 2 Client side validation getting skipped.November 30

    I am doing a simple program using struts 2. Usually in struts2 bean class will be combined with action class unlike in struts 1.2. But for enhancing readability I made a seperate bean class and I reference it in my action class. But after seperating

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 3.084 s.