2013-01-03 89 views
4

我只是試圖綁定一個Integer和一個String屬性。經過一番google搜索這應該是可能有兩個提供的方法之一:不同屬性的雙向綁定

  1. 公共靜態無效bindBidirectional(物業stringProperty,
    物業otherProperty,字符串轉換器)

  2. 公共靜態無效bindBidirectional(物業stringProperty ,
    屬性otherProperty,格式java.text.Format子類)

不幸第似乎並不適合我。我究竟做錯了什麼?

import java.text.Format; 

import javafx.beans.binding.Bindings; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.util.converter.IntegerStringConverter; 

public class BiderectionalBinding { 

    public static void main(String[] args) { 
     SimpleIntegerProperty intProp = new SimpleIntegerProperty(); 
     SimpleStringProperty textProp = new SimpleStringProperty(); 

     Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter()); 

     intProp.set(2); 
     System.out.println(textProp); 

     textProp.set("8"); 
     System.out.println(intProp);  
    } 
} 
+3

如果您的問題已得到解決。你應該接受一個答案。 –

回答

0

我認爲這是一個錯誤。反正你可以像解決方法:

StringConverter sc = new IntegerStringConverter(); 
Bindings.bindBidirectional(textProp, intProp, sc); 
2

我試着在Eclipse中的代碼,不得不投的轉換器。然後一切看起來確定:

public class BiderectionalBinding { 

    public static void main(String[] args) { 
     SimpleIntegerProperty intProp = new SimpleIntegerProperty(); 
     SimpleStringProperty textProp = new SimpleStringProperty(); 
     StringConverter<? extends Number> converter = new IntegerStringConverter(); 

     Bindings.bindBidirectional(textProp, intProp, (StringConverter<Number>)converter); 

     intProp.set(2); 
     System.out.println(textProp); 

     textProp.set("8"); 
     System.out.println(intProp);  
    } 
} 

的輸出是:

StringProperty [值:2]

IntegerProperty [值:8]

+0

完美,謝謝。我還在https://forums.oracle.com/forums/message.jspa?messageID=10773242 – dethlef1

+0

上獲得了其他有用答案有關JavaFX Property API的更多信息,請閱讀http://docs.oracle.com/javafx/2/ api/javafx/beans/binding/Bindings.html –

3

我有一個類似的問題。我試圖將字符串轉換爲文件對象並返回。但是我使用了Bindings.bindBidirectional(...,...,java.text.Format)。從字符串到文件的轉換按預期工作,但在另一個方向上結果爲空。我用你的例子試了一下,結果一樣!我認爲這是在綁定機制的一個錯誤,也許我的實現java.text.Format子是錯誤的..

package de.ludwig.binding.model; 

import java.text.FieldPosition; 
import java.text.Format; 
import java.text.ParsePosition; 

import javafx.beans.binding.Bindings; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 

public class BidirectionalBinding { 

    public static void main(String[] args) { 
     SimpleIntegerProperty intProp = new SimpleIntegerProperty(); 
     SimpleStringProperty textProp = new SimpleStringProperty(); 

     Bindings.bindBidirectional(textProp, intProp, new Format() { 

      @Override 
      public StringBuffer format(Object obj, StringBuffer toAppendTo, 
        FieldPosition pos) { 
       return toAppendTo.append(obj.toString()); 
      } 

      @Override 
      public Object parseObject(String source, ParsePosition pos) { 
       return Integer.parseInt(source); 
      } 

     }); 

     intProp.set(2); 
     System.out.println(textProp); 

     textProp.set("8"); 
     System.out.println(intProp);  
    } 
} 

得到的東西如預期工作的唯一方法是字符串轉換實施的建議亨德里克·埃貝斯。謝謝你的提示!

+1

爲什麼不把你的代碼添加到你的代碼中,以便這個答案對其他人有用。其他方面,如果有人正在尋找同樣的問題,這不是一個好的答案。 – BBdev

14

類型混亂

Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter()); 

的簡單的事情應該是:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());