How to set the distance to the border in a GridPane? - javafx

I am unable to find a way to set the distance to the border in a GridPane. Now, the text starts immediately after the border stops, which is not very nice for the design. I could give all the children a padding, but I was thinking there must be a shorter way to do this (just some property of the GridPane).
Unfortunately, I was not able to find this anywhere online. I prefer a solution which uses FXML or CSS (preferably CSS), but if this must be done with some Java code, it is not a problem.
I have already tried:
Setting the margin of the GridPane in the CSS:
#someGridPane { -fx-border-insets: 5; }, but these insets are outside the border (and I want them inside).
Setting padding on all of the child elements, which is not as efficient as I had hoped).
So the question is: How to set the distance to the border in a GridPane?
Notes:
I am using JavaFX8
If the solution is somewhere online already, please let me know. (I searched for it for some time now)

Depending on what's in your grid pane you could cheat a bit.
GridPane > Text {-fx-translate-x : 5;}
GridPane > Label {-fx-label-padding: 0 0 0 5;}
Of course, add some style class names, but you get the idea.

There is a simpler, more natural solution:
GridPane {
-fx-padding: 5;
-fx-hgap: 5;
-fx-vgap: 5;
}
It does the same as using GridPane.setHgap(5) etc, but with CSS.

Related

How to remove the rounded corners from JavaFX buttons

I've noticed that buttons in JavaFX have rounded corners, which means when you have a grid of them there are little white spaces visible between.
This illustrates the problem
I'd like to make my buttons appear as rectangles, with right angled corners, is this possible? I assume this might be possible with CSS, but I can't find this question being asked before.
Thanks.
You can do Via CSS :
"-fx-background-radius: 0"
You can add your CSS file in several ways by code , by Inline , External file
By Code :
Button rectangleButton = new Button();
roundButton.setStyle("-fx-background-radius: 0");
By FXML Inline :
By FXML External :
mystyle.css file
.button{
-fx-background-radius: 0;
}
then choose directory of file to apply the style to your container

Changing thumb length from CSS in scroll pane JavaFX8

I want to shorten the thumb in the scroll panes through CSS, I have used the min, max, and pref height but it does not seem to work, am I missing something or does this edit needs to be done at another level of the container (.track or .track-background).
.scroll-pane > .scroll-bar:vertical > .thumb{
-fx-pref-height: 5;
-fx-min-height: 5;
-fx-max-height: 5;
This cannot be done from CSS. The ScrollBar skin simply resizes the thumb to the size required based on visibleAmount and the track size. The CSS properties are simply ignored for the thumb.
It's unclear, why you would attempt to do something like this in the first place. If this worked, it would lead to unusual/unintuitive behavior of the ScrollBar, i.e. not being able to move the thumb down until the bottom reaches the end of the track and the size of the thumb not being chosen according to the size of the content of theScrollPane.
Unfortunately, as Fabian pointed out, it is not possible to do this using CSS. You can however do it by creating a copy of the ScrollBarSkin class and setting thumbLength explicitly there.
You then change the skin like so -
for (Node node : yourScrollPane.lookupAll(".scroll-bar")) {
ScrollBar s = (ScrollBar) node;
s.setSkin(new YourScrollBarSkin(s));
}
I would also recommend adding the following to disable the scrollbar when there isn't enough content to make it scrollable -
if(this.snapSizeY(Utils.clamp(this.minThumbLength(), this.trackLength * visiblePortion, this.trackLength)) == this.trackLength)
{
incButton.setDisable(true);
decButton.setDisable(true);
thumb.setDisable(true);
track.setDisable(true);
}

JavaFX: Textarea background color error

Here's the problem:
I tired to make the textarea from javafx have a black color, so i tried to add the parameter:
"-fx-background-color" with the value "black"
It did change something: Around the text area a black border appeared. I tried to change the background size with:
"-fx-background-insets" with the value "100" (for testing purposes, i know there are up to 4 values)
But nothing visual happend.
However, if i set the value to "-100", the screen 100 pixels outwards of the textarea is painted black. So, in theory, reversed parameter delivers the reversed result of what i want.
Therefore i ask: Why is it not working? I looked up other solutions, and they do it with the "-fx-background-color" parameter, so what a i missing here?
Use the following in an external css file:
.text-area .content {
-fx-background-color: black;
}
don't forget to include this css file, either through FXML or through code. You can use this tutorial.
I just found the solution to change the color of the background of TextArea in JavaFX. Write this in your controller class:
textarea.setStyle("-fx-control-inner-background: black;");
I was deep searching on the stackoverflow and eventually found it. The link is given below: Textarea javaFx Color
Happy coding!

JavaFX Grid lines styling

Is there a way to style the grid lines of a gridpane in JavaFx? I could only see the option to make it visible or hidden.
-fx-grid-lines-visible: true;
Even if we can set a background for each cell that would fill the entire cell just like in the following diagram, that would be great.
Is there really an option to do this either by CSS or by Java coding?
I added hgap and vgap to achieve this.
-fx-vgap: 10;
-fx-hgap: 10;
But it looks like this:-
I need to remove the intersecting lines as circled above. The required one would be like this:-

javaFX width css not reacting?

Im trying to get a dynamic scrollpane in my JavaFX (FXML) application using CSS. my CSS for the scrollpane looks like this:
.scroll-pane {
-fx-width: 80%;
-fx-height: 80%;
-fx-background-color: #FF3333;
}
the color works, but the size properties don't if I open the CSS in JavaFX scene builder it doesn't show the height and width lines at all.
I assume im missing something pretty basic, but I can't seem to figure it out.
As far as I know you cant use -fx-width/-fx-height at all and you cant use percentage values for it. The width and height of elements are read-only. You can set -fx-pref-width, -fx-pref-height, -fx-max-width, -fx-min-width, -fx-max-height and -fx-min-height to adjust the size of Java FX elements.
Check the CSS Reference Documentation out to see which CSS styles are possible.
If you want to have the size as percentage you probably need to bind the size to another property.
As example:
myView.prefWidthProperty().bind(myOtherView.widthProperty().multiply(0.75));
There is another post about percentage values with the use of a GridLayout.
How do specify a width percentage in JavaFX 2 using FXML?

Resources