Javafx文本字段调整为文本长度?

时间:2012-10-05 00:18:38

标签: user-interface javafx textfield

大家好我正在构建一个聊天服务器,我在屏幕上使用文本字段输入用户写的聊天消息,这个想法是当他输入消息时,它就像个人头上的泡泡一样。 / p>

我的问题是为了不使文本框太大或太小有没有办法让文本框调整大小(如果你愿意修剪)所以它调整到文本字段中写的文字?

P.S。我正在使用JavaFx场景构建器来完成所有这些。

6 个答案:

答案 0 :(得分:14)

您可以在computeTextWidth中使用com.sun.javafx.scene.control.skin.Utils方法。该方法在javafx.scene.control.Label类中用于计算标签内容的最小宽度。

我解决了我的问题如下:

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

我已经为textProperty添加了一个监听器,每次更改文本时都会更改文本字段的prefWidth

注意:只要Utils.computeTextWidth() 不公开,我就会将源代码复制到新类(TextUtils)。

以下是完整的源代码:

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

答案 1 :(得分:9)

自JavaFX 8以来,这是迄今为止最简单的:

textField.prefColumnCountProperty().bind(textField.textProperty().length());

答案 2 :(得分:8)

在JavaFX 8中,有一个解决方案,这里是代码:

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!");
  • 在JavaFX 2.2中,此代码的作用很小。您无法设置字体(因此,如果您不使用标准字体,则必须手动设置)。
  • 您无法从TextField获取填充(因此,如果您知道填充,请将其写为硬编码)。

快乐编码,
Kalasch

答案 3 :(得分:6)

是时候在幕后做一些编码了(建设者):) 以下代码块不是一个简洁的解决方案,但总比没有好。 :)

// 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.
    }
});

答案 4 :(得分:3)

如果使用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);
            }
        });

答案 5 :(得分:0)

最好/最简单的方法是使用JavaFX的“USE_COMPUTED_SIZE”选项。您可以在FXML中定义它,或者以编程方式定义它:

TextField textField = new TextField("hello");
textField.setPrefWidth(Control.USE_COMPUTED_SIZE);
相关问题