I have a JavaFX interface generated by SceneBuilder...so I have my view components in sample.fxml.
When I start the application it looks like this : http://i.imgur.com/3tj0tN9.png
In that blue pane I want to add the browser ( with JxBrowser library), so I did this:
public void loadMap(ActionEvent actionEvent) {
initComponents(); //setting my buttons visibility
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
gamePane.getChildren().add(browserView);
browser.loadURL("http://www.google.com");
gamePane.getChildren().add(new Button("random"));
}
This is my main:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Google Maps ");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
That button was added, but my browser won't appear..What can I do?I used this in Swing and it worked,but here seems not.
The browser is loaded(because I get the specific messages in console) but isn't displayed.
Related
I'm creating a JavaFX application (on OSX) which consists of a stage.
It should neither be allowed to resize it manually nor to enter full screen via the maximize button. This is currently achieved by stage.setResizable(false) which works fine.
The problem occurs when adding an Alert: After closing the Alert, the maximize button becomes targetable again, making it possible to enter full screen.
Here's a simple example of code:
// imports
public class MyApplication extends Application {
public static void main(String[] args) { launch(args); }
#Override
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(new StackPane()));
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Close?");
if (alert.showAndWait().get().equals(ButtonType.CANCEL)) {
event.consume();
}});
primaryStage.show();
}
}
Does anyone know why this is happening?
When I load a site / html using javafx.scene.web.WebView that site seems to be affected by my scene custom styling. A minimal example to demonstrate the issue.
Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
primaryStage.setScene(new Scene(root, 900, 900));
String css = Main.class.getResource("/test.css").toExternalForm();
primaryStage.getScene().getStylesheets().add(css);
WebView webView = new WebView();
root.getChildren().add(webView);
webView.getEngine().load("http://google.pl");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
test.css
.text-area,
.text-field {
-fx-background-color: red;
}
This results in
Ultimately I wish for a method like webview.getEngine().dontInheritStyles()of course there is none and I couldn't find any method of doing it otherwise. Tried:
webView.getStylesheets().clear();
webView.setStyle(null);
webView.getStyleClass().clear();
none of them worked. One way that I think could make this work (haven't tried it yet tho) would be to open the webview in a sub window which doesn't use the same scene, however I want the webview to be embeded in my existing application view so that option would be my last resort and I rather avoid it.
You can use some kind of hack, such as a combination of JavaFX and Swing.
You have two classes:
JFXPanel - which allows you to embed a JavaFX control into Swing
SwingNode - which allows you to embed a Swing control into JavaFX
You can combine the use of the JFXPanel and SwingNode classes in the wrapper class:
public class Styleless<T extends Parent> extends StackPane {
private T root;
public Styleless(T root) {
this.root = root;
JFXPanel jfxPanel = new JFXPanel();
jfxPanel.setScene(new Scene(root));
SwingNode swingNode = new SwingNode();
swingNode.setContent(jfxPanel);
getChildren().add(swingNode);
}
public T getRoot() {
return root;
}
}
And you can use it like this:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
primaryStage.setScene(new Scene(root, 900, 900));
String css = Main.class.getResource("/test.css").toExternalForm();
primaryStage.getScene().getStylesheets().add(css);
WebView webView = new WebView();
webView.getEngine().load("http://google.pl");
Styleless<WebView> webViewStyleless = new Styleless<>(webView);
root.getChildren().add(webViewStyleless);
VBox.setVgrow(webViewStyleless, Priority.ALWAYS);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You can also use style classes in your CSS to manage which elements get styles applied to them. Define a 'myapplication' class in your css, and add that class to your root node.
test.css
/* any text fields inside a container with the myapplication style class */
.myapplication > .text-field{
-fx-background-color: red;
}
/* any text areas inside a container with the myapplication style class */
.myapplication > .text-area{
-fx-background-color: red;
}
Main.java
...
root.getStyleClass().add("myapplication");
TextField txtField = new TextField("Application text field");
root.getChildren().addAll(webView, txtField);
...
Using the '>' css selector (https://www.w3schools.com/cssref/css_selectors.asp) this way will apply the style to text fields whose parent has the style class 'myapplication'. When a WebView is created, it has the style class 'web-view' (https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#webview)
While there is no real way to prevent style values from being inherited, style classes are not added to children.
I can't find a clear example of how to write the Main class in the JavaFX application to use the fxml files created in Scene Builder, the part that loads and shows the stage and the scene. Can someone please show me one? I have created 7 different screens and controllers for my application, but the main class has me stumped. This is a different question than just creating the fxml markup in the main class.
Main class with main method:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
String fxmlResource = "MainWindow.fxml";
Parent panel;
panel = FXMLLoader.load(getClass().getResource(fxmlResource));
Scene scene = new Scene(panel);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
How can I add a swingNode to a specific pane using my controller class?
I'm trying to add a JPanel that loads a network graph created using the JUNG software library I'm not sure how to do it.
You can achieve what you want to do using SwingNode class. Here is an example code where a JButton (swing component) is added to a StackPane (JavaFX component):
public class SwingFx extends Application {
#Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setScene(new Scene(pane, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
swingNode.setContent(new JButton("Click me!"));
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Source: http://download.java.net/jdk8/jfxdocs/javafx/embed/swing/SwingNode.html
I'm launching my JavaFX scene as:
Applicaiton.launch(Main.class);
form my java code.
How to return to my code after I'm done with JavaFX!
Example:
public String Method()
{
Stirng s = "MyName";
Application.launch(Main.class);//here I lauch JavaFX scene
s.trim();//how to come back here after I'm done with that scene.
}
It's not supposed to work that way. You start by extending javafx.application.Application, then the entry point is start(Stage), which you must override. That method is the place where you have to set up the Scene for your stage, build the layout with Node's (buttons, layout managers, text fields, checkboxes), and register event handlers. You can access startup parameters with getParameters().
The application can be launched by providing the usual main() that calls launch(), a facility of JavaFX. So the minimal JavaFX application looks like:
public class MyApp extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
root.getChildren().setAll(new Label("Hello world!"));
Scene scene = new Scene(root, 600, 400);
// Add widgets and set up event handlers
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args) {
launch(args);
}
}
Platform.exit(); will do it click here for javafx2.2 documentation