Create a webview from a button in JavaFX - button

I have the following simple code where I have an horizontal block where there are two buttons, the remain part is empty. Now, I would like to view a Webview inside the free part after the user press the "current" button. I already did something similar but I wasn't able to manage the layout because it was a Jframe + JavaFX. I thought to re-build totally in JavaFx , for this reason I want to put the webview in a jfxPanel(I think it's the best solution, any other suggest is really appreciate). I should see the webview when I press the button , in this case current, for this reason I create an handle. So, How can I do it?
Thanks in advance
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Launcher extends Application {
public static void main(String[] args) {
launch(Launcher.class, args);
}
Button buttonCurrent;
static HBox web;
static MyBrowser browser_lau;
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox();
border.setTop(hbox);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.setTitle("Layout Sample");
stage.show();
buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
final JFXPanel jfxPanel = new JFXPanel();
//border.setStyle("-fx-background-color: #ff0000;");
}
});
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10); // Gap between nodes
hbox.setStyle("-fx-background-color: #336699;");
buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
}
}

Here is some sample code to get you started, it creates a couple of WebViews and toggles between them depending upon a radio button selection.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class CountThePeople extends Application {
private static final String CURRENT_URL =
"http://www.census.gov/popclock/";
private static final String PROJECTED_URL =
"http://www.sciencedaily.com/releases/2015/08/150810110634.htm";
#Override
public void start(final Stage stage) throws Exception {
WebView current = new WebView();
current.getEngine().load(CURRENT_URL);
WebView projected = new WebView();
projected.getEngine().load(PROJECTED_URL);
StackPane viewHolder = new StackPane();
ToggleGroup choice = new ToggleGroup();
RadioButton currentRadio = new RadioButton("Current");
currentRadio.setToggleGroup(choice);
RadioButton projectedRadio = new RadioButton("Projected");
projectedRadio.setToggleGroup(choice);
choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (projectedRadio == newValue) {
viewHolder.getChildren().setAll(projected);
} else {
viewHolder.getChildren().setAll(current);
}
});
choice.selectToggle(currentRadio);
VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder);
layout.setPadding(new Insets(10));
stage.setTitle("World Population");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}

Related

How can I scroll anchorPane in JAVAFX?

hi i write code like this in Below and how can i add Scroll in my project,**Note **The line shape added in the image is added by code and is not in scene builder space
`package schedul;
import edu.classes.drawInschedulClass;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(text);
Scene scene=new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}`
How can i add scroll!??? Please Click to show pic
when i put ScrollPane in myCode,unfortunatly dose not have good performance and myscroll dosnt cover all my Line
this Code is Below
package schedul;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
Text text=new Text();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(line);
HBox content = new HBox(root);
ScrollPane scroller = new ScrollPane(content);
scroller.setFitToWidth(true);
Scene scene= new Scene(new BorderPane(scroller, null, null, null,
null));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I expected the scroll to be created automatically
I took your sample code and removed the irrelevant bits.
All you need to do to make an AnchorPane scrollable is put it in a ScrollPane. This is demonstrated by the sample below. The same will work for any other Pane type.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class ScrollApp extends Application {
#Override
public void start(Stage stage) {
stage.setScene(
new Scene(
new ScrollPane(
new AnchorPane(
new Line(50, 50, 800, 50)
)
)
)
);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
After the application launches, by default, it will size to fit all of the content, so you won't see the scroll bars. If you make the window smaller horizontally, you will see the horizontal scrollbar, and similarly for the vertical direction.

Javafx label not updating after input from TextField in another scene

I have a label called test that is supposed to display the input value from TextField in the textInput class, from another scene. the value is being sent over to the main class but the label is not updating unless i click the button to go to the dialog box from textInput.
package javafx11;
import application.textInput;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
Stage window = new Stage();
Scene s1;
Scene s2;
Label test;
String input;
Button btn;
#Override
public void start(Stage primaryStage)throws Exception {
window=primaryStage;
VBox layout = new VBox();
s1= new Scene(layout,500,500);
test = new Label("This is where your text will appear");
btn = new Button("Click me");
window.setTitle("Dummy program");
layout.setAlignment(Pos.CENTER);
window.setScene(s1);
window.show();
btn.setOnAction(e -> {
input = textInput.textInput("title", "mnessage");
test.setText(input);
window.setScene(s1);
System.out.println(input);
});
layout.getChildren().addAll(test, btn);
}
public static void main(String[] args) {
launch(args);
}
}
the text input class:
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class textInput {
static String input;
public static TextField userField;
public static String textInput(String title, String message) {
Stage window = new Stage();
VBox layout = new VBox();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Button btn = new Button("Click me to go back");
userField = new TextField();
Scene s1 = new Scene(layout, 500, 500);
window.setScene(s1);
layout.getChildren().add(btn);
layout.getChildren().add(userField);
btn.setOnAction(e ->{
input = userField.getText();
window.close();
System.out.println(input);
});
layout.setAlignment(Pos.CENTER);
window.show();
return input;
}
}
I tried googling it but i cant really seem to understand the solution provided by others.
Its all good, figured it out after tinkering with it a little longer.
in the main class, i had to change window.show(); to window.showAndWait();

I want my JavaFX program to play a video when a button is pressed but only audio plays

I'm new to JavaFX and I need to play a video when a button is pressed, but for some reason I can't quite understand, only the sound is played. Here's my code:
package trailer;
import javafx.event.ActionEvent;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Trailer extends Application implements EventHandler<ActionEvent>{
Button button;
BackgroundImage image;
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("videonamehere.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("title content");
button = new Button("button name");
button.setOnAction(this);
Label label = new Label("label content");
label.setTextFill(Color.WHITE);
VBox layout = new VBox();
layout.setPadding(new Insets(200));
layout.setSpacing(20);
layout.getChildren().add(button);
layout.getChildren().add(label);
Scene scene = new Scene(layout, 1920, 1080);
primaryStage.setScene(scene);
scene.getStylesheets().add(Trailer.class.getResource("cssfilename.css").toExternalForm();
primaryStage.show();
}
#Override
public void handle(ActionEvent event){
if(event.getSource() == button){
player.play();
}
}
}
To clarify, the interface with the button, the label, and the CSS effects does show up, but when I press the button only the sound from the video plays.

Snapshot Image can't be used as stage icon

I'm trying to set an image from a stage snapshot as the stage icon.
The following code demonstrates it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class IconTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new StackPane(new ImageView(generateIcon(32))));
primaryStage.setScene(scene);
primaryStage.getIcons().setAll(generateIcon(32));
primaryStage.show();
}
private static Image generateIcon(int dimension) throws IOException {
Pane root = new Pane();
root.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
Scene scene = new Scene(root, dimension, dimension);
return scene.snapshot(new WritableImage(dimension, dimension));
}
}
Result:
As you can see, the stage icon is not a red square. Why is that? Loading from a file always works, generating the image using AWT works, too.
Workaround:
BufferedImage bimg = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(scene.snapshot(new WritableImage(dimension, dimension)), bimg);
SwingFXUtils.toFXImage(bimg, new WritableImage(dimension, dimension));

ControlsFx Decoration on a Label

I would like to apply ControlsFx decorations in a TableCell and as such would like to apply them to a Label.
This following does NOT apply the decoration to the Label. Should it?
import org.controlsfx.control.decoration.Decorator;
import org.controlsfx.control.decoration.GraphicDecoration;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class LabelDecoration extends Application {
private static final Image REQUIRED_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/required-indicator.png").toExternalForm()); //$NON-NLS-1$
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Test");
Node requiredDecoration = new ImageView( REQUIRED_IMAGE );
Decorator.addDecoration( label, new GraphicDecoration( requiredDecoration, Pos.TOP_LEFT ));
primaryStage.setScene( new Scene( label, 100, 100 ));
primaryStage.show();
}
public static void main(String[] args) {
launch( args );
}
}
The Decorator tries to install a DecorationPane into the scene, which does not exist yet in your case.
Wrap the line Decorator.addDecoration(...) in a Platform.runLater(...) and it will work.

Resources