12

Hello i am using JavaFx to make an application. i have a small png picture that i want to add to the right side of my textField.

it is not a problem to add the picture to the frame of the textField but for some reason i cannot move the picture to any possition (which means that it does not move from the starting possition - which is left)

my code is the following:

#textField {
    -fx-background-image:url('apr.png');
    -fx-background-repeat: no-repeat;
    -fx-background-image-position:right;
}

I have also tried with Center yet that did not work either.

dokaspar
  • 8,186
  • 14
  • 70
  • 98
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

3 Answers3

17

As Marek points out in his answer you have the css attribute id wrong, you need to use -fx-background-position: right center;

Here is a short example which demonstrates adding a picture to the right side of a TextField using CSS:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TextFieldCssSample extends Application {
  @Override public void start(Stage stage) throws Exception {
    TextField textField = new TextField();
    textField.setId("textField");
    StackPane layout = new StackPane();
    layout.getChildren().addAll(textField);
    layout.getStylesheets().add(this.getClass().getResource("textfield.css").toExternalForm());
    stage.setScene(new Scene(layout));
    stage.show();
  }
  public static void main(String[] args) { launch(args); }
}

Css File:

/* textfield.css 
   place in same location as TextFieldCssSample.java and ensure build system copies it to output directory
   image used courtesy of creative commons attribution license: http://www.rockettheme.com/blog/design/1658-free-halloween-icon-pack1
*/
.root {
  -fx-padding: 15; 
  -fx-background-color: cornsilk;
}

#textField {
  -fx-background-image:url('http://icons.iconarchive.com/icons/rockettheme/halloween/32/pumpkin-icon.png');
  -fx-background-repeat: no-repeat;
  -fx-background-position: right center;
  -fx-font-size: 20;
}

Sample output:

text field background position center right

If my png image is a local file in my jar file, how will I access or refer to it?

According to the uri section of the css reference:

"address can be an absolute URI . . . or relative to the location of the CSS file."

For example

  • a) Put the css and image files in the same location in the jar file and reference with just url('pumpkin-icon.png'); OR
  • b) Put the image files in an images directory underneath the directory holding the css, and reference like url('images/pumpkin-icon.png'); OR
  • c) Put the image files in an images directory at the root of your jar, and reference like url('/images/pumpkin-icon.png');

Do not use a relative reference which uses a .. parent specifier e.g. ../images/pumpkin-icon.png as, although it works for a disk file, the .. specifier is not a valid jar protocol path and will not extract a file from a jar.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • If my png image is a local file in my jar file, how will I access or refer to it? – likejudo Jun 14 '13 at 03:37
  • 1
    @likejiujitsu Updated answer to answer your question. Although it's related, I think it's best to ask new question as questions rather than comments. – jewelsea Jun 14 '13 at 05:43
  • if I set it in Java code, it fails ` field.setStyle("-fx-border-color: red; -fx-background-image:url('/resources/errorIcon.png'); -fx-background-repeat: no-repeat; -fx-background-position: right center;"); ` resources is a folder under root. the Java file is under the package for the source which is another folder under root. – likejudo Jun 14 '13 at 15:06
  • even when I copy the image to the same folder as the Java file, it fails ` field.setStyle("-fx-border-color: red; -fx-background-image:url('errorIcon.png'); -fx-background-repeat: no-repeat; -fx-background-position: right center;"); ` – likejudo Jun 14 '13 at 15:24
  • asked it here as a new question http://stackoverflow.com/questions/17113887/why-cant-this-css-statement-in-java-code-find-this-image-file – likejudo Jun 14 '13 at 17:15
5

Instead of

-fx-background-image-position:right;

use

-fx-background-position: right center;
Marek
  • 51
  • 1
  • 1
  • Thanks Marek - this is correct. I updated the sample code in my original solution to use the css attribute as you suggest. – jewelsea Feb 25 '13 at 19:37
-7

copy this html and follow me :

<div class="textBoxWithImage">
  <img src="YOUR IMG URL" class="textBoxWithImage_img"/>
  <textarea id="textField"></textarea> // Change it with your textField !
</div>

fill image url on img tag above and then use this stylesheet!:

.textBoxWithImage{
 position:relative;
 overflow:hidden;
}
.textBoxWithImage_img{
 position:absolute;
 top:0px;
 right:0px;
 z-index:1;
}
#textField{
 background:transparent;
 position:relative;
 top:0px;
 left:0px;
 z-index:2;
}

Good luck friend !

Michael Antonius
  • 942
  • 2
  • 11
  • 20