17

I want to change the color of a Pane which I get as a String from user. How can I set this String as a background color in my pane?

Code:

colorField.setOnKeyTyped(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent t) {
        color = colorField.getText();
    }
});
Neuron
  • 5,141
  • 5
  • 38
  • 59
maryam
  • 387
  • 2
  • 3
  • 10
  • 1
    I understand that you are beginner, though you should show us your effort. How the code looks like so far? Have you accomplished changing the color of pane to fixed value? Was you able to take the value in textfield that the user entered? – Uluk Biy Apr 03 '14 at 14:50
  • yeah,that is right,I am beginer.I want to build a component like this: (http://www.uifaces.com) and I try to solve my problem myself, but I have a lot of problems:(.I dont know how to set the string color to pane!!!and this is part of my code till now: colorField.setOnKeyTyped(new EventHandler() { @Override public void handle(KeyEvent t) { color = colorField.getText(); } }); – maryam Apr 03 '14 at 20:28
  • If you don't know where to start, try [here](http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/index.html); and [here](http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/apply-css.htm#CHDGHCDG) for this particular question. [Here](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html) is the reference page for JavaFX CSS, and the Javadocs for [Pane](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html) and [Region](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html) will also help. – James_D Apr 03 '14 at 22:53

1 Answers1

28

If you really just want to know how to accomplish that particular thing, I'd suggest the following:

Set the Nodes' CSS like this, using the hexacolor that was entered by the user:

String enteredByUser = "abcdef";
yournode.setStyle("-fx-background-color: #" + enteredByUser);

If you want to know more please be more specific with you questions and provide some code samples.

Since you tagged this question with 'javafx-8' i'll provide that code example as well(only works in javafx 8):

yournode.setBackground(new Background(new BackgroundFill(Color.web("#" + enteredByUser), CornerRadii.EMPTY, Insets.EMPTY)));

Hope it helps, Laurenz

int lawl is over 9000
  • 977
  • 1
  • 15
  • 25
  • Thank you for your answer,I try your first answer,but it does not work,it cause warning:(WARNING: com.sun.javafx.css.parser.CSSParser term CSS Error parsing in-line style '-fx-background-color:#' from javafx.scene.Node$22@1564f4: Unexpected token '#' at [1,21]),does it have another way? – maryam Apr 04 '14 at 14:44
  • 1
    @maryam, it is because the "enteredByUser" string is empty while setting the style. – Uluk Biy Apr 04 '14 at 18:36
  • right the String represents the entered value by the user – int lawl is over 9000 Apr 04 '14 at 19:01