I am writing a JavaFX application using JXBrowser. I seem to be having some trouble however figuring out how to add the BrowserView component into Scene Builder.
I have tried importing all of the .jar's into Scene Builder and attempted to create a custom component for it but to no avail.
I have exhausted all of my google fu.
Tested on Scene Builder 8 and 10.
Tested Java Sources 8 through 10.
JxBrowser supports BrowserView import only via the code or FXML file. Import via the SceneBuilder is not supported as specific requirements should be met in order to import BrowserView using SceneBuilder.
Import via the code is performed in the following way. Please check the example below.
#Override
public void start(final Stage primaryStage) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
Scene scene = new Scene(new BorderPane(view), 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
browser.loadURL("http://www.google.com");
}
Import via the FXML file is described in this article, please take a look at the FXML section: https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013071-using-jxbrowser-in-javafx
Related
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());
I'm trying to create a simple data entrty application with javafx and ran into a problem when adding a scene control. The display loses it's fill colour even BEFORE i've added the control to the scene! Simply instantiating the control breaks it.
I was running on Oracle java 8 on windows, but I;ve tried OpenJDK 8 on Windows and OpenJDk/OpenJFX 13 on linux and all behave identically. I stripped out the code to the bear minmium to recreate the problem.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class BasicTest extends Application {
#Override
public void start(Stage primaryStage) {
Rectangle r = new Rectangle();
r.setWidth(200);
r.setHeight(50);
r.setFill(Color.BLUE);
r.setStroke(Color.WHITE);
r.setStrokeWidth(2);
Text t = new Text();
t.setText("Confirm");
t.setFill(Color.WHITE);
t.setFont(Font.font("null", 40));
StackPane sp = new StackPane(r);
sp.getChildren().add(t);
sp.setMaxWidth(200);
t.setTranslateY(-2);
Label b = new Label("Click me");//Comment this line out after first run
Scene scene = new Scene(sp, 300, 200);
scene.setFill(Color.BLUE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
With the label commented out the scene background is blue so I get a white "Confirm" with white outline. Just adding the label constructor will make the scene background go grey.
Thanks for all the comments. Many apologies if my wording was confusing. I did add some images to show the difference but they seem to have disappeared. Anyway my thanks to #Matt for their very succinct description (which I wish I'd have thought of!). I like the mouse click idea too.
I was probably being very naughty asking this "question" on SO when I firmly believe this is a bug in javafx. Instantiating an object that goes no where near (yet) the scene graph should have no effect on it in my opinion - CSS or no CSS. I will raise a bug against javafx.
However I knew the power of SO would help me and it has!
Thanks to the clue by #jewelsea I simply replaced my scene.setFill() (which was only a test anyway) with CSS and the problem is circumvented. I can even add the control to the scene now and it works as expected. From the JavaFX CSS Reference:
"The Scene object has no settable CSS properties, nor does it have any pseudo-classes. However, the root node of the scene is assigned the style class "root" (in addition to style classes already assigned to the node). "
So I set up in my css:
.root {
-fx-background-color: blue;
}
Another way to circumvent this is to simply set the background of the StackPane to BLUE:
sp.setBackground(new Background(new BackgroundFill(Color.BLUE, null, null)));
I am new to this Javafx. I need to know rtsp supported library for Javafx I knew few of the media library GStreamer and VLCJ. I don't know which will support this features in Javafx. I have read many portals which I cant get an answer. many of them posted you cant run RTSP in Javafx. please guide me which library is best for RTSP that will support for Javafx.
File f = new File("Video.mp4");
new NativeDiscovery().discover();
EmbeddedMediaPlayerComponent playerComponent = new EmbeddedMediaPlayerComponent();
/*I dont know where to add the playerComponent as we do in swing*/
MediaPlayer mp=playerComponent.getMediaPlayer();
StackPane root = new StackPane();
Scene scene = new Scene(root, 700, 700);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
This is the normal video player in Javafx.
This might be a late answer but I saw the quetsion while I had the same quetsion so check this site out I think it got all the things I have to say https://github.com/mhrimaz/AwesomeJavaFX/blob/master/README.md#frameworks
I have a basic application, sort of like a toy box, it doesn't really do anything except let you move shapes around a screen, if you double click the shape, it brings it into the foreground. There is also a menu with menu Items "New, "open", "Save" -
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class RandCMove extends Application
{
public static void main(String [] args)
{
launch(args);
}
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Menu fileMenu = new Menu("File");
MenuItem newMenuItem = new MenuItem("New");
MenuItem openMenuItem = new MenuItem("Open");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
fileMenu.getItems().addAll(newMenuItem,openMenuItem,saveMenuItem,
new SeparatorMenuItem(),exitMenuItem);
menuBar.getMenus().add(fileMenu);
Rectangle rect = new Rectangle();
rect.setWidth(200);
rect.setHeight(200);
rect.setArcHeight(20);
rect.setArcWidth(20);
rect.setFill(Color.RED);
rect.setX(200);
rect.setY(100);
root.setTop(menuBar);
root.getChildren().add(rect);
Circle circle = new Circle(
300,300,100);
Text text = new Text(150,150,"Text");
Font phosphate = Font.font("Phosphate",150);
text.setFont(phosphate);
text.setTranslateY(circle.getBoundsInParent().getMinY()+10);
root.getChildren().add(text);
//Positions the circle under the rectangle
circle.setTranslateY(rect.getBoundsInParent().getMinY()+30);
root.getChildren().add(circle);
// Moves shapes depending on if the cursor on the particular shape
// Brings shape to the front using double click
root.setOnMouseMoved(e ->
{
if(rect.contains(e.getX(),e.getY()))
rect.setOnMouseDragged(f ->{
rect.setX(f.getX());
rect.setY(f.getY());
});
rect.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
rect.toFront();
});
if(circle.contains(e.getX(),e.getY()))
circle.setOnMouseDragged(f->{
circle.setCenterX(f.getX());
circle.setCenterY(f.getY());
});
circle.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
circle.toFront();
});
if(text.contains(e.getX(),e.getY()))
text.setOnMouseDragged(f ->{
text.setX(f.getX());
text.setY(f.getY());
});
text.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
text.toFront();
});
});
Scene scene = new Scene(root,600,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
I want to be able to save the current state of the shapes on screen, and have them load up as they were upon pressing "Open".
I can't seem to find anything useful on this, can anyone provide some guidance or point me in the right direction? Any assistance would be appreciated!
Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences.
More at:
http://docs.oracle.com/javase/8/docs/technotes/guides/preferences/
This is a non-trivial task. I am not going to provide code for it here, but only a general approach outline:
Serialize your shapes to FXML (you will need to code the serializer yourself).
Each time the user edits the shapes, serialize the to FXML again and save the FXML as a preference (as outlined in Frank Fotangs answer).
When the user first opens the application, read the current preference containing the FXML for the current shapes and load them up using an FXMLLoader.
The same technique can be used for implementing your Open and Save menu options, only instead of writing the serialized data to a preference you will write it to a file location chosen by a FileChooser.
I would really prefer to do this the FX way rather than Fxml.
I think you misunderstand the problem that you need to solve. There is no "FX way" of doing this.
Your problem is that you need to serialize and deserialize nodes from a scene graph, for which the core JavaFX API does not provide a full implementation.
When you create a scene graph using JavaFX using JavaFX APIs, it is an in-memory representation of your scene, only available while your program is executing. What you need to be able to do is persist that scene to a file so that when you shut down your application the data representing the scene is stored somewhere. Then you need to read the data from the file and recreate the scene in memory when the user starts the application back up again. The only way to do this is to serialize the objects to storage when needed and deserialize the objects from storage when the application starts back up again. Read the wikipedia article on serialization to understand the conceptual process.
Now you don't need to use FXML for your serialized format, you could use other formats such as JSON or YAML and other serialization support libraries such as Jackson. However, to me, FXML seems the most logical format to choose because JavaFX ships with the FXMLLoader, which provides an easy to use API to transfer your serialized data (an FXML file representing your scene) into an in-memory structure (the scene graph), via a simple call to the load method.
What makes this task tricky rather than trivial is that the JavaFX API only ships with an FXMLLoader, it does not ship with a corresponding class (e.g. an FXMLSaver), which does the opposite work of taking the in-memory structure (the scene graph) and creating serialized data from it (an FXML file). So you will need to write such logic yourself.
Now, SceneBuilder is an open source project and does include logic to save a scene graph to an FXML file. For instance you can find source code for an FXMLSaver in the SceneBuilder source. Perhaps you could use or adapt that code to save your scene graph objects into a serialized format. In doing so, you wouldn't be using the scene builder application to create and save your scene, instead you would be adapting SceneBuilder source code to be included in your own application which would be creating and saving your scene.
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;");