JavaFX resizing webview problem on 2 monitors - java

I'm new to javaFX and I'm using the webview. Everything works fine on my small monitor and I can see all the content of the webview. But when I drag my application to my bigger monitor (1920 x 1080) resolution. I can only see a part of the webview.
This is how the WebView looks like on my bigger monitor:
As you can see i can not see all the content of the webview on my bigger monitor.
I than tried resizing the webview like this so I can see all the content on my bigger monitor:
double height = Screen.getPrimary().getBounds().getHeight();
double width = Screen.getPrimary().getBounds().getWidth();
StackPane root = new StackPane();
//Create WebView
WebView view = new WebView();
view.setMinWidth(width);
view.setMinHeight(height);
The problem now is that the webview height and width is to big for my smaller screen.
I'm displaying my webview in a tab of a tabpane like this:
tabPane.getTabs().add(tab1);
tab1.setContent(view);
How can I make it so that the webview size will adjust to the current screen the stage currently is?
Any kind of help is appreciated

First of all - I highly recommend you to work with SceneBuilder (Drag & Drop user interface design): https://gluonhq.com/products/scene-builder/
SceneBuilder stores the view in a FXML file, and you can use it as follows:
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
I also recommend you to work with IntelliJ:
https://www.jetbrains.com/help/idea/javafx.html
If you do so, you can set the value of pref width/height property to USE_COMPUTED_SIZE over the SceneBuilder GUI, that would solve your problem.
Another (ugly) solution without SceneBuilder would be:
setMinWidth(getWidth());
setMinHeight(getHeight());

Related

Why is element always blue surrounded when showing stage

Hey guys I have "bug" if you can call it so.
When starting the program, one element is always blue.
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("resource/Start.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Alc Calc V1.1");
stage.show();
}
=>
The reason that shows up as blue is it is the active element. JavaFX allows you to use CSS to style your program and if you do not put in your own it will use the default. In the default it has the fx-focus-color attribute set to adding that blue that you're referring to.
You can just get rid of the effect on all controls by changing the attribute itself in the code.
control.setStyle("-fx-focus-color: transparent;");
If you plan on changing more than just a thing or two I would recommend making your own CSS file and using that. You can attach it with this:
scene.getStylesheets().add("your_custom_css_file.css");
Then to set this attribute within your CSS file you would want to add this attribute:
.root { -fx-focus-color: transparent; }

How can I make a full screen java application?

I have an image viewer appplication. It works perfectly, but I'd like to make a full screen mode for it. It's an FXML project in Netbeans so, the main java is a separated file, therefore I cannot use this:
stage.setFullScreen(true);
because I can't reach the stage from my main .java file.
So I have a file, its name is imageViewer.java, it has this:
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("image_view.fxml"));
Scene scene = new Scene(root);
String css = this.getClass().getResource("style.css").toExternalForm();
scene.getStylesheets().add(css);
stage.setMinHeight(640);
stage.setMinWidth(960);
stage.setScene(scene);
stage.show();
}
and I have another file, it has fxml implements, and the ActionEvent void methods, you know, click the button, next picture etc...
The mentioned method (start method above) it's a method, therefore I can't return the stage. I have to reach the stage from image_viewController.java (the main java), what includes the other methods, functions and FXML implements etc...
I'd like to do this:
public void fullScreen(ActionEvent e) {
stage.setFullScreen(true);
}
But I can't reach the stage from another .java file. This is the first problem. And the second problem.
How can I make position absolute my panes, Vboxes etc like in html??? If my ImageView (what contains my image) get the full width and height, pulls down my HBox (what includes the buttons). If HBox were in absolute position, it wouldn't happened this, would it?
And the final problem, how can I make that, my HBox doesn't appear, just triggered by a hover effect. Is it possible with a separated css file? As I know, it is possible to make with FXML files.
Thanks for the answers!

Setting the background of a GridPane (with or without JavaFx Scene Builder)

I'm new to javafx and I am trying to set the background of a GridPane to an image(, or behind the GridPane). Setting the image in the fxml-file itself did not work. I tried to do this with the JavaFx Scene builder provided by oracle first and then later by manually adding this to the code with: <GridPane style="-fx-background-image: url('board.jpg' ); ..., but this resulted to nothing being changed when running the application.
I then resorted to setting the background image in the main-method and not the fxml-file. When I try to access the GridPane object (with getNamespace().get()) I get null back, so my problem here is that I can't seem to access the object to then set it's background to the image I want.
This what I tried most recently:
sample.fxml
<GridPane id="pain"...
main.java
FXMLLoader fxmlloader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = fxmlloader.load();
GridPane pane = (GridPane)fxmlloader.getNamespace().get("pain");
pane.setStyle("-fx-background-image: url('image.jpg');");
I already tried to find how to do this without success here:
JavaFX 2.0 + FXML - strange lookup behaviour
JavaFX How to set scene background image

Creating a control seems to break transparent stages on JFX8

The above program should create a transparent stage with some text, but the stage appears opaque:
public class Test extends Application {
#Override
public void start(Stage primaryStage) {
new TextArea(); //Comment this out to enable transparency
Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
Text text = new Text("Is this transparent?");
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box, 300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
}
}
The new TextArea() line is what breaks things - comment that out and it all works.
Creating any subclass of control (even via new Control() {};) breaks things - a Region or above does not.
This doesn't occur in Java 7 / JFX2.x.
I've created a JIRA for this since it seems a very obvious regression (https://javafx-jira.kenai.com/browse/RT-38938), but is anyone aware why this happens and thus how to work around it until a fix is provided? I've tried replicating this issue by copying the code in Control's constructor, but this seems to be fine - it's just instantiating Control itself that seems to break things.
I remember some forum discussion on this. I think the general gist is that creating a control forces css to be applied to the layout pane, and the layout pane is getting the opaque background.
As a workaround, make the background of the layout pane transparent:
box.setStyle("-fx-background-color: transparent;");

Java Fx whole Application in fullscreen

I want to create an Fullscreen Application on an RasperryPI.
But now i have a problem with the fullscreen mode, because everytime if I switch sites I must set the FullSreen property false and then true otherwise the window won't get fullscreen.
But if I switch the site the window is shortly not fullscreen and the it is fullscreen.
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.VERTICAL);
splitPane.getItems().addAll(table,vbLayout);
Scene scene = SceneBuilder.create().root(splitPane). build();
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.sizeToScene();
primaryStage.setFullScreen(false);
primaryStage.setFullScreen(true);
I hope you understand what i mean.
Best wishes
Johannes
You should not be able to switch sites when you are in fullscreen mode. If you are able to, you must first stop it. The user must come back to normal mode before doing anything else or you must force the normal mode. It's just like playing a video on fullscreen mode on youtube.
You Can Use Follwing Code:-
I Have Use Rectangle 2D Class
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.VERTICAL);
splitPane.getItems().addAll(table,vbLayout);
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = SceneBuilder.create().root(splitPane). build();
scence.setWidth(primScreenBounds.getWidth());
scence.setHeight(primScreenBounds.getHeight())
primaryStage.setScene(scene);
primaryStage.show();

Categories

Resources