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

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

问题描述

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

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



PS我正在使用JavaFx场景构建器完成所有这些。

解决方案

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

  //定义宽度限制
textField.setMinWidth(50);
textField.setPrefWidth(50);
textField.setMaxWidth(400);
//添加列表器
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。
}
});


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?

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

解决方案

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天全站免登陆