我正在構建一個具有幾個單選按鈕的應用程序,根據用戶的選擇我必須做某件事或另一件事。現在我使用了一個OptionGroup來創建單選按鈕,但我似乎無法理解如何區分單選按鈕。在純java中,它非常簡單,因爲我會創建每個單選按鈕,然後使用ButtonGroup對象將它們組合在一起,但在vaadin中我真的不知道。文件是一樣糟糕,所以我有點卡住了。這是給你一些代碼:區分vaadin中的單選按鈕
public class ConverterComponent extends CustomComponent{
private TextField name2 = new TextField();
private OptionGroup single;
private TextField userInput;
private TextField result;
private Button submit;
private Button reset;
private static final String conversion1 = "Km to miles";
private static final String conversion2 = "Miles to Km";
private static final long serialVersionUID = 1L;
public ConverterComponent(){
submit = new Button("Submit");
reset = new Button("Reset");
result = new TextField();
userInput = new TextField();
result.setEnabled(false);
result.setVisible(false);
userInput.setVisible(false);
reset.setVisible(false);
submit.setVisible(false);
single = new OptionGroup("Select the conversion");
single.addItems(conversion1, conversion2);
reset.addClickListener(new Button.ClickListener(){
@Override
public void buttonClick(ClickEvent event){
clearFields();
getResult().setVisible(false);
}
});
submit.addClickListener(new Button.ClickListener(){
@Override
public void buttonClick(ClickEvent event) {
getResult().setVisible(true);
//NEED TO KNOW WHICH RADIO BUTTON HAS BEEM CLICKED SO THAT i CAN DECIDE WHICH CONVERSION TO USE
}
});
single.addValueChangeListener(new Property.ValueChangeListener(){
@Override
public void valueChange(ValueChangeEvent event) {
clearFields();
/*System.out.println("You chose: " + event.getProperty().getValue().toString() + "\n");
System.out.println("other line " + event.getProperty() + "\n" + " id is " + single.getId() + " size " + single.size());*/
//System.out.println("event is " + event.getProperty().getValue());
switch(event.getProperty().getValue().toString()){
case conversion1:
System.out.println(conversion1);
break;
case conversion2:
System.out.println(conversion2);
break;
}
displayFields();
}
});
}
public OptionGroup getRadioButtons(){
return single;
}
public TextField getResult(){
return result;
}
public TextField getUserInput(){
return userInput;
}
public void displayFields(){
//getResult().setVisible(true);
getUserInput().setVisible(true);
getResetButton().setVisible(true);
getSubmitButton().setVisible(true);
}
public Button getResetButton(){
return reset;
}
public Button getSubmitButton(){
return submit;
}
public void clearFields(){
getResult().setValue("");
getUserInput().setValue("");
}
public void validateInputs(){
}
}
記住,我要在將來添加更多的選擇,但我嘗試去,當用戶選擇一個單選按鈕,無論其中,他將得到兩個輸入框,一個用於輸入,另一個 - 只讀 - 顯示轉換。重點是,當做出選擇並顯示輸入框時,我必須知道用戶做了什麼選擇,因爲我必須能夠抓住輸入並正確轉換輸入。在上面的代碼中,我顯示用戶的選擇,但我沒有任何可以檢查或比較的選項。理想情況下,我想要做的是: - 點擊第一個單選按鈕 - 確定哪個單選按鈕已被選中,以便我知道要使用哪個轉換。
你seenn這個實況? https://vaadin.com/docs/-/part/framework/components/components-optiongroup.html –