JavaFX: Text in button not centered when using custom font - java

I want to use the "Another Danger" font in my project. However JavaFX isn't quite liking it. Labels and buttons seem to estimate the height of the letters wrong and take a completely wrong size.
I wrote a simple example illustrating the problem:
public class HelloApplication extends Application {
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) {
Button button = new Button("Test");
Scene scene = new Scene(button, 320, 240);
button.setFont(Font.loadFont(HelloApplication.class.getResourceAsStream("/fonts/AnotherDanger-Demo.otf"), 30));
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
The text in the button is clearly not centered. However, when I remove the custom font everything works perfectly. Is this a problem with the font? And if so is there any way to fix this?
I'd be very thankful for any help here.
EDIT: Here are screeshots as requested:
With the font:
Without the font:

Related

My Scene changes its color once I add element to it [duplicate]

I have a program that first starts up a little splash screen, and after 5 seconds it continues to a new Stage.
The strange thing is, until now it worked fine, but now that I'm starting to build the new Stage I'm declaring new Nodes in the class to use it.
Now all of the sudden the splash screen has a white background, commenting the before created nodes out will revert the image back to being transparent.
public static Pane pane = new Pane(); //this is fine
//Uncommenting this Node will change the splash screen background white
//public static TextArea textArea = new TextArea();
public static Image splashImage = new Image(Class.class.getClassLoader().getResourceAsStream("image.png"));
public static ImageView splashImageView = new ImageView(splashImage);
public static BorderPane splashPane = new BorderPane(splashImageView);
public static Scene splashScene = new Scene(splashPane);
#Override
public void start(Stage primaryStage) throws Exception {
splashScene.setFill(Color.TRANSPARENT);
primaryStage.setScene(splashScene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
}
Here a shortened version of my code
Could this be because of memory issues or something? I have never encountered anything like this.
Any help is appreciated.
The default style sheet is only loaded if a control (i.e an instance of Control or one of its subclasses) is created. (The idea behind this is to avoid the performance overhead of loading CSS for applications that manage all their own graphics and don't use any controls, such as games or simulations.)
The default stylesheet sets the background color of the root node (the splashPane in your example) to a very light grey (specifically, it is 26.4% brighter than the color #ececec).
Since the text area is the only control in your class, creating it causes the default style sheet to be loaded, which sets the background color of the splashPane to a very light grey.
If you need to instantiate controls and want the background of the pane to be transparent, you need to specify that in an external CSS:
splashStyle.css:
.root {
-fx-background-color: transparent ;
}
And
#Override
public void start(Stage primaryStage) throws Exception {
splashScene.setFill(Color.TRANSPARENT);
splashScene.getStylesheets().add("splashStyle.css");
primaryStage.setScene(splashScene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
}
(As a quick check that this will work, you can just test with
splashPane.setStyle("-fx-background-color: transparent; ");
)

RichTextFX: Highlighting a Word in StyleClassedTextArea not working

I am new to RichTextFX and need some help. I want to use StyleClassedTextArea (see https://github.com/FXMisc/RichTextFX).
My simple java code:
public class GuiMain extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
StyleClassedTextArea styleClassedTextArea = new StyleClassedTextArea();
String exampleString = "This is a WARNING for an INFO! Please stay tuned";
styleClassedTextArea.appendText(exampleString);
styleClassedTextArea.setStyle(10, 16, Collections.singleton("-fx-font-weight: bold; -fx-highlight-fill: #B22224;"));
styleClassedTextArea.setEditable(false);
Scene scene2 = new Scene(new StackPane(styleClassedTextArea), 600, 400);
primaryStage.setScene(scene2);
primaryStage.setTitle("");
primaryStage.setMaximized(false);
primaryStage.show();
}
}
But when I run my java program, I just get the following:
However, as the picture shows, nothing is highlighted.
Does anyone know how I can render the text in a certain range (from, to) in red (or what I have done wrong)?
For all of you who are facing the same problem:
Instead of -fx-highlight-fill, you can use RichTextFX with -rtfx-background-color: #...
Solution found here: Text background color in RichTextFx CodeArea

Output formate changed java

I am new in Java. I am trying to learn to make some view in javafx. Here, I am trying to write some simple code to create button in a scene. Button is created successfully but the issue is not showing output properly. And I don't know how to search this type of issue but I look javafx documents. But, didn't find any solution.
Here is my code.
public class PlayingCard extends Application {
#Override
public void start(Stage primaryStage) {
System.out.println("hello world");
primaryStage.setTitle("Button Title");
Button btn = new Button("Click HERE");
StackPane layout = new StackPane();
layout.getChildren().add(btn);
Scene scena = new Scene(layout, 600, 300);
primaryStage.setScene(scena);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
I know there is nothing wrong in the code. I missed some setting. Here I am posting output screenshot.
And addition, when I add imageview with some image the same thing like output was not shown.

JavaFx Not able to pass a value from a class to configure setScene

I am a Newbie trying to learn java and I am a complete beginner at java Fx so please forgive the noob question but I am trying to structure my code more cleanly by having all my buttons in a class but I know that I have to have the variable 'button' in a StackPane but when I do it it tells me that java can't find the variable. if someone can help me then that would be greatly appreciated. (also when the button is not in another class the code worked)
//this class is for drawing the buttons
public void Buttons() {
Button button = new Button("My Button");
}
public static void main(String[] args) { launch(args); }
#Override
public void start(Stage primaryStage) {
//this sets the Title for the window
primaryStage.setTitle("Calcultor");
//this calls the class that draws the buttons on screen
Buttons();
StackPane root = new StackPane();
root.getChildren().add(button);
//this cerates a stage and sets the window size
primaryStage.setScene(new Scene(root, 300, 400));
//this refreshes the window
primaryStage.show();
}
}

Unresizable ever JavaFX Window

My problem is that my JavaFX window expands further than it should at the bottom, thus showing a weird blank background...
I set stage.setResizable(false) but it doesn't seem to work. I also set
stage.setMinWidth(1280);
stage.setMaxWidth(1280);
stage.setMinHeight(800);
stage.setMaxHeight(800);
I think it's due to some ImageView bigger than the window, but I really would like my window to stick to the max I set and be absolutely not resizable ever.
Here's what it looks like : click here
I also noticed it happens only when I set stage.initStyle(StageStyle.TRANSPARENT).
But I don't want my window to be decorated with stage.initStyle(StageStyle.DECORATED)... :(
Thank you in advance for your help. :)
Have you tried directly using setWidth and setHeight:
stage.setWidth(1280);
stage.setHeight(800);
I think the setMaxWidth, setMinHeight... etc. only works on a decorated screen because its main purpose is to limit the sizes the user can set the window. Here's my example:
public class FixedWindowSize extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
Pane p = new Pane();
p.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
p.setMinSize(6000, 6000); //Make this go out beyond window bounds.
Scene s = new Scene(p);
primaryStage.setScene(s);
primaryStage.initStyle(StageStyle.TRANSPARENT);
//This doesn't work
//primaryStage.setMinWidth(1280);
//primaryStage.setMaxWidth(1280);
//primaryStage.setMinHeight(800);
//primaryStage.setMaxHeight(800);
//This should work.
primaryStage.setWidth(1280);
primaryStage.setHeight(800);
primaryStage.show();
}
}

Categories