I cannot figure out why, but the text on this is not centering on the screen no matter what I do, it is aligning on the left. please help.
public class FarmApp extends Application {
Scene scene;
HBox pane;
Stage gameStage;
/**
* {#inheritDoc}
*/
public void start(Stage stage) {
pane = new HBox();
Scene scene = new Scene(pane, 1500, 900);
createWelcome();
this.scene = scene;
gameStage = stage;
stage.setTitle("Chuggville");
stage.setResizable(false);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
} // start
/**
* Creates the welcome screen for Farm game
*/
public void createWelcome() {
HBox hbox2 = new HBox();
Text chugville = new Text();
chugville.setText("Chugville");
chugville.setFont(Font.font("Verdana", 90));
BorderPane centerText2 = new BorderPane();
centerText2.setCenter(chugville);
hbox2.getChildren().addAll(centerText2);
pane.getChildren().addAll(centerText2);
}
}
You have so much noise in your code that it's hard to know what's causing the issue.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App extends Application {
/**
* {#inheritDoc}
*/
#Override
public void start(Stage stage) {
BorderPane welcomeScreen = createWelcome();
Scene scene = new Scene(welcomeScreen, 1500, 900);
stage.setTitle("Chuggville");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
} // start
/**
* Creates the welcome screen for Farm game
* #return
*/
public BorderPane createWelcome() {
Text chugville = new Text();
chugville.setText("Chugville");
chugville.setFont(Font.font("Verdana", 90));
BorderPane centerText2 = new BorderPane();
centerText2.setCenter(chugville);
return centerText2;
}
}
Related
I have the main window (mainWindow.fxml) on top of which I want to display a transparent popup window (errorDialog.fxml) with 50% opacity so that the main window's content can still be seen underneath.
However, my attempts at making the background colour of overlay inside errorDialog.fxml transparent only results in the background colour being displayed as a solid 50% grey that hides the main window completely.
I tried to set the transparency both in the style attribute of "overlay" as well as in in the initialize method of controllerErrorDialog.java.
Any help is appreciated!
controllerMainWindow.java
package myPackage;
import [...];
public class controllerMainWindow extends AbstractController
{
#FXML
private Button btnOpenPopup;
#FXML
private BorderPane paneMainWindow;
//---------------------------------------------------------------------------------------------------
public void initialize()
{
}
//---------------------------------------------------------------------------------------------------
#FXML
public void handleButtonAction(ActionEvent event)
{
try {
if (event.getSource().equals(btnOpenPopup)) {
FXMLLoader errorLoader = new FXMLLoader();
errorLoader.setLocation(getClass().getResource("errorDialog.fxml"));
controllerErrorDialog errorController = new controllerErrorDialog();
errorLoader.setController(errorController);
Parent layout;
layout = errorLoader.load();
Scene errorScene = new Scene(layout);
Stage errorStage = new Stage();
errorStage.initStyle(StageStyle.UNDECORATED);
errorStage.setMaximized(true);
errorController.setStage(errorStage);
if(this.main!=null) {
errorStage.initOwner(main.getPrimaryStage());
}
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.setScene(errorScene);
errorStage.showAndWait();
}
}catch (IOException exceptionCockpitSettings) {
System.out.println("Error when switching to cockpitSettings.");
exceptionCockpitSettings.printStackTrace();
return;
}
}
//---------------------------------------------------------------------------------------------------
}
controllerErrorDialog.java
package myPackage;
import [...];
public class controllerErrorDialog extends AbstractController implements Initializable
{
#FXML
private BorderPane overlay;
private Stage stage = null;
//-------------------------------------------------------------------------------------------------------
#Override
public void initialize(URL url, ResourceBundle rb)
{
overlay.setStyle("fx-background-color: transparent");
}
//---------------------------------------------------------------------------------------------------
public void setStage(Stage stage) {
this.stage = stage;
}
//---------------------------------------------------------------------------------------------------
}
errorDialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import [...]?>
<BorderPane fx:id="overlay" prefWidth="1920" prefHeight="1080" style="-fx-background-color: rgba(0,0,0,0.5)" xmlns:fx="http://javafx.com/fxml">
<top></top>
<left></left>
<center></center>
<right></right>
<bottom></bottom>
</BorderPane>
You need to make sure:
The scene has a transparent background, with errorScene.setFill(Color.TRANSPARENT);
The stage is transparent, using errorStage.initStyle(StageStyle.TRANSPARENT);
Any content (that you explicitly want to see through) other than the root has fully transparent background
Here's a complete (albeit not very user-friendly) example:
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* JavaFX App
*/
public class App extends Application {
private Random rng = new Random();
#Override
public void start(Stage stage) {
Button createError = new Button("Try something dangerous");
createError.setOnAction(e -> {
try {
throw new Exception("Boom!");
} catch (Exception exc) {
BorderPane root = new BorderPane();
root.setTop(new Label("Error"));
Label stackTrace = new Label();
StringWriter sw = new StringWriter();
exc.printStackTrace(new PrintWriter(sw));
ScrollPane scroller = new ScrollPane(stackTrace);
stackTrace.setText(sw.toString());
root.setCenter(scroller);
Button close = new Button("Close");
HBox buttons = createHBox(close);
root.setBottom(buttons);
Scene errorScene = new Scene(root, 400, 400);
errorScene.setFill(Color.TRANSPARENT);
errorScene.getStylesheets().add(getClass().getResource("transparent.css").toExternalForm());
Stage errorStage = new Stage();
close.setOnAction(evt -> errorStage.close());
errorStage.setScene(errorScene);
errorStage.initStyle(StageStyle.TRANSPARENT);
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.initOwner(stage);
errorStage.show();
}
});
HBox buttons = createHBox(createError);
BorderPane root = new BorderPane(createContent());
root.setBottom(buttons);
Scene scene = new Scene(root, 600, 600);
stage.setScene(scene);
stage.show();
}
private HBox createHBox(Node... content) {
HBox buttons = new HBox(content);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(2));
return buttons;
}
private Node createContent() {
Pane pane = new Pane();
for (int i = 0 ; i < 15 ; i++) {
Circle circle = new Circle(
50 + rng.nextDouble() * 500,
50 + rng.nextDouble() * 500,
50 + rng.nextDouble() * 50,
randomColor());
pane.getChildren().add(circle);
}
return pane ;
}
private Color randomColor() {
return Color.color(rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), 0.5 + 0.25 * rng.nextDouble());
}
public static void main(String[] args) {
launch();
}
}
with transparent.css being:
.root {
-fx-background-color: #ffffff7f ;
}
.root HBox, .root .scroll-pane, .root .scroll-pane .viewport {
-fx-background-color: transparent ;
}
I'm learning Javafx layouts and I would like to obtain the following result:
HBox in the bottom center fo a Pane I have tried with the following code but I'm not able to do that
Can you please explain what I'm doing wrong? and How to achieve that?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestLayout extends Application {
#Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
Scene scene =null;
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
StackPane pane = new StackPane();
hbox.setStyle("-fx-background-color: #AAFAAA;");
hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
pane.getChildren().add(hbox);
scene = new Scene(pane, 600, 600);
pane.prefWidthProperty().bind(scene.widthProperty());
pane.prefHeightProperty().bind(scene.heightProperty());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class AppLauncherTest {
public static void main(String[] args) {
TestLayout.main(args);
}
}
I would Use a VBox or BorderPane as the root node. In this example, I use a VBox. I am assuming more nodes will go into this so I used a StackPane as the top node. This may need to be changed out or some other Pane may need to be added to this. That depends on what you are trying to do with the end product. For the bottom, I used an HBox and I set it's Margins.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Insets;
/**
* JavaFX App
*/
public class App extends Application {
#Override
public void start(Stage stage) {
StackPane subRootTop = new StackPane();
VBox.setVgrow(subRootTop, Priority.ALWAYS);
//subRootTop.setStyle("-fx-background-color: yellow;");
HBox subRootBotton = new HBox();
VBox.setVgrow(subRootBotton, Priority.ALWAYS);
subRootBotton.setStyle("-fx-background-color: green;");
subRootBotton.setMaxHeight(150);
VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
VBox root = new VBox(subRootTop, subRootBotton);
root.setStyle("-fx-background-color: red;");
Scene scene = new Scene(root, 600, 600);;
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
The binding of the StackPane is not size shouldn't be done. The scene automatically resizes its root to fill the whole area available. Assuming you want relative sizes, using StackPane is not really a good choice.
If you want the child to have a fixed distance to left right and bottom of the StackPane, you can do so specifying margins and alignment. Make sure the child does not grow to fit the parent though by setting the maxHeight to use the preferred height:
private void initUI(Stage stage) {
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
hbox.setMaxHeight(Region.USE_PREF_SIZE);
// could be calculated based on children instead of assigning
// an absolute value
hbox.setPrefHeight(30);
StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
StackPane.setMargin(hbox, new Insets(20));
StackPane pane = new StackPane(hbox);
pane.setStyle("-fx-background-color: #AAFAAA;");
Scene scene = new Scene(pane, 600, 600);
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
Maybe you can use an AnchorPane as a base layout instead of the StacKPane. I made a small exmaple how it would look like. Of cause you could keep the StackPane as the base layout and just put the AnchorPane with the HBox onto it, but you should definitly use an AnchorPane for your plan.
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private void initUI(Stage stage) {
// Create an anchor pane as base layout and set a color:
AnchorPane root = new AnchorPane();
root.setStyle("-fx-background-color: #FFFAAA;");
// Create a second container and set a minimum height and a color;
HBox hbox = new HBox();
hbox.setMinHeight(100d);
hbox.setStyle("-fx-background-color: #AAFAAA;");
// Give the child container a fixed location:
AnchorPane.setBottomAnchor(hbox, 50d);
AnchorPane.setLeftAnchor(hbox, 75d);
AnchorPane.setRightAnchor(hbox, 75d);
// Add the horizontal box to the base anchor pane:
root.getChildren().add(hbox);
stage.setScene(new Scene(root, 600, 600));
stage.show();
}
#Override
public void start(Stage stage) throws Exception {
initUI(stage);
}
public static void main(String[] args) {
launch(args);
}
}
Preview:
Currently my gridpane stays centred on the X axis while the window is resized, how can I get it to stay centred on the Y axis as well?
I have this in a tab in a TabPane:
GridPane grid = new GridPane();
grid.setPadding(new Insets(20, 0, 0, 0));
grid.setAlignment(Pos.CENTER);
grid.setHgap(20);
grid.setVgap(20);
this.setContent(grid);
The key here is VBox.setVgrow(tabPane, Priority.ALWAYS);
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication65 extends Application
{
#Override
public void start(Stage primaryStage)
{
VBox root = new VBox();
MenuBar mb = new MenuBar();
TabPane tabPane = new TabPane();
root.getChildren().addAll(mb, tabPane);
//StackPane stackPane = new StackPane();
CreateProfilePane cp = new CreateProfilePane();
tabPane.getTabs().add(cp);
VBox.setVgrow(tabPane, Priority.ALWAYS);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
I'm trying to keep the Elipsis String (...) on my Label (lbl) but I want it to be set to an empty String when the window gets too small. I'm using the code here to show an example. I'm using the ResizeHelper class found here: Allow user to resize an undecorated Stage.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* #author bparmeter
*/
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) {
Label lbl = new Label();
lbl.setText("Hello World");
AnchorPane root = new AnchorPane();
AnchorPane.setBottomAnchor(lbl, 0.0);
AnchorPane.setTopAnchor(lbl, 0.0);
AnchorPane.setRightAnchor(lbl, 0.0);
AnchorPane.setLeftAnchor(lbl, 0.0);
root.getChildren().add(lbl);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
ResizeHelper.addResizeListener(primaryStage);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Not the best solution but it works...
lbl.widthProperty().addListener((ov, o, n) -> {
final Text t = new Text(lbl.getText().charAt(0)+"...");
t.applyCss();
final double width = t.getLayoutBounds().getWidth();
lbl.setEllipsisString(width>=lbl.getWidth() ? "" : "...");
});
How to set on Mnemonic Parsing in same letter.In my project set Mnemonic in button, but button setText Change in every Event action,but Mnemonic is same in _o but short keys working only one event.How to solve this problem
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author user
*/
public class JavaFXApplication4 extends Application {
boolean b = false;
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Hell_o");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
b = !b;
if(!b){
btn.setText("Hell_o");
System.out.println("Hello");
} else {
btn.setText("w_orld");
System.out.println("world");
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
sorry for my English
You need to switch off mnemonic parsing before the text change, and after the change switch it on again. The method setText() does not implement any refreshing for mnemonic so it seems that all is done in the Scene.
public class JavaFXApplication4 extends Application {
boolean b = false;
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setMnemonicParsing(true);
btn.setText("Hell_o");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
b = !b;
btn.setMnemonicParsing(false);
if (!b) {
btn.setText("Hell_o");
System.out.println("Hello");
} else {
btn.setText("W_orld");
System.out.println("World");
}
btn.setMnemonicParsing(true);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}