In my JavaFX implementation I use a pane, where I want to open a webview, when a button on the pane is clicked.
But however with this snippet it does not work:
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.load("http://google.com");
button.setOnAction((event) -> {
webEngine.load(url.toExternalForm());
});
I've got this to call fxml:
Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
Any hint? thx
You have to add your pane to the root and add your webview to the pane. Also, look at this button handler compared to yours.
Button btn = new Button();
btn.setText("Say 'Hello World'");
WebView webview = new WebView();
WebEngine webengine = webview.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
webengine.load("http://www.google.com");
}
});
VBox root = new VBox();
root.getChildren().addAll(btn, webview);//I don't see where you are doing this
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
Related
I have setContextMenuEnabled(true), but it doesn't work, no context menu is available!
FXCanvas canvas = new FXCanvas(composite, SWT.EMBEDDED);
StackPane root = new StackPane();
webView = new WebView();
webView.setContextMenuEnabled(true);
root.getChildren().add(webView);
Scene scene = new Scene(root);
canvas.setScene(scene);
Can anyone help?
please i am trying to settext on a textfield after it as been open from login window but the changes it not displaying pls am new to java and javafx below is the code wich open the new window
try {
Statement stmnt= conn.createStatement();
rs=stmnt.executeQuery(sql);
if(rs.next()==true){
String name = null;
Image img = null;
name = rs.getString("name");
img = new Image(rs.getBinaryStream("photo"));
System.out.println(name);
Stage primaryStage = new Stage();
/*FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("main.fxml").openStream());
mainController mnctrl = (mainController) loader.getController();
mnctrl.dispOpInfo(name, img);*/
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent)fxmlLoader.load();
mainController controller = fxmlLoader.<mainController>getController();
controller.dispOpInfo(name, img);
primaryStage.show();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("BECE 2017 Validation");;
primaryStage.getIcons().add(new Image(this.getClass().getResourceAsStream("icon.png")));
primaryStage.show();
//
}
and below is the code for the newly open window of it textfield is not changing at runtime
#FXML TextField txtSchName = new TextField();
public void populateCanInfo (String canSchool, String canName, String canState,String canGender, Image canPhoto){
System.out.println(canGender );
txtCanName.setText(canName);
txtSchName.setText(canSchool);
imgViewCan.setImage(canPhoto);
// int index=cmbCanState.getValue().indexOf(canState);
// cmbCanState.getSelectionModel().select(index);
if(canGender.equalsIgnoreCase(canGender)){
rbtMale.setSelected(true);
}else{
rbtFemale.setSelected(true);
}
}
I've been trying to make a toolbar inside a window with a checkers game, what happens now is, Checkers game opening in a separate window and so is the toolbar, what am I doing wrong? How can I make this code open in one window with both functions?
#Override
public void start(Stage primaryStage) throws Exception {
Stage toolStage = new Stage();
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
Scene toolScene = new Scene(pane, 600, 400);
toolStage.setScene(toolScene);
toolStage.show();
Scene scene = new Scene(createContent());
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
You're creating a new Scene+Stage fot the toolbar, show it and then show the content in the primaryStage instead of adding both toolbar and content as parts of the same scene, e.g. by adding the content as center node of the BorderPane:
#Override
public void start(Stage primaryStage) throws Exception {
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
pane.setCenter(createContent());
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
How my webview is empty ? Why I have a blank window ?
Here my start method
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(Color.BLUE);
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
//webEngine.load("https://www.google.fr");
webEngine.loadContent("<p>qqqqqqq</p>");
primaryStage.setScene(scene);
primaryStage.show();
You don't add the WebView as a node in the scene.
root.setCenter(webView);
Ok so I am in a basic programming class, and we have to make a borderpane that has all the different panes inside of it. When i try to put just a regular pane in the center with a "hello" button inside it the button gets put on the top the borderpane. All of the other panes I use a method that makes 5 buttons and randomly inserts a word and randomly rotates it. How do i get the pane to be in the center and not the top
StackPane root = new StackPane();
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
You should add a background color for identification, e. g. via setStyle("-fx-background-color:black). This way you see how each of your panes are laid out.
Example:
public class BindIt extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.setStyle("-fx-background-color:red");
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
flow.setStyle("-fx-background-color:blue");
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
hbox.setStyle("-fx-background-color:green");
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
vbox.setStyle("-fx-background-color:yellow");
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
pane.setStyle("-fx-background-color:cyan");
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setStyle("-fx-background-color:black");
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
private Button[] createButtons() {
return new Button[] { new Button( "Button") };
}
public static void main(String[] args) {
launch(args);
}
}
It gives you this:
Now you see that the center pane is in the center and fills the center. The problem is that the button isn't in the center. The easy solution is to just use a StackPane for the center pane:
StackPane pane = new StackPane();
Another solution would be to relocate the button to the center of the center pane. But then you'd have to consider resizing of the parent.