JavaFX - Add image to ColorPicker - java

I'm using netBeans and SceneBuilder to create a small window with a few text editor options. One of the buttons I've included in the screen is a colorPicker that I have modified according to the following style;
<ColorPicker fx:id="clrFill" layoutX="119.0" layoutY="18.0" prefHeight="30.0" prefWidth="30.0" style="-fx-color-label-visible: false; -fx-color-rect-height: 3; -fx-color-rect-width: 17;" styleClass="button" stylesheets="/css/colorPickerStyle.css" />
Here is the current stylesheet being used;
.color-picker .color-picker-label .picker-color
{
-fx-alignment : bottom-center;
}
colorPicker.getStyleClass().add("button");
.color-picker{
-fx-background-image: "/gui_resources/fill.png"
-fx-background-size: 20 20;
-fx-background-position: top center;
}
My objective is to have an image display above the modified colorpicker 'rectangle' that displays the current color. Initially, I tried to treat the colorPicker as a Button class, as I have changed the style to a "button", but I was unable to place an image using the setGraphic method. As you can see from the css file, I have also tried to implement a background image unsuccessfully.
Here is a copy of what my screen currently looks like. The colorPicker is located beside the ComboBox dropdown.

In case you were trying to change the ColorPicker style class inside the css file, that won't work, you can't place code there.
Actually, to use an image, you don't need to change its style class, you just need to use url(image.png) (image placed in the same folder as FXML), or with a relative path to the FXML file like url(../image.png), or using # in case of an absolute path.
This should work:
.color-picker {
-fx-background-image: url(fill.png);
-fx-background-size: 20 20;
-fx-background-position: top center;
-fx-background-repeat: no-repeat;
}
.color-picker .color-picker-label .picker-color {
-fx-alignment : bottom-center;
}
You can check the <uri> format for the image here.

Related

JavaFX button pressed issue

I need that when I press a button, the button color changes, Im making the interface in scene builder and style it with fx css. I tried:
.botones:hover {
-fx-background-color: red;
}
.botones:pressed {
-fx-background-color: blue;
}
.botones {
-fx-background-color: #262626;
}
The hover works fine, but when i press the button, it turns blue for a second and then changes to its original color and i want it to stay blue.
Maybe i could use a toggle button instead, but im using jfoenix buttons because they have some cool effects.
You're right that the ToggleButton in JFoenix is more of a switch:
However, you can use a lesser-known control in JFoenix called JFXToggleNode.
Just put a label inside of it. (Don't forget to include the import statements.)
<?import com.jfoenix.controls.JFXToggleNode?>
<?import javafx.scene.control.Label?>
...
<JFXToggleNode>
<Label text="Boton Azul" />
</JFXToggleNode>
and add this rule to your stylesheet:
.jfx-toggle-node {
/* This is the color once toggled on. */
-jfx-toggle-color: deepskyblue;
}
Note that SceneBuilder doesn't support non-standard controls very well, yet, so though you can't drag and drop it, you can add it manually to the FXML file just fine.

How do I change the color of a table in JavaFX?

I have a table in a JavaFX application where I want to be able to change the color of the selected element with a color picker.
When I try the following:
menuBar.setStyle("-fx-background-color:" + settings.getRgbString(memberView.colorPicker.getValue()));
There is no way to add the style just to the selected item. I tried to use a pseudo class but I can't understand how to do it. How do I solve this?
Note: changing the background of buttons works fine with this method.
try to add a css file to the project and put
.table-row-cell:selected .table-cell {
-fx-background-color: #FF9034;
-fx-text-fill: #FFF;
}

Set JavaFX PieChart label color via css

I have a PieChart in my Java application which looks just like this:
A picture of a label (very hard to see):
Since the pie chart is rendered on a solid gray background, (which is basically the same color as the labels' text) the labels are virtually invisible. So, my question is, is there any CSS code that would change the color of a PieChart label's text, and if so, what is it?
For any future users below is answer with css code:
You can find more here : http://docs.oracle.com/javafx/2/charts/css-styles.htm
.chart-pie-label-line {
-fx-stroke: #8b4513;
-fx-fill: #8b4513;
}
.chart-pie-label { /*this is what you need for labels*/
-fx-fill: #8b4513;
-fx-font-size: 1em;
}
.chart-legend {
-fx-background-color: #fafad2;
-fx-stroke: #daa520;
}
So, the labels of a pie chart are Text objects, meaning that the CSS property -fx-text-fill does not work for them. Instead, the -fx-fill property changes their color.
Here is a screenshot of my Pie Chart with colored labels:
A simple line of CSS does the trick...
.chart-pie-label {-fx-fill: #ff4f0a;}

JavaFX background-image scaling

I have this piece of code, which works fine.
gamePane.setPrefSize(game.getMaxX(), game.getMaxY());
However, the background-image my gamePane (which is a Pane) has automatically scales with it, and I do not want this. I've searched for background scaling properties, but I couldn't find anything.
The image is originally applied with css.
Edit: I have found the solution!
What my css code was originally:
.background {
-fx-background-image: url("../images/background.png");
}
What I made it to be:
.background {
-fx-background-image: url("../images/background.png");
-fx-background-size: 1024 768;
-fx-background-position: center;
}

style pressed button in JavaFX

I've got a Button in my FXML file and i give a style to it by below CSS
.button {
-fx-background-color: linear-gradient(#ff5400, #be1d00);
-fx-background-radius: 30;
-fx-background-insets: 0;
-fx-text-fill: white;
}
as you can see the button has a new awesome style, but whenever i click on it, it remains as like as before and you can't understand is it clicked or not...
As I searched, I found one solution in this link: Pressed CSS, but if you notice it is The CSS that is used by Web Browsers and JavaFX does not support it.
so what is the solution? I want my button changes its appearance when the users hit or click it.
You need to add the psuedoclass state pressed to you css and add new css to it, which will differentiate your current button css with that when pressed :
.button:pressed {
// Your new css
}
For changing the style while hovering the button use :
.button:hover {
// Your new css
}
For better understanding of what style you can add for styling the button, you can go through How to make a button appear to have been clicked or selected?
for arrayLists on javaFX, try this on the CSS to change colors on mouse click:
.list-cell:selected { -fx-background-color: yellow; }

Categories