根据文本字段条目更新字符串属性

时间:2016-04-06 15:04:35

标签: javafx-8

我有一个框架,有一个文本字段,布局是naff但这不是目前的目标。

我想根据输入到文本字段中的内容更新字符串属性,然后将其打印在下面的控制台中(eclipse)

类似于通过调用.set()

在代码本身内进行更新

这是我的代码 -

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;

public class TextEntry extends Application
{
    private static StringProperty text = 
            new SimpleStringProperty("text");

    public static void main(String [] args)
    {
        launch(args);
    }
    public void start(Stage primaryStage)
    {
        Pane root = new Pane();

        TextField enterText = new TextField();
        enterText.setFont(Font.font("SanSerif",20));

        enterText.setOnMousePressed(e ->{
            text.bind(enterText.textProperty());
            System.out.println("the new value is: " + text);
        });

        root.getChildren().addAll(enterText);
        Scene scene = new Scene(root,600,600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Text Entry");
        primaryStage.show();
    }
}

我尝试了一些事情,包括上面代码中的行 -

text.set("").bind(enterText.textProperty());

text.textProperty().bind(enterText.textProperty());

第二个在语法上是不正确的我意识到,但我想不出解决方案,任何想法?

1 个答案:

答案 0 :(得分:0)

您只需要绑定属性一次,而不是每次按下一个键。然后只需在属性中添加Sub Button1_Click() 'Set and instantiate our working objects Dim Req As Object Dim sEnv As String Dim Resp As New MSXML2.DOMDocument60 Set Req = CreateObject("MSXML2.XMLHTTP") Set Resp = CreateObject("MSXML2.DOMDocument.6.0") With Req .Open "Post", "https://XXXX.com/", False Dim Pwd As String Pwd = Range("D8").Value Dim QuoteId As String QuoteId = Range("D9").Value ' SOAP envelope for submission to the Web Service sEnv = sEnv & "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">" sEnv = sEnv & " <soapenv:Header>" sEnv = sEnv & " <wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:wsu=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"">" sEnv = sEnv & " <wsse:UsernameToken wsu:Id=""UsernameToken-2"">" sEnv = sEnv & " <wsse:Username>" & Range("D7").Value & "</wsse:Username>" sEnv = sEnv & " <wsse:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">" & Pwd & "</wsse:Password>" sEnv = sEnv & " </wsse:UsernameToken>" sEnv = sEnv & " </wsse:Security>" sEnv = sEnv & " </soapenv:Header>" sEnv = sEnv & " <soapenv:Body>" sEnv = sEnv & " <bm:getTransaction>" sEnv = sEnv & " <bm:transaction>" sEnv = sEnv & " <bm:id>" & Range("D9").Value & "</bm:id>" sEnv = sEnv & " </bm:transaction>" sEnv = sEnv & " </bm:getTransaction>" sEnv = sEnv & " </soapenv:Body>" sEnv = sEnv & "</soapenv:Envelope>" ' Send SOAP Request .send (sEnv) Resp.LoadXML Req.responseText End With If Resp Is Nothing Then MsgBox "No XML" End If With Resp .setProperty "SelectionNamespaces", "xmlns:bm=""http://xmlns.oracle.com/XXXCLD/commerce/ZZZYYY_PPP_1"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""" Dim strName As String If .SelectSingleNode("//bm:_customer_t_company_name") Is Nothing Then MsgBox "No Node" End If strName = .SelectSingleNode("//bm:_customer_t_company_name").Text MsgBox strName End With 'clean up code Set Req = Nothing Set Resp = Nothing Range("A1").Value = "DONE" End Sub

ChangeListener

当然,使用上面的代码,额外的属性是多余的:你可以做到

public void start(Stage primaryStage) {
    Pane root = new Pane();

    TextField enterText = new TextField();
    enterText.setFont(Font.font("SanSerif",20));

    text.bind(enterText.textProperty());

    text.addListener((obs, oldTextValue, newTextValue) -> 
        System.out.println("The new value is "+newTextValue));

    root.getChildren().addAll(enterText);
    Scene scene = new Scene(root,600,600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Text Entry");
    primaryStage.show();
}

但也许还有其他原因需要额外的财产。