is there a way to set an Image (like setGraphic() for Button) for a ComboBox?
file = new File(IMG_DIR + "load.png");
BackgroundImage bg = new BackgroundImage(new Image(file.toURI().toString()),BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
BackgroundSize.DEFAULT);
comboBox.setBackground(new Background(bg));
That's what i tried, but it only results in a black background.
As comparison, what i do with my buttons:
File file = new File(IMG_DIR + "save.png");
ImageView imageView = new ImageView(new Image(file.toURI().toString()));
buttonSave.setGraphic(imageView);
what it currently looks like:
I want a load image on the ComboBox positioned like the images on the Buttons. Is that possible?
Thanks!
Related
I'm trying to add a background image to my JavaFX 3D program, but it just translates the ImageView instead of setting it as the background. What am I missing?
I create the ImageView, and then add it to a separate Group object in the start method. The choiceLayout is another Group object.
Image image = new Image(Program.class.getResourceAsStream("picture.jpg"));
ImageView view = new ImageView(image);
view.setPreserveRatio(true);
view.getTransforms().add(new Translate(-image.getWidth() / 2, -image.getHeight() / 2, 800));
Group bg = new Group();
bg.getChildren().add(choiceLayout);
bg.getChildren().add(view);
Scene choiceScene = new Scene(bg, 1024, 768, true);
I want add white border around ImageView.
One solution is to wrap the images in a Button.
And I try use StackPane:
StackPane stackPaneforImageActivity = new StackPane();
Image activityImage = new Image(file.toURI().toString());
ImageView imv = new ImageView(activityImage);
newActivityHBox.getChildren().add(stackPaneforImageActivity);
stackPaneforImageActivity.getChildren().add(imv);
stackPaneforImageActivity.setPadding(new Insets(10));
stackPaneforImageActivity.setStyle("-fx-border-color:white;-fx-background-color: black;");
imv.setFitHeight(30);
imv.setFitWidth(30);
But
Are there other solutions?
But the image is outside the StackPane. Why?
The title says it all, I guess.
How to set a .jpg (or any other supported image format) image to a Scene background?
I somewhat achieved this by using a HBox, an Image and an ImageView, like this:
String url = ...
HBox box= new HBox();
Image x = new Image(url);
ImageView iv = new ImageView(x);
box.getChildren().add(iv);
box.setVisible(true);
Then I add that box to the Scene first, and everything else afterwards.
I'm not complaining about that piece of code - it works for my purposes - but is there a proper way to set a background?
An ImagePattern can be used as fill of the Scene:
ImagePattern pattern = new ImagePattern(myImage);
scene.setFill(pattern);
Just call setBackground on the root node of the scene. For example:
Pane root = ... ; // probably some pane subclass...
String url = ... ;
Image img = new Image(url);
BackgroundImage bgImg = new BackgroundImage(img,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false));
// put stuff in root as normal....
Scene scene = new Scene(root);
See the Javadocs for the parameters for the BackgroundImage constructor, etc.
I making random image gallery in javafx using buttons. I am setting imageView to buttons as I need Images get clicked. The problem is imageView is not setting on button as expected. Buttons have pref height and width as 110.
Below is the code am using to set imageView for buttons.
for(int i=0; i<35; i++){
Button button = list.get(i);
ImageView imageview =new ImageView(imageList.get(i));
imageview.setPreserveRatio(true);
imageview.setFitHeight(110);
imageview.setFitWidth(110);
button.setGraphic(imageview);
}
You need to change the padding of the Buttons to empty:
Button button = new Button(null, imageView);
// quadratic grey background
button.setBackground(new Background(new BackgroundFill(Color.GREY, CornerRadii.EMPTY, Insets.EMPTY)));
// no padding
button.setPadding(Insets.EMPTY);
I want to set background image autoresized with StackPane.
I tried this:
StackPane stackPane = new StackPane();
Image im = new Image(Dashboardpanel.class.getResource("/images/11.jpg").toExternalForm());
stackPane.setBackground(new Background(new BackgroundImage(im, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
But the image is not auto-resized when I resize the StackPane. The image is repeated to fill the gap. How I can resize the image just as the Stackpane no matter how I extend the StackPane?
Don't use the default BackgroundSize, create a new BackgroundSize and use the contain and cover arguments of the BackgroundSize constructor.
For example (and I just wrote this by reading the javadoc, I didn't test it):
new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, false, true);
I advise using a CSS stylesheet for this kind of styling rather than coding the style in Java.