2017-08-10 65 views
0

好的,继承人我的情况。当编辑器为空时,我需要禁用这两个微调按钮,这是我需要完成这个“自定义”组件的最后一部分。这里是我的SSCCE。JavaFX Spinner禁用空编辑器上的按钮

重点丢失时:默认值设置为零,文本更新。

它只接受小数点后两位的十进制数值,只表示接受金钱或百分比值。

没有别的要补充的。

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Spinner; 
import javafx.scene.control.SpinnerValueFactory; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class test extends Application{ 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     VBox v = new VBox(); 
     v.setPadding(new Insets(20)); 
     Spinner<Double> spinner = new Spinner<>(); 
     spinner.setEditable(true); 
     Button dummy = new Button("dummy focus"); 
     v.getChildren().addAll(spinner,dummy); 


     //----------------------------------HERE IS EVERYTHING RELATED TO THE SPINNER--------------------------------------------- 
     spinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 100)); 
     spinner.getValueFactory().setValue(0.0); 
     spinner.getEditor().textProperty().addListener((obs,old,gnu)->{ 

      if(gnu.isEmpty()) { 
       System.out.println("empty, buttons should be disabled here, they will be disabled after this "); 
       spinner.getValueFactory().setValue(0.0); 
       return; 
      } 
      System.out.println("enabling buttons"); 
      if(!gnu.matches("^\\d*\\.?\\d*$")) { 
       try { 
        spinner.getEditor().setText(old); 
        spinner.getValueFactory().setValue(Double.parseDouble(old)); 
       }catch (NumberFormatException e) { 
        System.out.println("invalid string, previous value was empty, no biggie you are safe: Current value : "+spinner.getValueFactory().getValue()); 
       } 
      } else { 
       if((Double.parseDouble(gnu)*100)%1!=0) { 
        spinner.getEditor().setText(old); 
        spinner.getValueFactory().setValue(Double.parseDouble(old));      
       } 
       /* 
       * You can use this to validate inside a range, for example. PERCENTAGES : 0 ~ 100 
       * 
       double val = Double.parseDouble(gnu)*100; 
       if(val%1!=0 || val>10000 || val<0) { 
        spinner.getEditor().setText(old); 
        spinner.getValueFactory().setValue(Double.parseDouble(old));      
       } 
       */ 
      } 
     }); 
     spinner.getEditor().setOnKeyPressed(e->{    
      switch (e.getCode()) { 
       case UP: 
        spinner.increment(1); 
        break; 
       case DOWN: 
        spinner.decrement(1); 
        break; 

       default: 
        break; 
      } 
     }); 
     spinner.setOnScroll(e->{ 
      if(e.getDeltaY()>0) 
       spinner.increment(1); 
      else 
       spinner.decrement(1); 
     }); 
     spinner.getEditor().focusedProperty().addListener((obs,old,niu)->{ 
      if(!niu && spinner.getEditor().getText().isEmpty()) { 
       spinner.getEditor().setText("0"); 
       spinner.getValueFactory().setValue(0.0); 
      } 
     });  
     //----------------------------------------------------------------------------------------------------------------------------------------- 




     Scene sc = new Scene(v); 
     primaryStage.setScene(sc); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 

编辑

这也恰好与按键和滚动事件。

回答

1

恐怕你不能仅禁用微调器的按钮。但是在Editor(实际上是TextField)之后设置该值是空的呢?通过使用这种解决方案,您不会在单击按钮后发生任何异常 - 值仅从0增加。我修改了一下你的gnu.isEmpty()代码。

if(gnu.isEmpty()) { 
    System.out.println("empty, buttons should be disabled here, they will be disabled after this "); 
    double valueToSet = 0.0; 
    spinner.getValueFactory().setValue(valueToSet); 
    Platform.runLater(() -> spinner.getEditor().setText(Double.toString(valueToSet))); 
    return; 
} 

另一件事是,你的代码允许把“0”作为第一个数字,即使之后有另一个号码。检查代码,就可以解决问题(带全掉它if/else语句开始if(!gnu.matches("^\\d*\\.?\\d*$"))):

if (!isDouble(gnu)) { 
    gnu = old; 
} 
spinner.getEditor().setText(gnu); 

isDouble是一个方法:

private boolean isDouble(String string) { 
    boolean startsWithZero = 
      string.startsWith("0") && 
        (string.length() > 1) && 
        (!string.startsWith("0.")); 
    boolean minusZeroCondition = 
      string.startsWith("-0") && 
        (string.length() > 2) && 
        (!string.startsWith("-0.")); 
    boolean containsTypeSpecificLetters = 
      Pattern.matches(".*[a-zA-Z].*", string); 
    boolean isEmpty = string.equals(""); 
    boolean isMinus = string.equals("-"); 
    try { 
     Double.parseDouble(string); 
     return !(startsWithZero || minusZeroCondition || containsTypeSpecificLetters); 
    } catch (IllegalArgumentException exception) { 
     return isEmpty || isMinus; 
    } 
} 
+0

是啊,这就是我最后做。现在我在开始时有一个小小的问题,看起来很烦人,但我认为这是另一个问题。 BTW spinner.getEditor()。setText(“0”)就足够了。 –

+0

@WesosdeQueso我编辑了我的答案,检查它。 – notmyf4ulty

+0

我已经用我自己的方式解决了它。稍后将发布我的版本。也许我错过了一些东西。 –