How to separate a border and an effect? - javafx

Currently, I have a button with the following CSS:
Button {
-fx-text-fill: -fx-color-text;
-fx-font: 15pt "Raleway SemiBold";
-fx-background-color: -fx-color-theme;
-fx-background-insets: 0, 0, 0, 0, 0, 1, 2;
-fx-background-radius: 0;
-fx-padding: 4px 20px;
/* -fx-effect: innershadow(two-pass-box, white, 2, 0.2, 0, 0); */
-fx-border-insets: 0;
-fx-border-color: black;
}
And it looks like this:
And I'm trying to add an inner shadow to the button. I want the border to show on the outside of this button, which should result in something like this:
I made that in Swing. However, when I try and apply my inner shadow, it draws on top of my border, as shown:
I've tried setting the border insets to -1 (because there isn't an insets property for the effect), but that just moved the effect with it:
My question is: What can I do to ensure that I can see my border around the outside, but keep my effect on the inside?
EDIT: I'm pretty sure that this is a bug in OpenJFX and I've submitted a bug report. Here's some quick code you can use to reproduce the issue:
public class App extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Region example = new Region();
example.setMaxWidth(100);
example.setMaxHeight(100);
example.setEffect(new InnerShadow(BlurType.GAUSSIAN, Color.RED, 10, 0.04, 0, 0));
example.setStyle("-fx-background-color: white; -fx-border-color: BLUE; -fx-border-width: 5px;");
StackPane container = new StackPane();
container.setStyle("-fx-background-color: black;");
container.setAlignment(Pos.CENTER);
container.getChildren().add(example);
Scene scene = new Scene(container, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}

I think the border is getting blended with the effect which is applied. And as you are pretty sure that it could be a bug in JavaFX rendering, below is one way to get it look the way you want.
You can consider the below solution as when you think like "I definitely want this look only !!" kind of thing :)
The idea is, instead of setting the effect, you paint the effect manually.
.button {
-fx-text-fill: #FFFFFF;
-fx-font: 15pt "Raleway SemiBold";
-fx-background-color: #000000, #720D4D, #D03A9A, #DD70B6, #B01378, #CC2C93, #D03A9A, #C81688;
-fx-background-insets: 0, 1, 1 2 2 1, 1 2 2 2, 2, 2 3 3 2, 2 3 3 3, 3;
-fx-background-radius: 0;
-fx-padding: 4px 20px;
-fx-border-insets: 0px;
-fx-border-radius: 0px;
-fx-border-width: 0px;
}
And the output is as below [ignore the font as I dont have the .tff ;)]:
And a closer look for verification purpose:
This may not be an actual solution, but can be one alternate (least case).

Related

Highlight top of a JavaFX TableRow with css

I'm trying to figure out the CSS for highlighting the top of a TableRow. I'm using this for reordering rows so that anyone reordering them can tell where it will be inserted. Currently I've only been able to draw a rectangle around the row, but I just want the top line of that rectangle.
.table-row-cell.drag {
-fx-focus-color: #00a9d3;
-fx-faint-focus-color: #00a9d322;
-fx-highlight-fill: -fx-accent;
-fx-background-color:
-fx-focus-color,
-fx-control-inner-background,
-fx-faint-focus-color,
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: -0.2, 1, -1.4, 3;
-fx-background-radius: 3, 2, 4, 0;
}
You could specify different -fx-background-insets for the sides. If one color should only appear on one side, the insets of the next color match the insets for the mark color except for the side that should be colored.
Furthermore I recommend using PseudoClass instead of a style class, since this way you don't need to make sure to not add a class multiple times.
final PseudoClass mark = PseudoClass.getPseudoClass("mark");
...
boolean marked = ...
row.pseudoClassStateChanged(mark, marked);
The following CSS is a bit simplified, but it should demonstrate the approach:
.table-row-cell:mark {
-fx-background-color: -fx-table-cell-border-color, red, -fx-background;
/* show red highlight of size 2 at the top */
-fx-background-insets: 0, 0 0 1 0, 2 0 1 0;
}

JavaFX CSS two-pixel border

Help me with JavaFX CSS. I need to create a border width of 2 pixels:
Up pixel - #000
Down pixel - #5d5c5e
I think I need to use a linear-gradient, but I don't know how to do it.
It's not really clear what you're asking; but the standard way to put a border on something in JavaFX is to use "nested backgrounds". This technique involves creating different color backgrounds, one drawn over the other, with different insets so that you get the effect of a border.
For example, the following CSS in an external CSS file will give a four-pixel black (#000) border around a four-pixel gray border (around the default background color defined in modena.css). (I used wider borders to make the effect clearer.)
style.css:
.root {
-fx-background-color: #000, #5d5c5e, -fx-background ;
-fx-background-insets: 0, 4, 8 ;
}
This works by drawing a black background with no insets, and then drawing a gray background with 4 pixel insets over it (leaving four pixels of the black background visible), and finally drawing a background with the default color over the top of that, with 8 pixels insets (so four pixels of the gray border is visible).
Here's a quick test:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class BackgroundTest extends Application {
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new StackPane(new Label("Nested backgrounds")), 400, 400);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
which results in
This variation
.root {
-fx-background-color: #000, #5d5c5e, -fx-background ;
-fx-background-insets: 0, 4 0 0 0, 4 0 4 0 ;
}
leaves four pixels of the black background visible at the top, and four pixels of the gray background visible at the bottom:
And this has black at the top, grey at the bottom, and a linear gradient fading from black to grey down the sides:
.root {
-fx-background-color: #000, #5d5c5e, linear-gradient(to bottom, #000, #5d5c5e), -fx-background ;
-fx-background-insets: 0, 4 0 0 0, 4 0 4 0 , 4;
}
You need to add a border for your first border, then a box-shadow for your remaining borders:
.doubleBorder{
border: 1px solid #5d5c5e;
box-shadow:
1px 1px #000,
-1px 1px #000,
1px -1px #000,
-1px -1px #000;
}

How to get a small ProgressBar in JavaFX

I'm trying to get an iTunes like progress bar that is very small (height of about 5px) but I can't seem to go any lower than 19 or 20px.
I tried setting -fx-max-height with no avail, also on the surrounding pane. Note that this value does indeed change the height - I just can't get it less than about 20px.
Any ideas how to achieve that? I'd like to avoid using a plain rectangle for indicating progress because of loss of semantic and support of assistive technology.
Thanks.
The ProgressBar default styling is the reason why you can't reduce its height.
As we can see in the modena.css file for the progress-bar CSS selector:
.progress-bar > .bar {
-fx-background-color: linear-gradient(to bottom, derive(-fx-accent, -7%), derive(-fx-accent, 0%), derive(-fx-accent, -3%), derive(-fx-accent, -9%) );
-fx-background-insets: 3 3 4 3;
/*-fx-background-radius: 0.583em; *//* 7 */
-fx-background-radius: 2;
-fx-padding: 0.75em;
}
two CSS properties are responsible for this: -fx-padding for the height of the blue inner bar and -fx-background-insets for the height of the surrounding gray bar.
So you can customize this selector as you need. For instance, with these properties you will have 5 pixels height:
.progress-bar > .bar {
-fx-background-insets: 1 1 2 1;
-fx-padding: 0.20em;
}
No need for height settings on your code:
#Override
public void start(Stage primaryStage) {
ProgressBar pb = new ProgressBar(0.4);
pb.setPrefWidth(200);
StackPane root = new StackPane(pb);
Scene scene = new Scene(root, 300, 200);
scene.getStylesheets().add(getClass().getResource("root.css").toExternalForm());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

How to change the color of text in javafx TextField?

I want to change font color in TextField .I found -fx-background-color , -fx-border-color for changing the color of background and border but nothing for text.
Setting the -fx-text-fill works for me.
See below:
if (passed) {
resultInfo.setText("Passed!");
resultInfo.setStyle("-fx-text-fill: green; -fx-font-size: 16px;");
} else {
resultInfo.setText("Failed!");
resultInfo.setStyle("-fx-text-fill: red; -fx-font-size: 16px;");
}
The CSS styles for text input controls such as TextField for JavaFX 8 are defined in the modena.css stylesheet as below. Create a custom CSS stylesheet and modify the colors as you wish. Use the CSS reference guide if you need help understanding the syntax and available attributes and values.
.text-input {
-fx-text-fill: -fx-text-inner-color;
-fx-highlight-fill: derive(-fx-control-inner-background,-20%);
-fx-highlight-text-fill: -fx-text-inner-color;
-fx-prompt-text-fill: derive(-fx-control-inner-background,-30%);
-fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: 0, 1;
-fx-background-radius: 3, 2;
-fx-cursor: text;
-fx-padding: 0.333333em 0.583em 0.333333em 0.583em; /* 4 7 4 7 */
}
.text-input:focused {
-fx-highlight-fill: -fx-accent;
-fx-highlight-text-fill: white;
-fx-background-color:
-fx-focus-color,
-fx-control-inner-background,
-fx-faint-focus-color,
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: -0.2, 1, -1.4, 3;
-fx-background-radius: 3, 2, 4, 0;
-fx-prompt-text-fill: transparent;
}
Although using an external stylesheet is a preferred way to do the styling, you can style inline, using something like below:
textField.setStyle("-fx-text-inner-color: red;");
If you are designing your Javafx application using SceneBuilder then use -fx-text-fill(if not available as option then write it in style input box) as style and give the color you want,it will change the text color of your Textfield.
I came here for the same problem and solved it in this way.

javafx-2, remove focus highlighting via CSS

I want to remove the blue border that surronds a TableView when it is focused.
I checked into the caspian.css, that for most components like Button and TextField, does something like:
.table-view:focused {
-fx-background-color: -fx-focus-color,-fx-box-border,-fx-control-inner-background;
-fx-background-insets: -1.4, 0, 1;
-fx-background-radius: 1.4, 0, 0;
/*....*/
-fx-padding: 1; /* 0.083333em; */
}
My question is about CSS, at the end. Can I override this pseudoclass specification in my stylesheet instead of trying to turn the colors into transparent?
Yes you can. In your stylesheet define exactly the same CSS selector with JavaFX CSS properties of your own choice like this:
.table-view:focused {
-fx-background-color: red /* or transparent or other preferred color */,-fx-box-border,-fx-control-inner-background;
-fx-background-insets: -1.4, 0, 1;
-fx-background-radius: 1.4, 0, 0;
/*....*/
-fx-padding: 1; /* 0.083333em; */
}
Because JavaFX 8 has a new theme 'modena' but with the same old problem I want to make an update of the answer of Uluk Biy and give some further information:
If you ever wonder, where some style came from, you can have a look into modena.css. You can find this css file in jfxrt.jar, which itself could be found in your JRE or JDK installation directory. On Ubuntu 14.04 the jdk home is usually under
/usr/lib/jvm/java-8-oracle/
and the jfxrt.jar should be here
/usr/lib/jvm/java-8-oracle/jre/lib/ext
Open the jar-file and have a look in the directory /com/sun/javafx/scene/control/skin/modena where you will find the modena.css
Starting from line 406 you can see a headlines like this
/* ==== BUTTON LIKE THINGS ============================================== */
The css under this section specifies the background and border for a lot of controls and containers. You simply have to search for your target class. I will show it for buttons as an example:
For the button class the following css is specified
-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0 0 -1 0, 0, 1, 2;
For some reasons they do not use borders, but use multiple backgrounds simulating a border like look. So if you want to remove the border only, you have to adjust the background and inset property.
Finally if you want to remove the focused effect you have to override this default css
.button:focused,
.toggle-button:focused,
.radio-button:focused > .radio,
.check-box:focused > .box,
.menu-button:focused,
.choice-box:focused,
.color-picker.split-button:focused > .color-picker-label,
.combo-box-base:focused,
.slider:focused .thumb {
-fx-background-color: -fx-focus-color, -fx-inner-border, -fx-body-color, -fx-faint-focus-color, -fx-body-color;
-fx-background-insets: -0.2, 1, 2, -1.4, 2.6;
-fx-background-radius: 3, 2, 1, 4, 1;
}
for the controls of you choice with the default definition for background and insets from above. E.g. for buttons:
.button:focused {
-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0 0 -1 0, 0, 1, 2;
}

Resources