在TextField中用Text替换Text [英] Replace Text with number in TextField

查看:133
本文介绍了在TextField中用Text替换Text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字段,我在其中插入端口号。我想将字符串自动转换为数字:

I have this field in which I insert port number. I would like to convert the string automatically into number:

fieldNport = new TextField();
    fieldNport.setPrefSize(180, 24);
    fieldNport.setFont(Font.font("Tahoma", 11));
    grid.add(fieldNport, 1, 1);

你能告诉我怎么做吗?我在堆栈溢出中找不到合适的例子。

Can you tell how I can do this? I cannot find suitable example in stack overflow.

编辑:

可能这样:

 fieldNport.textProperty().addListener(new ChangeListener()
        {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal)
            {
                try
                {
                    int Nport = Integer.parseInt((String) oldVal);
                }
                catch (NumberFormatException e)
                {

                }
            }
        });


推荐答案

你可以这样写:

fieldNPort.text.addListener(new ChangeListener(){
        @Override public void changed(ObservableValue o,Object oldVal, Object newVal){
             //Some Code
             //Here you can use Integer.parseInt methods inside a try/catch 
             //because parseInt throws Exceptions
        }
      });

以下是JavaFX中有关属性和监听器的所有内容:

http://docs.oracle.com/javafx/2/binding /jfxpub-binding.htm

如果您有任何疑问,我将很乐意为您提供帮助。

Here are all the things you'd need about properties and Listeners in JavaFX:
http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
If you have any question, I'll be glad to help.

这篇关于在TextField中用Text替换Text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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