private void adjustPlayerCount() { int difficulty = 0; if (Config.getInstance().difficulty == Difficulty.HILL) { difficulty = 0; } else if (Config.getInstance().difficulty == Difficulty.MESA) { difficulty = 1; } else if (Config.getInstance().difficulty == Difficulty.PLATEAU) { difficulty = 2; } else if (Config.getInstance().difficulty == Difficulty.MOUNTAIN) { difficulty = 3; } int foodCount; if (difficulty == 0) { foodCount = 8; } else { foodCount = 4; } int energyCount; if (difficulty == 0) { energyCount = 4; } else { energyCount = 2; } for (int i = 0; i < Config.getInstance().maxPlayers; i++) { if (Config.getInstance().playerList[i] == null) { Config.getInstance().playerList[i] = new Player(i); Config.getInstance().playerList[i].changeStockOf(ResourceType.ENERGY, energyCount); Config.getInstance().playerList[i].changeStockOf(ResourceType.FOOD, foodCount); } } playerSlider.setMax(Config.getInstance().numOfPlayers); if (Config.getInstance().currentPlayer >= Config.getInstance().numOfPlayers) { changePlayer(0); } }
private void initSettingsPane() { trainTroopsSlider.setMin(0); trainTroopsSlider.setMax(Settings.MAX_TRAIN_TROOPS); trainTroopsSlider.setBlockIncrement(1); trainTroopsSlider .valueProperty() .addListener( (observable, oldValue, newValue) -> { trainTroopsSliderPreview.setText( String.format("%d", MainController.toInt(newValue.doubleValue()))); }); final Troop[] availableTroops = model.getAvailableTroops(); rax1ComboBox.getItems().addAll(availableTroops); rax2ComboBox.getItems().addAll(availableTroops); rax3ComboBox.getItems().addAll(availableTroops); rax4ComboBox.getItems().addAll(availableTroops); rax5ComboBox.getItems().addAll(availableTroops); rax6ComboBox.getItems().addAll(availableTroops); autoAttackComboBox.getItems().addAll(Attack.noStrategy()); autoAttackComboBox.getItems().addAll(Attack.getAvailableStrategies()); autoAttackComboBox.setValue(autoAttackComboBox.getItems().get(0)); final ChangeListener<String> intFieldListener = (observable, oldValue, newValue) -> { try { if (!newValue.isEmpty()) { Integer.parseInt(newValue); } } catch (final NumberFormatException e) { ((TextField) ((StringProperty) observable).getBean()).setText(oldValue); } }; goldField.textProperty().addListener(intFieldListener); elixirField.textProperty().addListener(intFieldListener); deField.textProperty().addListener(intFieldListener); maxThField.textProperty().addListener(intFieldListener); logLevelComboBox .getItems() .addAll( Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG, Level.INFO, Level.WARNING, Level.SEVERE); logLevelComboBox.setValue(logLevelComboBox.getItems().get(1)); updateUI(); }
/** * Verbindet ein Textfeld bidirektional mit einem Slider und sorgt für Validierung der Eingaben. * * @param textField Textfeld * @param slider Slider * @param maxValue Maximalwert für das Textfeld */ private void bindTextInputListenersAndSlider( final TextField textField, final Slider slider, final long maxValue) { final StringProperty textProperty = textField.textProperty(); // Eingaben auf Zahlen beschränken textField.addEventFilter( KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() { public void handle(final KeyEvent keyEvent) { if (!"0123456789".contains(keyEvent.getCharacter())) { keyEvent.consume(); } } }); textProperty.addListener( new ChangeListener<String>() { public void changed( final ObservableValue<? extends String> property, final String oldValue, final String newValue) { // Wert auf gültigen Bereich beschränken try { final long parsedNumber = Long.parseLong(newValue); if (parsedNumber < MIN_VALUE) { textProperty.setValue(getTwoDigitValue(MIN_VALUE)); } else if (parsedNumber > maxValue) { textProperty.setValue(getTwoDigitValue(maxValue)); } else if (newValue.length() > 2) { textProperty.setValue(getTwoDigitValue(parsedNumber)); } else { textProperty.setValue(newValue); } // Änderung an Zeit-Property durchstellen timeProperty.set(getValue()); } catch (final NumberFormatException e) { // alten Wert wieder setzen (sinnvoll bei Änderung per Copy & Paste) textProperty.setValue(oldValue); } } }); // bei Verlassen des Feldes sicherstellen, dass Wert zweistellig textField .focusedProperty() .addListener( new ChangeListener<Boolean>() { public void changed( final ObservableValue<? extends Boolean> property, final Boolean oldValue, final Boolean newValue) { if (Boolean.FALSE.equals(newValue)) { try { textProperty.setValue(getTwoDigitValue(Long.parseLong(textProperty.get()))); } catch (final NumberFormatException e) { textProperty.setValue(getTwoDigitValue(MIN_VALUE)); } } } }); // Textfeld mit Slider koppeln if (slider != null) { slider.setMax(maxValue); textProperty.bindBidirectional( slider.valueProperty(), new StringConverter<Number>() { public String toString(final Number number) { return getTwoDigitValue(number.longValue()); } public Number fromString(final String string) { return Long.parseLong(string); } }); } // auf Scroll-Ereignis im Textfeld reagieren textField.setOnScroll( new EventHandler<ScrollEvent>() { public void handle(final ScrollEvent event) { // Scrollen über Feld ohne Fokus hat keine Auswirkung if (!textField.isFocused()) { return; } final double change = event.getDeltaY() > 0 ? 1 : -1; slider.setValue(slider.getValue() + change); event.consume(); } }); }
@FXML private void initialize() { m_combo.setItems(Person.DEFAULT_DATA); m_combo.setConverter( new StringConverter<Person>() { @Override public String toString(Person p) { return p.getFirstName().get() + " " + p.getLastName().get() + " (" + p.getAge().get() + ")"; } @Override public Person fromString(String string) { for (Person p : m_combo.getItems()) { String current = p.getFirstName().get() + " " + p.getLastName().get() + " (" + p.getAge().get() + ")"; if (current.equals(string)) { return p; } } return null; } }); m_slider.setMin(0); m_slider.setMax(100); m_slider.setValue(50); m_spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100, 0, 1)); m_list.setItems(Person.DEFAULT_DATA); m_list.setCellFactory( new Callback<ListView<Person>, ListCell<Person>>() { @Override public ListCell<Person> call(ListView<Person> p) { return new ListCell<Person>() { @Override protected void updateItem(Person p1, boolean bln) { super.updateItem(p1, bln); if (p1 != null) { String current = p1.getFirstName().get() + " " + p1.getLastName().get() + " (" + p1.getAge().get() + ")"; setText(current); } } }; } }); }
protected void setControls(PropDesc... properties) { GridPane grid = new GridPane(); grid.getStyleClass().add("sample-control-grid"); grid.setVgap(10); grid.setHgap(10); int row = 0; for (PropDesc property : properties) { final PropDesc prop = property; Label propName = new Label(prop.name + ":"); propName.getStyleClass().add("sample-control-grid-prop-label"); GridPane.setConstraints(propName, 0, row); grid.getChildren().add(propName); if (prop.valueModel instanceof DoubleProperty) { final Label valueLabel = new Label(twoDp.format(prop.initialValue)); GridPane.setConstraints(valueLabel, 2, row); final Slider slider = new Slider(); slider.setMin(prop.min); slider.setMax(prop.max); slider.setValue(((Number) prop.initialValue).doubleValue()); GridPane.setConstraints(slider, 1, row); slider.setMaxWidth(Double.MAX_VALUE); slider .valueProperty() .addListener( new InvalidationListener() { @Override public void invalidated(Observable ov) { set(prop.valueModel, slider.getValue()); valueLabel.setText(twoDp.format(slider.getValue())); } }); grid.getChildren().addAll(slider, valueLabel); } else { // if (prop.property.getType() == Color.class || prop.property.getType() == // Paint.class) { // FIXME we assume anything that isn't a double property is now a colour property final Rectangle colorRect = new Rectangle(20, 20, (Color) prop.initialValue); colorRect.setStroke(Color.GRAY); final Label valueLabel = new Label(formatWebColor((Color) prop.initialValue)); valueLabel.setGraphic(colorRect); valueLabel.setContentDisplay(ContentDisplay.LEFT); GridPane.setConstraints(valueLabel, 2, row); final ColorPicker colorPicker = new ColorPicker(); GridPane.setConstraints(colorPicker, 1, row); colorPicker .getColor() .addListener( new InvalidationListener() { @Override public void invalidated(Observable valueModel) { Color c = colorPicker.getColor().get(); set(prop.valueModel, c); valueLabel.setText(formatWebColor(c)); colorRect.setFill(c); } }); grid.getChildren().addAll(colorPicker, valueLabel); } row++; } controls = grid; }