I noticed that every element which inherits from the Control class has an underscore at the bottom. You can see it in the picture:
When I put a focus on any of items, the line disappears. Why does it happen and how can I get rid of that?
Code:
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ComboBox?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<GridPane stylesheets="@sample.css"
xmlns:fx="http://javafx.com/fxml"
alignment="center">
<HBox>
<ProgressBar minWidth="100" progress="0.3"/>
<Button text="Button"/>
<Button text="Button"/>
<Button text="Button"/>
<ComboBox>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="String" />
<String fx:value="String" />
<String fx:value="String" />
</FXCollections>
</items>
</ComboBox>
</HBox>
</GridPane>
sample.css
.root {
-fx-background-color: gray;
}
Put inside the stop() method everything that needs to be executed before the JavaFX context terminates. With the System. exit(0) method, the application terminate abruptly.
@Override: @Override is an annotation that acts as a “hint for the compiler to let it know that you're overriding the method of a parent class” [Source]. launch: The launch method (line 23) is a static method defined in the Application class for launching a stand-alone JavaFX application.
To make the labels of the current pie chart invisible you need to invoke this method by passing the boolean value false as a parameter.
Almost all GUI platforms use a single threaded event dispatching model and JavaFX is no exception. JavaFX has a unique set of challenges when dealing with multithreaded programming. The reason is that JavaFX is primarily designed to work in a more or less linear fashion.
BorderPane lays out children in top, left, right, bottom, and center positions. The top and bottom children will be resized to their preferred heights and extend the width of the border pane. The left and right children will be resized to their preferred widths and extend the length between the top and bottom nodes.
It's to do with background insets. I had a look at the file modena.css
which is available in the jfxrt.jar
at com.sun.javafx.scene.control.skin.modena
. Your JavaFX project will necessarily include this JAR, so feel free to take a look at the file yourself.
I looked at the style for .button
and saw a line which I suspected might be causing this:
-fx-background-insets: 0 0 -1 0, 0, 1, 2;
The order of parameters goes: top, right, bottom, left. I decided to override the 'bottom' value in my sample.css
, changing the -1 to 0:
.button {
-fx-background-insets: 0 0 0 0, 0, 1, 2;
}
This solved the problem, but broke the focus highlight, so I copied and pasted the background insets from modena.css
for button:focus
, to give the following CSS file:
.root {
-fx-background-color: grey;
}
.button {
-fx-background-insets: 0 0 0 0, 0, 1, 2;
}
.button:focused {
-fx-background-insets: -0.2, 1, 2, -1.4, 2.6;
}
The result is:
Left is a button without focus, right is with focus.
You can do a similar thing for most of the other controls you wish to change, however the progress bar is a bit different because two things need to change:
.progress-bar > .bar
{
-fx-background-insets: 3 3 3 3;
/* ^ was 4 */
}
.progress-bar > .track
{
-fx-background-insets: 0, 0 0 1 0, 1 1 1 1;
/* ^ was 2 */
}
Some helpful things for "debugging" JavaFX are:
modena.css
, as mentioned aboveThis is the default style provided by the global Modena.css stylesheet (or Caspian.css if you are still on JavaFX 7).
From Modena.css:
.button,
.toggle-button,
.radio-button > .radio,
.check-box > .box,
.menu-button,
.choice-box,
.color-picker.split-button > .color-picker-label,
.combo-box-base,
.combo-box-base:editable > .arrow-button {
-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;
-fx-background-radius: 3px, 3px, 2px, 1px;
-fx-padding: 0.333333em 0.666667em 0.333333em 0.666667em; /* 4 8 4 8 */
-fx-text-fill: -fx-text-base-color;
-fx-alignment: CENTER;
-fx-content-display: LEFT;
}
If you don't like the default effect, you can:
Example 1:
.button{
-fx-background-color: gray;
}
Example 2:
.root{
-fx-inner-border: transparent;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With