2016-04-15 71 views
0

我有一个javafx类ShowBytes其中包含字节a-z。每个字节的定义如下Classf和Combobox之间的Javafx连接

public static final byte A = (byte) 0x00; 

我想填充类中的字节列表并将它们显示在组合框中。有没有什么办法可以在类ShowBytes链接组合框项的字节,因此,如果我在下拉列表中选择A它仍然代表字节0

+0

你到底想干什么?在'ComboBox'中显示变量名,但保持项类型为'Byte'?这些价值是否独一无二?你为什么想这样做?使用'Byte'作为项目类型的好处是什么,而不是在使用项目时简单地将项目转换为'byte'? – fabian

+0

这些字节将通过串行通信发送到开发板。字节将被设备解释为命令。是否有可能有一个名为getbytes(String Bytename)的函数,它将从类中返回字节。 – tashtoons

+0

你不能修改那个类并把数据放在一个更合适的数据结构中吗? – fabian

回答

1

你可以创建一个包含字节类和String和覆盖toString方法返回字符串。如果需要,您可以从该类别获得价值

例如,

ObservableList<NamedByteValue> bytes = FXCollections.observableArrayList(); 

// just filling it with some sample values here 
for (char c = 'A'; c <= 'Z'; c++) { 
    bytes.add(new NamedByteValue((byte) (c - 'A'), Character.toString(c))); 
} 

ComboBox<NamedByteValue> comboBox = new ComboBox<>(bytes); 
comboBox.valueProperty().addListener((observable, oldValue, newValue) -> System.out.println(newValue.getValue())); 
public static class NamedByteValue { 

    private final byte value; 
    private final String name; 

    public NamedByteValue(byte value, String name) { 
     this.value = value; 
     this.name = name; 
    } 

    public byte getValue() { 
     return value; 
    } 

    public String getName() { 
     return name; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 

} 
+0

谢谢你。这是它 – tashtoons