2017-09-17 94 views
0

我一直試圖禁用javafx.scene.control.ColorPicker而不在我的用戶界面中調暗它。這意味着我希望它看起來像啓用(啓用外觀),但只是不響應任何命令,也沒有顯示鼠標點擊(實際上已禁用)的顏色表。根據系統狀態,這種行爲可以與正常行爲互換(即,有時候顏色選擇器將按照正常行爲)。如何讓JavaFX ColorPicker在沒有模糊外觀的情況下被禁用?

我已經嘗試了一些選項,但似乎沒有工作,具體如下:

1.使用setEditable(boolean)

myColorPicker.setEditable(false); 

這是行不通的,顏色選擇器中仍然可編輯。

2.使用setDisable(boolean)setOpacity(double)在一起:

myColorPicker.setDisable(true); 
myColorPicker.setOpacity(1.0f); 

這使得顏色選擇器實際上不可編輯,並將得到的顏色選擇器顯得有些不是僅僅使用setDisable(true)少變暗,但仍然沒有appearence啓用顏色選擇器。

3.重寫onMouseClick()onMousePressed()onMouseReleased()與空實現:

myColorPicker.setOnMouseClicked(new EventHandler <MouseEvent>() { 
    public void handle(MouseEvent event) { 
     System.out.println("Mouse clicked."); 
    } 
}); 

myColorPicker.setOnMousePressed(new EventHandler <MouseEvent>() { 
    public void handle(MouseEvent event) { 
     System.out.println("Mouse pressed."); 
    } 
}); 

myColorPicker.setOnMouseReleased(new EventHandler <MouseEvent>() { 
    public void handle(MouseEvent event) { 
     System.out.println("Mouse released."); 
    } 
}); 

上述方法印刷的我的控制檯上的對應的消息,但顏色選擇器仍響應鼠標點擊(表示顏色表和允許選擇一種新顏色)。還嘗試覆蓋setOnAction(EventHandler<ActionEvent>),但效果相同(除了點擊顏色選擇器時沒有控制檯打印)。

這是我FXML的摘錄:

<VBox fx:controller="mypackage.ElementConfigWidget" 
    xmlns:fx="http://javafx.com/fxml" fx:id="root" styleClass="elementConfigPane"> 
    <HBox id="elementInfo"> 
     (...) 
     <ColorPicker fx:id="elementColor" styleClass="elementColor" /> 
    </HBox> 
(...) 
</VBox> 

這是我的CSS exerpt:

.elementColor { 
    -fx-cursor: hand; 
    -fx-background-color: #fff; 
    -fx-focus-color: transparent; 
    -fx-faint-focus-color: transparent; 
    -fx-pref-width: 50.0; 
} 

其實我預計setEditable(boolean)解決我的問題,通過保持元件appearence和忽視輸入操作。我錯過了什麼?

非常感謝!

回答

1

我管理的一些CSS 。還有,你需要setDisable(真),不透明度爲實現這一目標,以1.0

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.ColorPicker; 
import javafx.stage.Stage; 

public class Main extends Application { 

    public static void main(String[] args) { 

     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     ColorPicker myColorPicker = new ColorPicker(); 

     myColorPicker.setDisable(true); 
     myColorPicker.setOpacity(1.0); 

     Scene s = new Scene(myColorPicker); 
     s.getStylesheets().add(this.getClass().getResource("test.css").toExternalForm()); 

     primaryStage.setScene(s); 
     primaryStage.show(); 
    } 

} 

而且test.css

.color-picker > .label:disabled { 
    -fx-opacity : 1.0; 
} 
+0

太好了! CSS的事情做到了。 :) –

相關問題