【发布时间】:2016-12-16 13:43:38
【问题描述】:
我曾经使用JComboBox 在 Swing 中像这样选择一个字节。
public synchronized void callPresetButtonActionPerformed(
java.awt.event.ActionEvent evt) {
byte _preset = (byte)getPresetcomboBox.getSelectedItem();
try {
something=presetNo[_preset-1];
byte[] command = {(byte) startTx, address, byteOne, goPreset, 0x00, something, endTx, 0x0F};
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Byte[] preset = { 1, 2, 3, 4, 5};
现在我正在转向 JavaFX,并且我正在尝试做同样的事情。
@FXML
public void setPresetButton (ActionEvent event) {
byte _preset = (Byte)setPresetComboBox.getSelectedItem();
try {
something=presetNo[_preset-1];
byte[] command = {(byte) startTx, address, byteOne, setPreset, 0x00, something, endTx, 0x0F};
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
Byte[] preset = { 1, 2, 3, 4, 5};
我收到一个错误:The method getSelectedItem() is undefined for the type ComboBox<Byte>。
我想我理解错误的含义,但是如何在 JavaFX 中解决它?
【问题讨论】:
-
错误信息告诉你解决方法:
ComboBox没有getSelectedItem()方法。您可以通过其选择模型:setPresetComboBox.getSelectionModel().getSelectedItem();或通过获取值:setPresetComboBox.getValue()来获取所选项目。 -
好吧,事实证明我根本不理解这个错误。作为答案发布,我会接受。谢谢