Javafx 文本字段调整为文本长度? [英] Javafx textfield resize to text length?

查看:32
本文介绍了Javafx 文本字段调整为文本长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello guys I am building a chat server where I use a textfield on the screen to type in the chat message that the user writes, the idea is that it works like a bubble over a persons head when he types a message.

我的问题是为了不使文本框太大或太小,有没有办法调整文本框的大小(如果愿意,可以修剪)以便它适应文本字段中写入的文本?

my question is in order to not make a textbox that is too large or too small is there a way to make the textbox resize (trim if you will) so it adjust to the text written in the textfield?

附言我正在使用 JavaFx 场景构建器来完成所有这些.

P.S. I'm using JavaFx scenebuilder to do all of this.

推荐答案

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

It is time to do some coding behind the scenes(builder) :).
The following code chunk 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.
    }
});

这篇关于Javafx 文本字段调整为文本长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆