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?
Related
Is it possible to have multiple Buttons set to Bottom (left, center, right)?
This is what I tried:
private Pane createPane() {
BorderPane rootPane = new BorderPane();
rootPane.setTop(createMenueBar());
rootPane.setCenter(createTableView(model.getIssues()));
rootPane.setBottom(createDeleteIssueButton());
rootPane.setBottom(createCloseIssueButton());
rootPane.setBottom(createCreateNewIssueButton());
BorderPane.setAlignment(deleteIssueButton, Pos.BOTTOM_LEFT);
BorderPane.setAlignment(closeIssueButton, Pos.BOTTOM_CENTER);
BorderPane.setAlignment(createIssueButton, Pos.BOTTOM_RIGHT);
return rootPane;
}
Result:
As you can see it only shows the last added Button. What is the best way to get this done with JavaFX/BorderPane? I'm very new to this so let me know if you need any more info!
Nested layouts
Gather the multiple buttons into a layout manager. Place that layout manager object in the bottom position of your BorderPane.
For example, you might choose FlowPane as your layout manager.
FlowPane buttons = new FlowPane() ;
buttons.getChildren().addAll( deleteIssueButton , closeIssueButton , createIssueButton ) ;
The BorderPane places only a single widget in the bottom slot. You want your container of buttons to be that widget.
BorderPane rootPane = new BorderPane();
rootPane.setBottom( buttons ) ;
Your use of Pos.BOTTOM_LEFT and such determines where the widget is placed within the bottom slot. The BOTTOM in BOTTOM_LEFT means bottom slot of the given space within a slot, not the bottom of the BorderPane. Two different bottoms involved here.
BorderPane.setAlignment( buttons , Pos.CENTER ) ;
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'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");
All dialog windows can be moved off screen (horizontally or vertically, doesn't matter). When this window is behind screen, one can continue dragging it further and the content of the screen will move.
It sounds difficult to understand, but at the end it looks like this:
The following changes in css don't help a lot:
body {
...
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
overflow-y: hidden;
overflow-x: hidden;
...
}
html {
overflow-y: hidden;
background-color: transparent;
}
Vertical and horizontal scroll bars don't appear when dialog window is moved there. But if there's a dialog window that has greater size than main screen, scroll bars also won't appear - and this is another problem.
How to prevent dialog window from moving off screen (fast example, dialog windows in google drive - they only move in visible part of screen)?
I came to solution I was looking for by overriding endDragging method of DialogBox. Here it is:
#Override
protected void endDragging(MouseUpEvent event)
{
//Move dialog window behind top border
if(this.getAbsoluteTop() < 0) {
this.setPopupPosition(this.getPopupLeft(), 0);
}
//Move dialog window behind bottom border
if(this.getAbsoluteTop() > (Window.getClientHeight() - this.getOffsetHeight())) {
this.setPopupPosition(this.getPopupLeft(), Window.getClientHeight() - this.getOffsetHeight());
}
//Move dialog window behind left border
if(this.getAbsoluteLeft() < 0) {
this.setPopupPosition(0, this.getPopupTop());
}
//Move dialog window behind right border
if(this.getAbsoluteLeft() > (Window.getClientWidth() - this.getOffsetWidth())) {
this.setPopupPosition(Window.getClientWidth() - this.getOffsetWidth(), this.getPopupTop());
}
super.endDragging(event);
}
When dialog box is moved off the screen, it appears in visible part of the screen after mouse release. One can improve it and override continueDragging in order to prevent window from moving off screen at all.
How can you change the color of a bar in a JavaFX BarChart?
I couldn't find a way to change the color through the css setStyle Method.
you can set color of bar using css
.default-color0.chart-bar { -fx-bar-fill: ***** }
.default-color1.chart-bar { -fx-bar-fill: ***** }
...
Using setStyle Method :
use lookupAll method in Node class,
Finds all Nodes, including this one and any children, which match the
given CSS selector. If no matches are found, an empty unmodifiable set
is returned. The set is explicitly unordered.
code :
//set first bar color
for(Node n:barChart.lookupAll(".default-color0.chart-bar")) {
n.setStyle("-fx-bar-fill: red;");
}
//second bar color
for(Node n:barChart.lookupAll(".default-color1.chart-bar")) {
n.setStyle("-fx-bar-fill: green;");
}
From JavaFX8 you can simply use the .chart-bar selector:
.chart-bar {
-fx-bar-fill: red;
}
Click in the Category axis or Number axis (which one you want to change color) > go the the editor (right side of scene builder) > choose color in tick label fill