2016-12-08 2 views
2

위쪽/아래쪽 화살표 버튼을 클릭하여 누르고 있으면 Spinner 업데이트 속도가 느려집니다. 변경 속도를 높이는 방법이 있습니까?마우스 버튼을 길게 클릭하면 JavaFX Spinner가 느려집니다.

클릭 할 때 마우스를 클릭하면 회 전자 값이 클릭 할 때마다 빠르게 변경됩니다. 또한 각 키를 누를 때마다 키보드의 위/아래 화살표를 사용하거나 위/아래 화살표 키를 누른 채로 있으면 빠르게 바뀝니다. 화살표 버튼을 클릭 한 상태에서 값을 빠르게 변경하고 싶습니다.

누구나 할 수있는 방법을 알고 있습니까?

답변

0

내가 아래로 마우스를 누른 상태에서 회 전자의 속도를 감소 파비안 약간의 답을 수정 : 따라서 유일한 방법은 빠른 속도로 업데이트를 트리거하는 이벤트 필터를 사용하고 반사하지 않고이 작업을 수행합니다 :

private int currentFrame = 0; 
private int previousFrame = 0;   

@Override 
public void handle(long now) 
{ 
    if (now - startTimestamp >= initialDelay) 
     { 
     // Single or holded mouse click 
     if (currentFrame == previousFrame || currentFrame % 10 == 0) 
     { 
      if (increment) 
      { 
       spinner.increment(); 
      } 
      else 
      { 
       spinner.decrement(); 
      } 
     } 
    } 

    ++currentFrame; 
} 

그리고 타이머를 중지 한 후

우리가 다시 previousFrame을 조정 :

public void stop() 
{ 
    previousFrame = currentFrame; 

    [...] 
} 
2

SpinnerSkinSpinnerBehavior은 750ms마다 업데이트를 트리거합니다. 죄송 합니다만,이 동작을 설정/수정 하시려면 private 회원에 대한 액세스를 거부하지 않고서는이 방법을 사용할 수 없습니다.

private static final PseudoClass PRESSED = PseudoClass.getPseudoClass("pressed"); 

@Override 
public void start(Stage primaryStage) { 
    Spinner<Integer> spinner = new Spinner(Integer.MIN_VALUE, Integer.MAX_VALUE, 0); 

    class IncrementHandler implements EventHandler<MouseEvent> { 

     private Spinner spinner; 
     private boolean increment; 
     private long startTimestamp; 

     private static final long DELAY = 1000l * 1000L * 750L; // 0.75 sec 
     private Node button; 

     private final AnimationTimer timer = new AnimationTimer() { 

      @Override 
      public void handle(long now) { 
       if (now - startTimestamp >= DELAY) { 
        // trigger updates every frame once the initial delay is over 
        if (increment) { 
         spinner.increment(); 
        } else { 
         spinner.decrement(); 
        } 
       } 
      } 
     }; 

     @Override 
     public void handle(MouseEvent event) { 
      if (event.getButton() == MouseButton.PRIMARY) { 
       Spinner source = (Spinner) event.getSource(); 
       Node node = event.getPickResult().getIntersectedNode(); 

       Boolean increment = null; 
       // find which kind of button was pressed and if one was pressed 
       while (increment == null && node != source) { 
        if (node.getStyleClass().contains("increment-arrow-button")) { 
         increment = Boolean.TRUE; 
        } else if (node.getStyleClass().contains("decrement-arrow-button")) { 
         increment = Boolean.FALSE; 
        } else { 
         node = node.getParent(); 
        } 
       } 
       if (increment != null) { 
        event.consume(); 
        source.requestFocus(); 
        spinner = source; 
        this.increment = increment; 

        // timestamp to calculate the delay 
        startTimestamp = System.nanoTime(); 

        button = node; 

        // update for css styling 
        node.pseudoClassStateChanged(PRESSED, true); 

        // first value update 
        timer.handle(startTimestamp + DELAY); 

        // trigger timer for more updates later 
        timer.start(); 
       } 
      } 
     } 

     public void stop() { 
      timer.stop(); 
      button.pseudoClassStateChanged(PRESSED, false); 
      button = null; 
      spinner = null; 
     } 
    } 

    IncrementHandler handler = new IncrementHandler(); 
    spinner.addEventFilter(MouseEvent.MOUSE_PRESSED, handler); 
    spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> { 
     if (evt.getButton() == MouseButton.PRIMARY) { 
      handler.stop(); 
     } 
    }); 

    Scene scene = new Scene(spinner); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

감사 파비안! 마우스 버튼을 누른 채로 증가/감소 속도를 줄이기 위해 소스 코드를 조금 수정해야했지만, 응답 시간이 많이 걸렸습니다! – Nighty

1

파비안의 대답에 작은 개선. MOUSE_RELEASED addEventerFilter에 다음 mod를 지정하면 스피너와 관련된 텍스트 필드를 클릭 할 때 발생하는 NullPointerException이 중지됩니다. 건배 Fabian!

spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> { 
     Node node = evt.getPickResult().getIntersectedNode(); 
     if (node.getStyleClass().contains("increment-arrow-button") || 
      node.getStyleClass().contains("decrement-arrow-button")) { 
       if (evt.getButton() == MouseButton.PRIMARY) { 
        handler.stop(); 
       } 
     } 
    }); 
+0

나는 당신을 예술이라고;) 이것 주셔서 감사합니다. –

0

업데이트 속도를 변경하는 대신 업데이트 당 값이 증가하거나 감소하는 양을 조정할 수도 있습니다.

SpinnerValueFactory.IntegerSpinnerValueFactory intFactory = 
(SpinnerValueFactory.IntegerSpinnerValueFactory) spinner.getValueFactory(); 
intFactory.setAmountToStepBy(100); 

참조 : http://news.kynosarges.org/2016/10/28/javafx-spinner-for-numbers/

관련 문제