I can't set a background image for a Hbox.
I tried this:
HBoxName.setStyle("-fx-background-image: images/background.png");
in the initialize method and then I also tried adding the CSS style in Scene Builder: -fx-background-image and url("images/background.png").
How can I do it?
There is a couple of ways to set a background image for your HBox,
1. Using CSS
Using setStyle method
Use setStyle() method to set a background image directly,
HBoxName.setStyle("-fx-background-image: url('images/background.png');" +
"-fx-background-repeat: stretch;" +
"-fx-background-size: 1000 700;" +
"-fx-background-position: center center;");
Using external CSS file
You should create an external CSS file load it to your scene(or you can load CSS file to any control as well),
scene.getStylesheets().add(
this.getClass().getClassLoader().getResource("style.css").toString()
);
Add these styles in your style.css file,
#HBoxName{
-fx-background-image: url("images/background.png");
-fx-background-repeat: stretch;
-fx-background-size: 1000 700;
-fx-background-position: center center;
}
References
JavaFX CSS Reference Guide
Apply CSS
CSS file on FXML
2. Setting BackgroundImage using setBackground()
You can set a background-image with programmatically as well.
BackgroundSize backgroundSize = new BackgroundSize(900,
700,
true,
true,
true,
false);
BackgroundImage image = new BackgroundImage(new Image("image/background.png"),
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER,
backgroundSize);
HBoxName.setBackground(new Background(image));
Related
I have a JavaFX application using gradle. I have used an Accordion as container for my TitledPane for my menu. The problem is that I can't set the TitledPane height according to sum of it's content height.
For example TitledPane 1 has 3 button inside:
So I set the height manually. But for TitledPane 2 I need to set height exactly for 1 button and remove extra blank spaces:
How could I do that? By the way the button border of Accordion gets disappear when I expand any TitledPane. My tress structure of Accordion is:
I have tried using css like:
.accordion .titled-pane {
-fx-fit-to-height: true;
}
or:
.accordion .titled-pane {
-fx-fit-to-height: true;
}
or some other css codes.
In my Eclipse RAP application I have a theme with declared CSS styles for controls Button, Combo ...
I have the following problem, creating controls (e.g. Button) with a FormToolkit the background and foreground color is ignored.
Normal Button:
new Button(parent, SWT.PUSH);
FormToolkit Button:
managedForm.getToolkit().createButton(parent, "search", SWT.PUSH);
This is the CSS that I use:
Button[PUSH] {
border: 1px solid #C5C5C5;
/* fancy test colors */
background-color: #0000ff;
color: #00ff00;
}
Normal Button
FormToolkit Button
This is especially bad because the hover effect is ignored as well. Is there a way to enforce the FormToolkit to also use the CSS styles?
I figured out a workaround, not the desired solution but works for now:
// hack to ensure CSS styles are used
toolkit.getColors().setBackground(null);
toolkit.getColors().setForeground(null);
If the colors are set to null, CSS style is used.
I wanted to use a image as a button. I got it working, but it is not very well made, please take a look at the screenshot. As you can see the Button itself is a lot bigger than the image, but I wanted it to be as big as the image:
The actual Button is bigger than the Image. The goal here is that there is nothing but the image to click. How can I achieve this? Here is the code ofthe button on the screenshot:
Button testButton = new Button();
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
testButton.setIcon(new FileResource(new File(basepath + "/VAADIN/themes/mytheme/img/Button.png")));
loginForm.addComponent(testButton);
I know that
testButton.setStyleName(BaseTheme.BUTTON_LINK)
makes the button invisible, but unfortunately that does not adjust the size of the button, just the visbility..
You can simply add a click listner to an image instead of using a button.
Image image = new Image(null, new ClassResource("/images/button-img.jpg");
image.addClickListener(e -> System.out.println("click"));
image.addStyleName("my-img-button");
And add this css, I use the #Stylesheet annotation to add in CSS.
.my-img-button {
cursor: pointer;
}
It works for me:
Button button = new Button();
button.setStyleName(ValoTheme.BUTTON_LINK);
button.setIcon(new ClassResource("/images/button-img.jpg"));
button.addClickListener(e -> System.out.println("click"));
Maybe you have additional css defined?
Maybe your button is contained in a layout with a fixed height?
Also make sure that your button has no width/height configured, so it can automatically adjust its size to that of the icon image.
The next problem you'll probably run into is the focus border:
Another approach would be to use a layout click listener, and add you own mouse-over/hover/focus styling via CSS.
VerticalLayout layout = new VerticalLayout(new Image(null, new ClassResource("/images/test.png")));
layout.addLayoutClickListener(e -> System.out.println("click"));
With Vaadin 14:
Image img = new Image("src");
Button testButton = new Button(img);
Quite straightforward.
I can not set transparent background in scrollpane. Probably because it contains anchorpane, and that anchorpane contains buttons? White background from scrollpane and red from anchorpane:
You have to use css and like this:
.scroll-pane{
-fx-background-color:transparent;
}
and(cause ScrollPane has a Viewport)
.scroll-pane > .viewport { //not .scrollpane but .scroll-pane
-fx-background-color: transparent;
}
or
.scroll-pane .viewport {
-fx-background-color: transparent;
}
If it doesn't work,either you have not defined externall css file well,or you have added some kind of container into the ScrollPane which has also a default background color.
Set an id to your AnchorPane in the fxml and then in your css assign the id to -fx-background-color : transparent.
Hope this works.
if not Please Refer to this question
JavaFX ScrollPane border and background
Hello i'm trying to change the color of an ImageView when your mouse is on it(hover)
So I have a css file that contains this:
.cross:hover {
-fx-background-color: red;
}
And this is cross:
#FXML
private ImageView cross;
I set the fx:id in the fxml file.
Now nothing happens when I hover over it.
This is how I add the style sheet to the scene:
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
This is how I set the id for css on the ImageView:
cross.getStyleClass().add("cross");
So how would I change the color of the ImageView when I hover over it?
The color of an ImageView is defined by the pixel data in the image itself. You probably need to create (externally) an alternative image with the "background" changed to red, and then use the -fx-image CSS property of ImageView to change the image displayed:
.cross {
-fx-image: url(images/cross.png);
}
.cross:hover {
-fx-image: url(images/red-cross.png);
}
where the images folder has the same parent folder as the CSS file (see CSS docs on URL.)