2015-02-19 3 views
5

Ho un texfield in uno stadio:JavaFX Cambio textcolor di disabile campo di testo in CSS

@FXML 
private TextField tfAdUsername; 

tfAdUsername.setPromptText(userName); 
tfAdUsername.setDisable(true); 

Il textcolor è lightgray, voglio cambiarlo in nero:

.text-field:readonly { 
    -fx-background-color: #E0E0E2; 
    -fx-border-color: #94BBDA; 
    -fx-text-fill: #000000; 
} 

.text-field:disabled { 
    -fx-background-color: #E0E0E2; 
    -fx-border-color: #94BBDA; 
    -fx-text-fill: #000000; 
} 

Questo non lo fa cambia il colore del testo. Qual è la proprietà CSS corretta da usare?

risposta

9

Il motivo per cui il colore diventa grigio disabilitando è a causa del cambiamento di opacità. Prova ad aggiungere il seguente css al tuo campo di testo.

-fx-opacity: 1.0; 

Lavorare esempio (usando setStyle())

public class KeyStroke extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Pane root = new Pane(); 
     TextField textField = new TextField("Itachi"); 
     textField.setDisable(true); 
     textField.setStyle("-fx-opacity: 1.0;"); 
     root.getChildren().add(textField); 
     Scene scene = new Scene(root, 200, 200); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

Non fa differenza ....... – hsc

+0

Come lo hai aggiunto? – ItachiUchiha

+0

Poco dettaglio stavo cercando e ho dimenticato di usare il testo corretto: tfAdUsername.setPromptText (userName); modificato in tfAdUsername.setText (userName); – hsc

1

Per cambiare l'uso di opacità:

.text-input:disabled { 
-fx-opacity: 1.0; 
} 

nel file css.


Per modificare il TextColor utilizzato (dopo che ho letto la domanda correttamente So che questo non è stato chiesto.)

Dal foglio di stile modena.css:

.root {   
    /* Used for the inside of text boxes, password boxes, lists, trees, and 
* tables. See also -fx-text-inner-color, which should be used as the 
* -fx-text-fill value for text painted on top of backgrounds colored 
* with -fx-control-inner-background. 
*/ 
-fx-control-inner-background: derive(-fx-base,80%); 
/* Version of -fx-control-inner-background for alternative rows */ 
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%); 

/* One of these colors will be chosen based upon a ladder calculation 
* that uses the brightness of a background color. Instead of using these 
* colors directly as -fx-text-fill values, the sections in this file should 
* use a derived color to match the background in use. See also: 
* 
* -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color 
* -fx-text-background-color for text on top of -fx-background 
* -fx-text-inner-color for text on top of -fx-control-inner-color 
* -fx-selection-bar-text for text on top of -fx-selection-bar 
*/ 
} 
.root { 
    -fx-text-inner-color: #FF01F3; 
} 

Ad esempio:

@Override 
    public void start(Stage primaryStage) { 
    VBox root = new VBox(); 
    root.setAlignment(Pos.CENTER); 
    Button btn = new Button(); 
    TextField text= new TextField(); 
    btn.setText("Press me!"); 
    btn.setOnAction((ActionEvent event) -> { 
     text.setText("Goodbye World"); 
    }); 
    root.getChildren().addAll(btn, text); 
    Scene scene = new Scene(root, 300, 250); 
    scene.getStylesheets().addAll(getClass().getResource("css.css").toExternalForm()); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

E il colore del testo nel campo di testo sarà rosa? Viola?

Questo non è un modo per cambiare il colore per ogni campo di testo singolarmente, se lo si cambia per la radice, allora tutti i campi di testo useranno questo colore al posto del solito nero. Non so come cambiare il colore di un campo di testo in blu e di un altro nella stessa applicazione in rosso.