1. Add items to ChoiceBox
choiceType.getItems().add("solid");
2. Select default item
choiceType.getSelectionModel().selectFirst(); choiceType.getSelectionModel().selectLast(); choiceType.getSelectionModel().selectNext(); choiceType.getSelectionModel().selectPrevious(); choiceType.getSelectionModel().select(0);
3. Get selected value
choiceType.getSelectionModel().getSelectedItem() choiceType.getSelectionModel().getSelectedIndex()
Full Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @FXML private ChoiceBox<String> choiceType; final String[] boxType = { "solid", "border", "simple" }; @Override public void initialize(URL url, ResourceBundle rb) { for( int i = 0; i < boxType.length; i++ ) { choiceType.getItems().add(boxType[i]); } choiceType.getSelectionModel().selectFirst(); } @FXML private void handleOnClickAction(ActionEvent event) { if( choiceType.getSelectionModel().getSelectedItem().equals("solid") ) { System.out.println("solid selected"); } if( choiceType.getSelectionModel().getSelectedItem().equals("border") ) { System.out.println("border selected"); } if( choiceType.getSelectionModel().getSelectedItem().equals("simple") ) { System.out.println("simple selected"); } } |
댓글 없음:
댓글 쓰기