Remove left padding of Alert - javafx

Consider this sample program which is adding a TabPane into an Alert. As you will see, there is a white padding on the left of the TabPane that I just cannot remove.
If anyone has any idea it will be great.
Code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class AlertTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new HBox());
primaryStage.setScene(scene);
primaryStage.show();
Alert alert = new Alert(Alert.AlertType.NONE);
alert.setTitle("");
alert.initOwner(primaryStage);
TabPane tabPane = new TabPane(new Tab("test"));
tabPane.setPadding(Insets.EMPTY);
alert.getDialogPane().setPadding(Insets.EMPTY);
alert.getDialogPane().setContent(tabPane);
alert.show();
}
}
Visual :

If you add css like this:
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new HBox());
primaryStage.setScene(scene);
//add this 3 lines
String css = Main.class.getResource("styles.css").toExternalForm();
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
...
}
and in styles.css add
.dialog-pane:no-header .graphic-container {
-fx-padding: 0; /* 10px 0px 0px 10px */
}
You can find more info about the default styles in file: fxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.css
and here is the result and full code demo:

Related

Mouse does not change to pointer on button hover CSS

I am trying to set the mouse to a pointer when a hover event is detected but the cursor does not change. If anyone can tell me what I am doing wrong I would be so grateful.
package replit;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
public class Main extends Application
{
#Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
vbox.setSpacing(20);
button button = new Button("Click");
button.setAlignment(Pos.CENTER);
vbox.getChildren().add(button);
Scene scene = new Scene(vbox, 300, 200);
scene.getStylesheets().add("Style.css");
primaryStage.setTitle("A Simple Scene!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The CSS
.button:hover{
-fx-cursor: pointer;
}

Javafx does not accept other fonts or languages in textArea of Text Field

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
public class textareasss extends Application {
#Override
public void start(Stage stage) throws Exception {
GridPane p = new GridPane();
TextArea tx= new TextArea();
p.add(tx,0,0);
Scene sce = new Scene(p,600,600);
stage.setScene(sce);
stage.show();
}
}
Link to the screen shot
Everytime i paste text in javafx Text Area or TextField, the text is not displayed but rather symbols like this:????.
I ran your code and it works, nothing wrong with JavaFX not accepting fonts or languages
this is what i used :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
GridPane gridpane = new GridPane();
TextArea text= new TextArea();
gridpane.add(text,0,0);
Scene scene = new Scene(gridpane,600,600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and this is the output i got :
TextArea containing text in many languages

Stage will not close with BorderPane after phone is rotated 90 degrees

I believe I've found a bug with BorderPane. The stage will close as expected if you do not rotate the phone. However, if you rotate the phone and then click on Close the screen does nothing until you rotate the phone back to the original position and then is displays the primaryStage. The code to recreate it is very simple.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
final Stage stage = new Stage();
Button closeBtn = new Button("Close");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
stage.close();
}
});
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Hello World!"), closeBtn);
Scene scene = new Scene(pane);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(primaryStage);
stage.setScene(scene);
stage.show();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setPrefHeight(Screen.getPrimary().getVisualBounds().getMaxY());
borderPane.setPrefWidth(Screen.getPrimary().getVisualBounds().getMaxX());
borderPane.setCenter(btn);
primaryStage.setScene(new Scene(borderPane));
primaryStage.show();
}
Does anyone know how to work around this issue?
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
}
}
apply plugin: 'org.javafxports.jfxmobile'
mainClassName = 'helloworld.HelloWorld'
version = '8u40'
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
jfxmobile {
ios {
forceLinkClasses = ['ensemble.**.*']
}
android {
applicationPackage = 'org.javafxports.ensemble'
}
}
Thank you for the hints. I removed the extra stage and scene. My workaround is this.
package helloworld;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
final Group group = new Group();
final BorderPane borderPane = new BorderPane();
borderPane.setPrefHeight(Screen.getPrimary().getVisualBounds().getMaxY());
borderPane.setPrefWidth(Screen.getPrimary().getVisualBounds().getMaxX());
final Button btn = new Button("Say 'Hello World'");
borderPane.setCenter(btn);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Button closeBtn = new Button("Close");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
group.getChildren().setAll(borderPane);
}
});
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Hello World!"), closeBtn);
group.getChildren().setAll(pane);
}
});
group.getChildren().add(borderPane);
primaryStage.setScene(new Scene(group));
primaryStage.show();
}
}
While I can reproduce your issue, and you can report it here, on mobile things are a little bit different from desktop, and you shouldn't create a different stage or a different scene: just switch the nodes on the scene instead.
If you create the project with Gluon's plugin, you'll notice it uses View nodes, and you can easily switch between them, but always related to the same root (GlassPane) on the same scene.
If you need dialogs, you can use JavaFX built-in Dialogs, but Gluon already has its own dialogs, more adapted to a mobile environment.

javafx fxml bring window to front form itself

I need to bring to front JavaFX FXML window from itself. Something like this:
procedure (boolean close)
{
if(close)
current_window.toFront();
}
How should I get this window(scene) ?
Try this
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
stage.toFront();
}
public static void main(String[] args) {
launch(args);
}
}
If you have access to any of the nodes, you can use the following
((Stage)node.getScene().getWindow()).toFront();

Removing object from scene in javafx

I am digging the documentation to see if there's a remove method, I just get this link whenever I google
http://www.coderanch.com/t/580998/JavaFX/java/remove-node
there's a simple remove option
Eg : .getChildren().remove(object)
It does not seem to work for me !
The code which you've provided works fine with me.
Add circle with ALT+Click, and remove them by simply clicking on them.
The reason I've used the ALT key for adding the circles is because in the below code, both the scene and the circles handle mouse clicks. Thus, the code has to know from where the event is coming from. This is just an example, of course.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ChangeListenerSample extends Application {
public static void main(final String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) throws Exception {
final Group root = new Group();
primaryStage.setResizable(false);
final Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(final MouseEvent event)
{
if (!event.isAltDown())
return;
final Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
circle.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(final MouseEvent arg0)
{
root.getChildren().remove(circle);
}
});
}
});
primaryStage.show();
}
}

Resources