Output formate changed java - 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.

Related

JavaFX: Text in button not centered when using custom font

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:

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

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();
}
}

How to return to code after I'm done with JavaFX scene

I'm launching my JavaFX scene as:
Applicaiton.launch(Main.class);
form my java code.
How to return to my code after I'm done with JavaFX!
Example:
public String Method()
{
Stirng s = "MyName";
Application.launch(Main.class);//here I lauch JavaFX scene
s.trim();//how to come back here after I'm done with that scene.
}
It's not supposed to work that way. You start by extending javafx.application.Application, then the entry point is start(Stage), which you must override. That method is the place where you have to set up the Scene for your stage, build the layout with Node's (buttons, layout managers, text fields, checkboxes), and register event handlers. You can access startup parameters with getParameters().
The application can be launched by providing the usual main() that calls launch(), a facility of JavaFX. So the minimal JavaFX application looks like:
public class MyApp extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
root.getChildren().setAll(new Label("Hello world!"));
Scene scene = new Scene(root, 600, 400);
// Add widgets and set up event handlers
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args) {
launch(args);
}
}
Platform.exit(); will do it click here for javafx2.2 documentation

Categories