I'm a newbie in JavaFX. How to set different background colors for the contents of different TextAreas. As far as I know , using CSS, I can set background color like
.text-area {
-fx-background-color: transparent;
-fx-text-box-border: gray;
}
.text-area .scroll-pane .content{
-fx-background-color: transparent;
}
But it is affecting both TextAreas.
Also what is the background color of the disabled TextArea in JavaFX and how can I modify it?
TextArea textarea = new TextArea();
TextArea textarea1 = new TextArea();
These are the attributes I have applied
textarea1.setMaxHeight(180);
textarea1.setMaxWidth(500);
textarea.setEditable(false);
textarea.setPrefRowCount(15);
textarea.setWrapText(true);
textarea.setStyle("-fx-background-color: transparent");
textarea1.setStyle("-fx-background-color: tomato");
You can introduce a custom variable in CSS to determine the color.
When a TextArea is disabled, the opacity of the TextArea and children is set to 0.4 (=40%). You can undo this by overwriting the property in your stylesheet, if you wish.
.text-area {
/* use variable as inner background */
-fx-control-inner-background: content-background;
}
/* keep element fully opaque, when disabled */
.text-area:disabled,
.text-area *:disabled {
-fx-opacity: 1;
}
/* replace inner background with darker color, when disabled */
.text-area:disabled {
-fx-control-inner-background: derive(content-background, -40%);
}
// set content-background from inline style
textarea.setStyle("content-background: transparent;");
textarea1.setStyle("content-background: tomato;");
In case you do not need the color to determine the -fx-control-inner-background based on your chosen color (the derive part), you could also simply assign the property from inline style. In this case you do not need the CSS rules for the background in your stylesheet.
textarea.setStyle("-fx-control-inner-background: transparent;");
textarea1.setStyle("-fx-control-inner-background: tomato;");
So what you need to do is put this line inside your css page:
.text-area .content {
-fx-background-color: text-area-background ;
}
So now whatever you set your text-area background to it will set the content back grounf to that. So you should be able to do this below and it will work:
TextArea one = new TextArea();
TextArea two = new TextArea();
TextArea three = new TextArea();
one.setStyle("-fx-background-color: transparent");
two.setStyle("-fx-background-color: tomato");
three.setStyle("-fx-background-color: steelblue");
Related
How do I set hyperlink color to black, which is by default blue?
source=new Hyperlink("source"); source.setFont(Font.font("Arial", 20));
source.setLayoutX(920); source.setLayoutY(900);
source.setTooltip(new Tooltip("Github"));
layout.getChildren().add(source); // here layout is Pane layout
I want to set the color of source hyperlink to black.
You can change it in CSS:
.hyperlink {
-fx-text-fill: black;
}
I am creating a small text editor with JavaFX.
For this purpose I use TextArea and ComboBox'es to dynamically style it: e.g. changing the font, it's size, making it bold, italic and so on. It kind of works, however there is a disturbing visual bug that I can't put up with.
I tried to narrow the problem and here is a simpler code that has the same behavior and a couple of pics to understand what I'm talking about:
(to reproduce the bug set the size to 70 then choose bold and you will see how the text steps away from the edge.)
public class Main extends Application {
public void start(Stage stage) {
textArea = new TextArea("TEST 112123");
textArea.setPrefWidth(800);
textArea.setPrefHeight(400);
textArea.setLayoutY(40);
CheckBox bold = new CheckBox("BOLD");
bold.setLayoutX(20);
bold.setOnAction(e -> {
Font currentFont = textArea.getFont();
if (bold.isSelected()) {
textArea.setFont(
new Font("System Bold", currentFont.getSize()));
//I set new Font each time to save all of it's past properties and
//change only one of them, this is the only way that I found to do
//this as there are no setters in the Font class, only constructors
} else {
textArea.setFont(
new Font("System", currentFont.getSize()));
}
});
ComboBox sizeBox = new ComboBox();
//I removed the list of options and the input validity check
sizeBox.setLayoutX(80);
sizeBox.setEditable(true);
sizeBox.setOnAction(e -> {
textArea.setFont(new Font(textArea.getFont().getName(),
Double.valueOf((String)sizeBox.getValue())));
});
stage.setScene(new Scene(new Group(textArea, bold, sizeBox), 800, 500));
stage.show();
}
}
images: https://imgur.com/a/Cg53nAL
You can add the following stylesheet.
.text-area .content {
-fx-padding: 3 7 3 7;
}
It overrides the padding from modena.css:
.text-area .content {
/*the is 1px less top and bottom than TextInput because of scrollpane border */
-fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */
-fx-cursor: text;
-fx-background-color:
linear-gradient(from 0px 0px to 0px 4px, derive(-fx-control-inner-background, -8%), -fx-control-inner-background);
-fx-background-radius: 2;
}
I'm having a problem handling the focus of a child Node. I have this custom layout with a bunch of nodes in its hierarchy, and I'd like to make one of its children (specifically, a Pane) to be focusTraversable or can obtain focus on mouse pressed (like a button control). What I really want to do is simply change the background color of the child when focused, here's my code:
Pane pane = new Pane();
pane.getStyleClass().setAll("container");
pane.setFocusTraversable(true);
pane.setOnMousePressed(event -> {
if (!pane.isFocused() && pane.isFocusTraversable()) {
pane.requestFocus();
// Test if this node is currently focused
System.out.println(pane.isFocused());
}
});
CSS changes the -fx-background-color of the pane:
.container {
-fx-background-color: red;
}
.container:focused {
-fx-background-color: blue;
}
Although it appears that the node is focused when traversing using TAB key, the background does not change at all when it is pressed. So, what was the proper way of handling focus of children?
I have tried the following with no success:
.tree-view {
-fx-skin: "com.sun.javafx.scene.control.skin.TreeViewSkin";
-fx-background-color: green, -fx-control-inner-background;
-fx-background-insets: 0, 1;
}
What I am trying to achieve is to set a blue background for an entire TreeView except the selected one. Setting the background on the selected tree item works fine, but changing for the entire tree view does not have any effect.
You can color the individual TreeCells by using the .tree-cell selector:
.tree-view {
-fx-background-color:lightsteelblue;
}
.tree-cell {
-fx-background-color:lightsteelblue; // Default color
}
.tree-view:focused .tree-cell:filled:selected {
-fx-background-color:indigo; // Focused color
}
.tree-cell:filled:selected{
-fx-background-color:lightcyan; // Unfocused color
}
Produces something like this:
I am styling a checkbox inside a menu button, and i am trying to change the color of the box's background but it appears the size of it (red) is larger than the margin (yellow), how can I change the size or what would be the attribute to change? I tried changing the size attribute in CSS but it is not working.
.menu-item .check-box > .box {
-fx-background-color: red;
-fx-border-color: yellow;
}
Example Link