Is is possible to separate javafx ui files? - java

I'm creating an app with javafx and I want to separate ui files.
Like: Tabs in separate file and just import and create new Tab in main app.
I have tried it, but when I try to set it on main app like borderPane.setCenter(tabPane); it says that The method setCenter(Node) in the type BorderPane is not applicable for the arguments (Tabs)
Main.java app code
import javafx.application.Application;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.*;
import javafx.stage.Stage;
import utils.Tabs;
public class Main extends Application {
#Override
public void start(Stage window) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);
BorderPane borderPane = new BorderPane();
Tabs tabs = new Tabs();
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabs);
root.getChildren().add(borderPane);
window.setTitle("Computer Security — Demos");
window.setScene(scene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
Tabs.java
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
public class Tabs extends TabPane {
public Tabs() {
TabPane tabPane = new TabPane();
Tab tab = new Tab("Tab: ");
HBox hbox = new HBox();
hbox.getChildren().add(new Label("Tab"));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);
tabPane.getTabs().add(tab);
}
}

Related

JavaFX TabPane reordering bug, looking for a workaround

I found a little bug in JavaFX TabPane, and am looking for a workaround. I am running JavaFX 13.0.1.
How it happens:
The TabPane's DragPolicy must be set to TabPane.TabDragPolicy.REORDER.
You can navigate between tabs via keyboard shortcuts CTRL + TAB & CTRL + SHIFT + TAB.
However, if I drag, say, the last tab to the very left and release it back to the position it was in (so that nothing should change), these keyboard shortcuts get messed up - no longer pointing to proper next/previous tabs.
You should be able to reproduce it simply with the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);
tabPane.getTabs().add(new Tab("First"));
tabPane.getTabs().add(new Tab("Second"));
tabPane.getTabs().add(new Tab("Third"));
tabPane.getTabs().add(new Tab("Fourth"));
tabPane.getTabs().add(new Tab("Fifth"));
StackPane root = new StackPane();
root.getChildren().add(tabPane);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TabPane bug");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
An interesting bug. It appears TabPane.getTabs() returns a List which may or may not reflect the visual ordering of the tabs. But the navigation keys always rely on the getTabs() order, not the visual order.
One workaround is to use a Label as a graphic for each Tab, while leaving the Tab’s text as null. You can then keep the Tabs sorted properly yourself, by checking the visual position of each such Label.
import java.util.List;
import java.util.ArrayList;
import javafx.beans.Observable;
import javafx.geometry.Bounds;
import javafx.geometry.NodeOrientation;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TabDragTest2 extends Application {
private static Tab createTab(String title) {
Tab tab = new Tab();
tab.setGraphic(new Label(title));
return tab;
}
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);
tabPane.getTabs().add(createTab("First"));
tabPane.getTabs().add(createTab("Second"));
tabPane.getTabs().add(createTab("Third"));
tabPane.getTabs().add(createTab("Fourth"));
tabPane.getTabs().add(createTab("Fifth"));
tabPane.getTabs().addListener((Observable o) -> {
List<Tab> tabs = new ArrayList<>(tabPane.getTabs());
NodeOrientation orientation = tabPane.getEffectiveNodeOrientation();
boolean ltr = orientation == NodeOrientation.LEFT_TO_RIGHT;
tabs.sort((t1, t2) -> {
Node title1 = t1.getGraphic();
Node title2 = t2.getGraphic();
Bounds title1Bounds =
title1.localToScene(title1.getLayoutBounds());
Bounds title2Bounds =
title2.localToScene(title2.getLayoutBounds());
if (tabPane.getSide().isHorizontal()) {
if (ltr) {
return Double.compare(
title1Bounds.getMinX(), title2Bounds.getMinX());
} else {
return Double.compare(
title2Bounds.getMaxX(), title1Bounds.getMaxX());
}
} else {
return Double.compare(
title1Bounds.getMinY(), title2Bounds.getMinY());
}
});
if (!tabPane.getTabs().equals(tabs)) {
Platform.runLater(() -> tabPane.getTabs().setAll(tabs));
}
});
StackPane root = new StackPane();
root.getChildren().add(tabPane);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TabPane bug");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

error when switching to Java 10

I made a javafx program using a horizontal SplitPane with a Canvas on one side in which something is drawn. When the SplitPane is resized the Canvas resizes with it.
Everything worked fine until I switched to Java 10. Suddenly the Canvas-side can only be expanded, not reduced.
Anyone has an idea why that is?
(The working Java version was 1.8.0_181)
package test;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
private static Display display;
#Override
public void start(Stage primaryStage) {
display = new Display();
StackPane stackPaneDisplay = new StackPane();
stackPaneDisplay.getChildren().add(display);
stackPaneDisplay.setStyle("-fx-background-color: white");
AnchorPane anchorPaneDisplay = new AnchorPane();
anchorPaneDisplay.getChildren().add(stackPaneDisplay);
AnchorPane.setBottomAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setTopAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setLeftAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setRightAnchor(stackPaneDisplay, Double.MIN_VALUE);
display.widthProperty().bind(stackPaneDisplay.widthProperty());
display.heightProperty().bind(stackPaneDisplay.heightProperty());
StackPane stackPaneLeft = new StackPane();
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.getItems().addAll(stackPaneLeft, anchorPaneDisplay);
BorderPane root = new BorderPane();
root.setCenter(splitPane);
Scene scene = new Scene(root);
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show();
splitPane.setDividerPositions(0.2);
}
public static void main(String[] args) {
launch(args);
}
}
and the Canvas-Class
package test;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class Display extends Canvas {
GraphicsContext gc = this.getGraphicsContext2D();
public Display() {
widthProperty().addListener(e -> {
draw();
});
}
public void draw() {
gc.strokeOval(500, 500, 100, 100);
}
}
Thanks in advance

Keep stage maximised after scene change

When I change scenes on my stage with the following code, my stage changes size. However the button in the top right to maximize/minimize the windows says that the stage is still maximized even though it is clearly not.
How am I able to keep the stage maximized when a scene change happens?
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Program2 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
try {
StackPane p = new StackPane();
primaryStage.setTitle("Chart Application");
Label loader = new Label("Loading...");
loader.setGraphic(new ImageView(new Image("https://media.giphy.com/media/FmcNeI0PnsAKs/giphy.gif")));
loader.setFont(new Font(35));
p.setStyle("-fx-background: #FFFFFF;");
p.getChildren().add(loader);
StackPane.setAlignment(loader, Pos.CENTER);
primaryStage.setScene(new Scene(p));
primaryStage.setMaximized(true);
Task<VBox> task = new Task<VBox>() {
#Override
public VBox call() {
VBox result = new VBox();
for(int i = 0; i < 50000; i++) { //Here simply for small delay
result.getChildren().add(new Label(Integer.toString(i)));
}
return result ;
}
};
task.setOnSucceeded(e -> {
VBox result = task.getValue();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(result);
scrollPane.setStyle("-fx-background: #FFFFFF;");
primaryStage.setScene(new Scene(scrollPane));
});
new Thread(task).start();
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
As this is operating system specific I image I am using Windows 10 with JDK 8 u112 and JavaFX 8 with the e(fx)clipse plugin for eclipse
Instead of replacing the scene, use the same scene and replace its root:
primaryStage.getScene().setRoot(scrollPane);

Why doesn't button add to scene?

package application;
import java.awt.Button;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
public class Main extends Application {
Button button; //Declare Button
public static void main(String[] args) {
launch(args); //calls application
}
#Override
public void start(Stage primaryStage) throws Exception {
//comes from application pack
primaryStage.setTitle("Title Of Window"); //Main Stage
button = new Button (); //Creates Button
button.setLabel("Click Me");
StackPane layout = new StackPane();
layout.getChildren().add(button); //Button in Scene
Scene scene = new Scene (layout, 300, 250); //Sets Scene
primaryStage.setScene(scene); //Stage and Scene
primaryStage.show();
}
Hey guys, so this is first time I am creating something in JavaFX and I was wondering why doesn't my button in Scene add in Layout.GetChildren() line, it keeps on displaying red line underneath add. I am using Eclipse IDE.
you have imported import java.awt.Button, you must import Button class in the javafx package.

How to change float of this button?

HiEveryone,
I'm new Javafx and trying to change the float of this button as to Right side from the current Left side.. How to do that?
I'm using simply Pane as a container at the moment.
Have a look into this screenshot:
How to do that? ..pls help
Thanks in advance!
EDITED
Source code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
public class Main extends Application {
#Override
public void start (Stage primaryStage){
primaryStage.setTitle("Modern web browser made by Rajendra arora.");
WebView wv = new WebView();
wv.setLayoutX(0.0);
wv.setLayoutY(40.0);
wv.setPrefHeight(Double.MAX_EXPONENT);
wv.setPrefWidth(Double.MAX_EXPONENT);
WebEngine we = wv.getEngine();
we.load("http://www.google.com/");
Button btn=new Button("Go");
TextField tf = new TextField("http://www.google.com/");
tf.setLayoutX(1.0);
tf.setPrefWidth(Double.MAX_EXPONENT);
tf.setPrefHeight(25.0);
Pane sp = new Pane();
sp.getChildren().add(tf);
sp.getChildren().add(btn);
sp.getChildren().add(wv);
Scene scene = new Scene(sp, 600, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Use a HBox to hold the textfield and button, later add it to the Pane
HBox hbox = new HBox();
hbox.getChildren().addAll(tf, btn);
Pane sp = new Pane();
sp.getChildren().add(hbox);
sp.getChildren().add(wv);

Categories

Resources