JAVAFX - Transparent bar on button using CSS - css

I want my button to have a transparent black bar on the bottom with a opacity of 75%. The button name should appear on top of the black bar. I have drawn a draft below.
So far I have tried with no success:
.button{
-fx-background-color: #5a9bdc;
-fx-font-size: 16;
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
-fx-text-fill: #ffffff;
}
.button:hover {
-fx-background-color: #97c0dc;
}
UPDATE:
So this is how my css looks:
.button-stats.parent{
-fx-background-color: #5a9bdc;
-fx-font-size: 16;
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
-fx-text-fill: #ffffff;
}
.button-stats:hover {
-fx-background-color: #97c0dc;
}
.button-stats.element{
padding: 20px;
color: rgba(255,255,255,.4);
position: absolute;
bottom: 0;
left: 0;
right: 0;
}

Here is a sample, it isn't going to be exactly what you want, but may help you in getting to where you want. It is based upon button styles found in modena.css in the jfxrt.jar that ships with Java 8.
Images are shown for unhovered and hovered and armed states (armed is when the button is pressed and the shadow is removed).
I did not provide info here for a focused state, so you will need to develop that yourself if you want it.
super-button.css
.button {
-custom-solid-button-color: lightgreen;
-custom-translucent-button-color: rgba(00, 80, 00, 0.75);
-custom-button-color:
linear-gradient(to bottom,
-custom-solid-button-color 0%,
-custom-solid-button-color 64%,
-custom-translucent-button-color 65%);
-fx-background-color: -custom-button-color;
-fx-background-insets: 0;
-fx-background-radius: 0;
-fx-text-fill: whitesmoke;
-fx-padding: 3.333333em 0.666667em 0.333333em 0.666667em;
-fx-font-size: 30px;
-fx-effect: dropshadow(gaussian, black, 10, 0, 3, 3);
}
.button:hover {
-custom-solid-button-color: derive(lightgreen, 20%);
-fx-effect: dropshadow(gaussian, goldenrod, 10, 0, 3, 3);
}
.button:armed {
-custom-solid-button-color: derive(lightgreen, -10%);
-fx-effect: null;
-fx-background-insets: 2 2 0 0;
}
SuperButton.java
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SuperButton extends Application {
private static final String BACKGROUND_IMAGE_LOC =
"http://edugeography.com/images/great-barrier-reef/great-barrier-reef-04.jpg";
#Override
public void start(Stage stage) {
Button button = new Button("I \u2764 Sea Turtles");
ImageView background = new ImageView(
new Image(BACKGROUND_IMAGE_LOC, 400, 0, true, true)
);
StackPane layout = new StackPane(
background,
button
);
StackPane.setAlignment(button, Pos.BOTTOM_CENTER);
StackPane.setMargin(button, new Insets(0, 0, 15, 0));
Scene scene = new Scene(layout);
scene.getStylesheets().add(getClass().getResource(
"super-button.css"
).toExternalForm());
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Getting the translucent area at the base is slightly tricky, especially because you are applying a drop shadow effect. What happens with a drop shadow effect is that the drop shadow is visible through the translucent area. Normally, when you have an opaque foreground, you can see the shadow through the foreground, but when you have a translucent foreground, the shadow mars the translucent effect a bit. To understand what I mean, review the above images and note the difference between the translucent area in the images with and without a drop shadow involved.
So you might want to rethink the design to not use the drop shadow. There are ways around this using clips, but it gets a bit more complicated and you cannot achieve it using just CSS (you will also need to write some custom skin code in Java, which I won't demonstrate here).

Try
.button {
-fx-opacity: 0.7;
}

Related

JavaFX, how to set SVGPath width of shape style CSS

I want to create a chat box style around a TextArea in JavaFX. I have the following code:
TextArea ta = new TextArea();
ta.getStyleClass().add("chat-bubble");
VBox vb = new VBox(9);
VBox.setMargin(ta, new Insets(10,10,10,10));
vb.getChildren().addAll(ta);
Scene scene = new Scene(vb, 200, 300);
scene.getStylesheets().add("resources/ta.css");
stage.setScene(scene);
stage.show();
and the following CSS (resources/ta.css):
TextArea {
-fx-background-insets: 0;
-fx-background-color: transparent;
}
TextArea .scroll-pane {
-fx-background-color: transparent;
}
TextArea .scroll-pane .viewport{
-fx-background-color: transparent;
}
TextArea .scroll-pane .content{
-fx-background-color: transparent;
}
.chat-bubble {
-fx-shape: "M334.266,29.302c-4.143,0-7.5,3.358-7.5,7.5v210.632H177.423c-1.656,0-3.266,0.548-4.578,1.559L120,289.717v-34.783c0-4.142-3.358-7.5-7.5-7.5s-7.5,3.358-7.5,7.5v50.031c0,2.858,1.625,5.468,4.19,6.73c1.05,0.517,2.182,0.77,3.309,0.77c1.626,0,3.242-0.529,4.579-1.56l62.9-48.472h154.288c4.143,0,7.5-3.358,7.5-7.5V36.802 C341.766,32.659,338.409,29.302,334.266,29.302 M72.884,247.433H15V44.302h272.883c4.143,0,7.5-3.358,7.5-7.5s-3.357-7.5-7.5-7.5H7.5c-4.142,0-7.5,3.358-7.5,7.5v218.132c0,4.142,3.358,7.5,7.5,7.5h65.384c4.142,0,7.5-3.358,7.5-7.5S77.026,247.433,72.884,247.433z";
-fx-background-color: darkred;
-fx-padding: 3 3 10 3;
}
VBox {
-fx-background-color: transparent;
}
But the stroke width of the chat box is too thick:
I have tried all sorts of tweaks with border-width, stroke-width etc. in CSS and programmatically.
How do I go about setting the stroke width of the -fx-shape path?

JavaFx : Customizing Alert Dialog

I want to customize the buttons, button container, backgroud color, the AlertType icon as well in an Alert Dialog.
Tried following these two solutions :
Styling default JavaFX Dialogs
Customize JavaFx Alert with css
I suppose the code from CSS that I have mentioned should be applicable to all the Alert dialog-pane ?
Not sure what am I missing here.
private static void createSimpleInformationDialog(String message){
Alert alert = createSimpleInformationAlert(message, AlertType.INFORMATION);
alert.getDialogPane().setHeaderText(StringTools.isNull(null, ""));
alert.getDialogPane().setMaxWidth(200);
alert.getDialogPane().setMinWidth(150);
alert.getDialogPane().setPadding(new Insets(0, 10, 0, 10));
alert.showAndWait();
}
private static Alert createSimpleInformationAlert(String message, AlertType type) {
Alert alert = new Alert(type);
alert.setTitle(Lang.get(Defs.FX_DIALOGS_EXCEPTIONS_GENERIC_TITLE));
alert.setContentText(message);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(FXMain.getInstance().getStage());
return alert;
}
CSS file :
.dialog-pane{
-fx-border-color:black;
-fx-border-width:2.0px;
}
/**Costumization of The Bar where the buttons are located**/
.dialog-pane > .button-bar > .container {
-fx-background-color:black;
}
.dialog-pane > .content.label {
-fx-padding: 0.5em 0.5em 0.5em 0.5em;
-fx-background-color: yellow;
-fx-text-fill:black;
-fx-font-size:15.0px;
}
/**Costumization of DialogPane Header**/
.dialog-pane:header .header-panel {
-fx-background-color: black;
}
.dialog-pane:header .header-panel .label{
-fx-background-color: yellow;
-fx-background-radius:10px;
-fx-text-fill:black;
-fx-font-size:15.0px;
}
/**Costumization of Buttons**/
.dialog-pane .button{
-fx-background-color:black;
-fx-text-fill:white;
-fx-wrap-text: true;
-fx-effect: dropshadow( three-pass-box, yellow, 10.0, 0.0, 0.0, 0.0);
-fx-cursor:hand;
}
.dialog-pane .button:hover{
-fx-background-color:white;
-fx-text-fill:black;
-fx-font-weight:bold;
}

Change the color of the Scrollbar corner in WebView

I'm working with JavaFX 8 on Windows 10. In a WebView with a dark background, I can see the light grey corner when the scrollbars are visible. WebView"manages scrolling automatically." I already tried this, as well as other selectors:
.corner {
-fx-background-color: black;
}
And also
.corner {
-fx-background-color: black;
}
.scroll-bar > .corner {
-fx-background-color: black;
}
.scroll-pane > .corner {
-fx-background-color: black;
}
.scroll-bar .corner {
-fx-background-color: black;
}
.scroll-pane .corner {
-fx-background-color: black;
}
.web-view .scroll-bar .corner {
-fx-background-color: black;
}
.web-view .scroll-pane .corner {
-fx-background-color: black;
}
But it doesn't work. So what could I do?
Example code: Main class
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.loadContent("<html><body><pre>This is a very very very very very very long string </pre><b>test</b><p>1</p><p>2</p><p>3</p></body></html>");
webEngine.setUserStyleSheetLocation("data:,body { background: black; color: white; } ");
StackPane root = new StackPane();
root.setPadding(new Insets(5));
root.getChildren().add(webView);
webView.setStyle("-fx-background-color:black;");
root.setStyle("-fx-background-color:black;");
root.getStylesheets().add("style.css");
primaryStage.setTitle("JavaFX Test");
primaryStage.setScene(new Scene(root, 300, 150));
primaryStage.show();
}
}
style.css
.scroll-bar .track {
-fx-background-color: black;
}
.scroll-bar .thumb {
-fx-background-color: brown;
}
.corner {
-fx-background-color: black;
}
This also has an awful behavior where the scrollbars only appear when I hover with the mouse, but nevermind. This doesn't happen in my main application. I just want to change the color of the grey square in the corner.
I am unable to reproduce the effect on Mac OS X v10.13.6 with Java v1.8.0_201. Because WebView "manages scrolling automatically" and JavaFX uses WebKit, #Pagbo suggests using -webkit-scrollbar-corner, as suggested here. In another context, #DVarga suggests using -fx-background-color, as shown here. As the effect may be platform/version dependent, I've added a complete example and screenshot for reference. In particular, the lower-right corner is overlain by the vertical scrollbar's decrement button. Stretching the window to hide the vertical scrollbar reveals the horizontal scrollbar's increment button. The corner is always occupied by a scrollbar button or black.
Main.java
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.loadContent("<html><body><pre>"
+ "This is a very very very very very very long string<br>"
+ System.getProperty("os.name") + " v"
+ System.getProperty("os.version") + "; Java v"
+ System.getProperty("java.version")
+ "</pre><b>test</b><p>1</p><p>2</p><p>3</p></body></html>");
webEngine.setUserStyleSheetLocation("data: ,body "
+ "{ background: black; color: white; } "
+ "::-webkit-scrollbar-corner { background: #0c0c0c; } ");
StackPane root = new StackPane();
root.setPadding(new Insets(5));
root.getChildren().add(webView);
webView.setStyle("-fx-background-color:black;");
root.setStyle("-fx-background-color:black;");
root.getStylesheets().add("style.css");
primaryStage.setTitle("JavaFX Test");
primaryStage.setScene(new Scene(root, 300, 150));
primaryStage.show();
}
}
style.css
.scroll-bar .track {
-fx-background-color: black;
}
.scroll-bar .thumb {
-fx-background-color: brown;
}
.corner {
-fx-background-color: black;
}

Make a dark mode with JavaFx

I was wondering if there is an easy way to make a dark mode using JavaFx and CSS. I have a MenuBar with a CheckMenuItem called 'Dark mode' and when I click it I want the scene to become dark and the text to become white.
Here's mine.
(Update) The previous one was too opaque.
.root {
-fx-accent: #1e74c6;
-fx-focus-color: -fx-accent;
-fx-base: #373e43;
-fx-control-inner-background: derive(-fx-base, 35%);
-fx-control-inner-background-alt: -fx-control-inner-background ;
}
.label{
-fx-text-fill: lightgray;
}
.text-field {
-fx-prompt-text-fill: gray;
}
.titulo{
-fx-font-weight: bold;
-fx-font-size: 18px;
}
.button{
-fx-focus-traversable: false;
}
.button:hover{
-fx-text-fill: white;
}
.separator *.line {
-fx-background-color: #3C3C3C;
-fx-border-style: solid;
-fx-border-width: 1px;
}
.scroll-bar{
-fx-background-color: derive(-fx-base,45%)
}
.button:default {
-fx-base: -fx-accent ;
}
.table-view{
/*-fx-background-color: derive(-fx-base, 10%);*/
-fx-selection-bar-non-focused: derive(-fx-base, 50%);
}
.table-view .column-header .label{
-fx-alignment: CENTER_LEFT;
-fx-font-weight: none;
}
.list-cell:even,
.list-cell:odd,
.table-row-cell:even,
.table-row-cell:odd{
-fx-control-inner-background: derive(-fx-base, 15%);
}
.list-cell:empty,
.table-row-cell:empty {
-fx-background-color: transparent;
}
.list-cell,
.table-row-cell{
-fx-border-color: transparent;
-fx-table-cell-border-color:transparent;
}
It's been a while since I played with "theming" a JavaFX application, but from a while ago I have a CSS file:
.root {
-fx-base: #3f474f;
-fx-accent: #e7eff7 ;
-fx-default-button: #7f878f ;
-fx-focus-color: #efefef;
-fx-faint-focus-color: #efefef22;
-fx-focused-text-base-color : ladder(
-fx-selection-bar,
-fx-light-text-color 45%,
-fx-dark-text-color 46%,
-fx-dark-text-color 59%,
-fx-mid-text-color 60%
);
-fx-focused-mark-color : -fx-focused-text-base-color ;
}
.text-input:focused {
-fx-highlight-text-fill: ladder(
-fx-highlight-fill,
-fx-light-text-color 45%,
-fx-dark-text-color 46%,
-fx-dark-text-color 59%,
-fx-mid-text-color 60%
);
}
If you put this in a file, say dark-theme.css, you can do
checkMenuItem.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
if (isSelected) {
scene.getStyleSheets().add("dark-theme.css");
} else {
scene.getStyleSheets().remove("dark-theme.css");
}
});
the property base can be applied to every JavaFX type, This enables a color theme to be specified using a single base color for a JavaFx Node or Layout..., and to have variant colors (for its children) computed based on that base color!
in this case, you are trying to set the theme for the whole scene so you should apply the base color to the highest Component in the hierarchy which you can get by getting the root Node of your scene!
checkMenuItem.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
if (isSelected) {
scene.getRoot().setStyle("-fx-base:black");
} else {
scene.getRoot().setStyle("");
}
});
I'm new to javafx and all, but I'm pretty sure creating 2 stylesheets and switching between them would suffice.
Again if what I said was wrong, sorry, I'm new to javafx

How to remove innser shadow in Text field

I want to remove the inner shadow in TextField.
Here is css.
-fx-font-size: 12px;
-fx-font-family: "Segoe UI Semibold";
-fx-pref-width:250px;
-fx-pref-height:35px;
-fx-background-radius:0px;
Try adding this line:
-fx-background-color: -fx-text-box-border, -fx-control-inner-background;
The problem is that it is NOT shadow. It is a specially decorated background. So making it transparent or putting a key which its value is transparent will work well.
.text-field {
-fx-text-box-border: transparent;
-fx-background-color: transparent;
}
.text-field:focused {
-fx-faint-focus-color: transparent; /*JavaFX 8*/
-fx-focus-color: transparent; /*JavaFX 2.2*/
}
The style code above(style.css) will generate a text field without any border and background and make it clear. By the way putting -fx-border-color or -fx-faint-color instead of transparent in -fx-background-color is completely fine since their value is transparent.
Edit: I'm adding now a little example code.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
public class TextFieldDemo extends Application {
public void start(Stage primaryStage) {
TextField textField = new TextField();
textField.setPromptText("TextField");
textField.setMaxWidth(100);
textField.setLayoutX(50);
textField.setLayoutY(25);
textField.setFocusTraversable(false);
Rectangle background = new Rectangle(40, 20, 120, 40);
background.setStyle("-fx-arc-width: 40px; -fx-arc-height: 40px;-fx-fill: yellow;");
Scene scene = new Scene(new Pane(background, textField), 200, 80);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Example Output:
Example Image

Resources