Javafx textbox resizes to text length?

Hi guys, I am creating a chat server where I use a text box on the screen to enter the chat message that the user is writing, the idea is that it works like a bubble above a person’s head when he enters a message.

my question is not to make the text box too big or too big to resize the text box (crop if you want) so that it adjusts the text written in the text box?

PS I am using JavaFx scriptbuilder to do all this.

+4
source share
6 answers

It's time to do some coding backstage (builder) :).
The following code snippet is not a neat solution, but better than none. :)

// define width limits textField.setMinWidth(50); textField.setPrefWidth(50); textField.setMaxWidth(400); // add listner textField.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { textField.setPrefWidth(textField.getText().length() * 7); // why 7? Totally trial number. } }); 
+5
source

You can use the computeTextWidth method in com.sun.javafx.scene.control.skin.Utils . this method is used in the javafx.scene.control.Label class to calculate the minimum width for shortcut content.

I solved my problem as below:

 field.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> ob, String o, String n) { // expand the textfield field.setPrefWidth(TextUtils.computeTextWidth(field.getFont(), field.getText(), 0.0D) + 10); } }); 

I added a listener to textProperty , and every time I change the text, I change the prefWidth text field.

Note: if Utils.computeTextWidth() not publicly available , I copied the source code to a new class ( TextUtils ).

Here is the complete source code:

 package me.jone30rw.fxcontrol; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextBoundsType; public class TextUtils { static final Text helper; static final double DEFAULT_WRAPPING_WIDTH; static final double DEFAULT_LINE_SPACING; static final String DEFAULT_TEXT; static final TextBoundsType DEFAULT_BOUNDS_TYPE; static { helper = new Text(); DEFAULT_WRAPPING_WIDTH = helper.getWrappingWidth(); DEFAULT_LINE_SPACING = helper.getLineSpacing(); DEFAULT_TEXT = helper.getText(); DEFAULT_BOUNDS_TYPE = helper.getBoundsType(); } public static double computeTextWidth(Font font, String text, double help0) { // Toolkit.getToolkit().getFontLoader().computeStringWidth(field.getText(), // field.getFont()); helper.setText(text); helper.setFont(font); helper.setWrappingWidth(0.0D); helper.setLineSpacing(0.0D); double d = Math.min(helper.prefWidth(-1.0D), help0); helper.setWrappingWidth((int) Math.ceil(d)); d = Math.ceil(helper.getLayoutBounds().getWidth()); helper.setWrappingWidth(DEFAULT_WRAPPING_WIDTH); helper.setLineSpacing(DEFAULT_LINE_SPACING); helper.setText(DEFAULT_TEXT); return d; } } 
+10
source

Since JavaFX 8, this is by far the easiest:

 textField.prefColumnCountProperty().bind(textField.textProperty().length()); 
+6
source

JavaFX 8 has a solution for this, here is the code:

 TextField tf = new TextField(); // Set Max and Min Width to PREF_SIZE so that the TextField is always PREF tf.setMinWidth(Region.USE_PREF_SIZE); tf.setMaxWidth(Region.USE_PREF_SIZE); tf.textProperty().addListener((ov, prevText, currText) -> { // Do this in a Platform.runLater because of Textfield has no padding at first time and so on Platform.runLater(() -> { Text text = new Text(currText); text.setFont(tf.getFont()); // Set the same font, so the size is the same double width = text.getLayoutBounds().getWidth() // This big is the Text in the TextField + tf.getPadding().getLeft() + tf.getPadding().getRight() // Add the padding of the TextField + 2d; // Add some spacing tf.setPrefWidth(width); // Set the width tf.positionCaret(tf.getCaretPosition()); // If you remove this line, it flashes a little bit }); }); tf.setText("Hello World!"); 
  • In JavaFX 2.2, this code works with slight restrictions. You cannot install the font (so if you are not using the std font, you must install it manually).
  • You cannot get a registration from TextField (so if you know a registration, write it hardcoded).

Happy coding,
Kalasch

+4
source

No font-specific magic is required if you use setPrefColumnCount

  tf.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> ob, String o, String n) { tf.setPrefColumnCount(tf.getText().length() +1); } }); 
+2
source

The best way to do this is to use the JavaFX option "USE_COMPUTED_SIZE". You can define it in FXML or programmatically as follows:

 TextField textField = new TextField("hello"); textField.setPrefWidth(Control.USE_COMPUTED_SIZE); 
0
source

Source: https://habr.com/ru/post/1437918/


All Articles