WebEngine not loading URL on button click - javafx

I am coding a Tabbed web browser in JAVAFx. The problem i am facing is :-
When I click on Home Button (HomeB) it is not loading the DEFAULT_URL in the current tab. Here is some useful part of my code. Some body please fix it. Thanks
class Browser extends Region{
final BorderPane borderPane;
final TabPane tabPane;
private final HBox toolbarMain;
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
private String DEFAULT_URL= "http://www.google.com";
final TextField urlField = new TextField(DEFAULT_URL);
//Custom function for creation of New Tabs.
private Tab createAndSelectNewTab(final TabPane tabPane, final String title) {
Tab tab = new Tab(title);
webEngine.locationProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
urlField.setText(newValue);
}
});
final ObservableList<Tab> tabs = tabPane.getTabs();
tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));
tabs.add(tabs.size() - 1, tab);
tabPane.getSelectionModel().select(tab);
return tab;
}
//Initialization function of the program.
public Browser() {
borderPane = new BorderPane();
tabPane = new TabPane();
toolbarMain = new HBox();
Button HomeB = new Button();
HomeB.setText("HOME");
tabPane.setSide(Side.TOP);
final Tab newtab = new Tab();
newtab.setText("+");
newtab.setClosable(false); // this will not let the New Tab button(TAB) close
tabPane.getTabs().addAll(newtab); //Addition of New Tab to the tabpane.
createAndSelectNewTab(tabPane, " ");
//Function to add and display new tabs with default URL display.
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
#Override
public void changed(ObservableValue<? extends Tab> observable,
Tab oldSelectedTab, Tab newSelectedTab) {
if (newSelectedTab == newtab) {
Tab tab = new Tab();
//WebView - to display, browse web pages.
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
webEngine.load(DEFAULT_URL);
EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
webEngine.load(urlField.getText().startsWith("http://")
? urlField.getText()
: "http://" + urlField.getText());
}
};
urlField.setOnAction(goAction);
final VBox vBox = new VBox(5);
vBox.getChildren().setAll(browser);
VBox.setVgrow(browser, Priority.ALWAYS);
tab.setContent(vBox);
final ObservableList<Tab> tabs = tabPane.getTabs();
tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));
tabs.add(tabs.size() - 1, tab);
tabPane.getSelectionModel().select(tab);
}
}
});
//OnClick handling HomeB
HomeB.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
webEngine.load(DEFAULT_URL);
}
});
toolbarMain.getChildren().addAll(HomeB,urlField);
//Placement of elements in borderpane
borderPane.setCenter(tabPane);
borderPane.setTop(toolbarMain);
getChildren().add(borderPane);
}
}

When you click on the HomeB the default URL is loaded into the browser, a global WebView. That works, but you don't see the URL loaded, because you haven't added this browser to any of your tabs.
Assuming you create the first tab for the home button:
tabPane.getTabs().addAll(newtab); // tab 0, then moves to 1
// Here you create a new tab, but put it on the 0 index:
createAndSelectNewTab(tabPane, " ");
// You can add now your global browser to the first tab:
final VBox vBoxIni = new VBox(5);
vBoxIni.getChildren().setAll(browser);
VBox.setVgrow(browser, Priority.ALWAYS);
tabPane.getTabs().get(0).setContent(vBoxIni);
Other option will be using the local webView you have created for each tab, and load the default URL on the active tab:
HomeB.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
VBox vBox=(VBox)tabPane.getSelectionModel().getSelectedItem().getContent();
if(vBox!=null){
WebView webView=(WebView)vBox.getChildren().get(0);
webView.getEngine().load(DEFAULT_URL);
}
}
});
Note this won't work on the first tab, since you haven't set any content there.

Related

How to get data from Tab in Tabpane JavaFX

I want get data from Tab in Tabpane JavaFX
I have 2 Tab in Tabpane, And each Tab I have a TextArea, I want click Button will get data from 2 tab
Here's my code:
btnThem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
i++;
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("/fxml/tab.fxml"));
Parent parent = (Parent) fxmlLoader.load();
Tab tab = new Tab("Điểm " + i);
tab.setContent(parent);
tab.setClosable(true);
tabPane.getTabs().add(tab);
controllerTab = (ControllerTab) fxmlLoader.getController();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
});
Your question is ambiguous but it seems you want to get data from each textArea in tab.To do this you should get nodes (children) from each tab and by using lookup() we confirm and we parse node to textArea.I tried to figure your scene and made this example to help you :
public class example extends Application {
TextArea textArea = new TextArea();
TextArea textArea1 = new TextArea();
Button button = new Button("button");
#Override
public void start(Stage primaryStage) {
Tab tab1 = new Tab("tab1");
Tab tab2 = new Tab("tab2");
tab1.setContent(textArea);
tab2.setContent(textArea1);
TabPane pane = new TabPane();
pane.getTabs().addAll(tab1, tab2);
Node node1 = tab1.getContent();
Node node2 = tab2.getContent();
button.setOnAction((ActionEvent event) -> {
if (node1.lookup("TextArea") != null && node2.lookup("TextArea") != null) {
TextArea area1 = (TextArea) node1.lookup("TextArea");
TextArea area2 = (TextArea) node2.lookup("TextArea");
System.out.println(area1.getText() + " " + area2.getText());
}
});
VBox root = new VBox();
root.setAlignment(Pos.TOP_RIGHT);
root.getChildren().add(pane);
root.getChildren().add(button);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
And you can see the result :
Hello ,tab1 Hello ,tab2
Deleting directory C:\Users\Electron\Documents\NetBeansProjects\Buttono\dist\run341573612
jfxsa-run:
BUILD SUCCESSFUL (total time: 22 seconds)

Why my JavaFX ImageView is not showing/hiding using the Fade Transition?

I m trying to add a fade transition to my login and logout ImageViews, I tried to use the same pattern as the JFeonix Hamburger, I also used some tutorial at oracle docs but, there is no fade transition my ImageViews are hiding and showing instantly. What I'm missing ?
below is my code :
#FXML
private JFXHamburger menu;
#FXML
private ImageView login;
#FXML
private ImageView logout;
private LoginLogic logic;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
final HamburgerSlideCloseTransition slideCloseTransition = new HamburgerSlideCloseTransition(menu);
slideCloseTransition.setRate(-1);
FadeTransition t = new FadeTransition(new Duration(3000), login);
FadeTransition t1 = new FadeTransition(new Duration(3000), logout);
t.setCycleCount(1);
t1.setCycleCount(1);
t.setAutoReverse(false);
t1.setAutoReverse(false);
t.setRate(-1);
t1.setRate(-1);
menu.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent event) -> {
t1.setRate(t1.getRate() * -1);
t.setRate(t.getRate() * -1);
slideCloseTransition.setRate(slideCloseTransition.getRate() * -1);
slideCloseTransition.play();
t.play();
t1.play();
login.setVisible(!login.isVisible());
logout.setVisible(!logout.isVisible());
});
}
Thanks.
You're not changing the look of the node with the FadeTransition, since you're still using the default values for fromValue, toValue and byValue.
This means effectively you simply toggle the visibility on and off...
Usually only the opacity is modified by a FadeTransition.
Example:
#Override
public void start(Stage primaryStage) {
Button btn = new Button("fade in/out");
ImageView image = new ImageView("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png");
image.setOpacity(0);
VBox root = new VBox(10, btn, image);
root.setPadding(new Insets(10));
FadeTransition t = new FadeTransition(Duration.seconds(3), image);
t.setCycleCount(1);
t.setAutoReverse(false);
t.setRate(-1);
t.setFromValue(0);
t.setToValue(1);
btn.setOnAction(evt -> {
t.setRate(t.getRate() * -1);
t.play();
});
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

Getting webpage opened in another tab from hyperlink of current webpage in webview of javafx browser

I have developed a very simplified but fully functional JavaFX web browser for illustrating what I want to ask. Here follows the source code.
public class OpenInNewTab extends Application {
#Override
public void start(Stage primaryStage)
{
TabPane tabPane = new TabPane();
WebView webView = new WebView();
Tab tab = new Tab("Home Tab");
tab.setContent(webView);
tabPane.getTabs().add(tab);
webView.getEngine().load("https://www.google.co.in/?gws_rd=ssl#q=javafx");
BorderPane root = new BorderPane();
root.setTop(tabPane);
Scene scene = new Scene(root, 600, 339);
primaryStage.setTitle("Basic browser");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
} }
This browser loads a web page from this link. I want that when I click on any of the search results, the clicked hyperlink should open in new tab adjacent to the existing "Home tab".
After searching on the internet extensively, I reached nowhere.
Please help me with relevant code. Thanks.
First of all, you need to find a way to handle all outgoing click events on the displayed page. I've made a helper method to achieve that. It creates an event listener for each link, clicking on which will open a new tab.
private void handleUrls(Document doc) {
NodeList links = doc.getElementsByTagName("a");
for (int i = 0; i < links.getLength(); i++) {
EventTarget eventTarget = (EventTarget) links.item(i);
String link = links.item(i).toString();
eventTarget.addEventListener("click", e -> {
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab(link);
tab.setContent(webView);
tab.setClosable(true);
webView.getEngine().load(link);
tabPane.getTabs().add(tab);
e.preventDefault();
}, false);
}
}
You may want to set a better name for your tab... I've used a link as a makeshift solution, but it's long and messy.
webView.getEngine().load(link);
The alterWebView referenced above is another helper function. It's responsible for tracking the loading progress. Once the Worker does nothing, it will add listeners to all urls.
private void alterWebView(WebView webView) {
WebEngine engine = webView.getEngine();
Worker worker = engine.getLoadWorker();
worker.stateProperty().addListener((oldVal, newVal, o) -> {
if (newVal.equals(State.RUNNING)) {
handleUrls(engine.getDocument());
}
});
}
So basically, the logic behind the code above is as follows:
We track our WebEngine's State until the page is fully loaded and then we add event listeners to every url.
These listeners, when invoked, will create a new WebView, handle it accordingly, and add it to the TabPane inside a new Tab.
And here's the complete, working example based on your code:
public class JavaFXTest extends Application {
private TabPane tabPane;
#Override
public void start(Stage primaryStage) {
tabPane = new TabPane();
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab("Home Tab");
tab.setContent(webView);
tab.setClosable(true);
tabPane.getTabs().add(tab);
webView.getEngine().load("https://www.google.co.in/?gws_rd=ssl#q=javafx");
BorderPane root = new BorderPane();
root.setTop(tabPane);
Scene scene = new Scene(root, 600, 339);
primaryStage.setTitle("Basic browser");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void alterWebView(WebView webView) {
WebEngine engine = webView.getEngine();
Worker worker = engine.getLoadWorker();
worker.stateProperty().addListener((oldVal, newVal, o) -> {
if (newVal.equals(State.RUNNING)) {
handleUrls(engine.getDocument());
}
});
}
private void handleUrls(Document doc) {
NodeList links = doc.getElementsByTagName("a");
for (int i = 0; i < links.getLength(); i++) {
EventTarget eventTarget = (EventTarget) links.item(i);
String link = links.item(i).toString();
eventTarget.addEventListener("click", e -> {
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab(link);
tab.setContent(webView);
tab.setClosable(true);
webView.getEngine().load(link);
tabPane.getTabs().add(tab);
e.preventDefault();
}, false);
}
}
}

Allow clicks to go through application GUI

I have a JavaFx application that loads a transparent stage with some text on it.
I want any click on the application to be completely ignored and the background application (if any) to receive that click.
My code at this stage is as follows:
public void start(final Stage primaryStage) {
primaryStage.setAlwaysOnTop(true);
final StackPane layout = new StackPane();
final Text mainText = new Text();
layout.getChildren().add(mainText);
mainText.setText("|||||||||||||||||||||||||||");
final Scene mainScene = new Scene(layout);
mainScene.setFill(null);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(mainScene);
primaryStage.show();
layout.setMouseTransparent(true);
mainText.setMouseTransparent(true);
}
I was not able to achieve the requirement. setMouseTransparent() just prevented the text from triggering events, it still captured the mouse clicks.
Is it possible to achieve this in JavaFx ? Even if it is a per-OS solution.
A way of doing this action in Windows is through user32.dll and Java Native Access (JNA). We used GetWindowLong to get the current configuration of the window and SetWindowLong to update the bit field that is controlling the ability of the window be transparent to the mouse.
Following is a working example that demonstrates this functionality:
#Override
public void start(final Stage primaryStage) {
primaryStage.setAlwaysOnTop(true);
final StackPane layout = new StackPane();
final Text mainText = new Text();
layout.getChildren().add(mainText);
mainText.setText("|||||||||||||||||||||||||||");
final Scene mainScene = new Scene(layout);
mainScene.setFill(null);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(mainScene);
primaryStage.setTitle(sTitle);
primaryStage.show();
sUser32.EnumWindows(
(hWnd, data) -> {
final byte[] windowText = new byte[512];
sUser32.GetWindowTextA(hWnd, windowText, 512);
final String wText = Native.toString(windowText);
if (!wText.isEmpty() && wText.equals(sTitle)) {
final int initialStyle = com.sun.jna.platform.win32.User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
com.sun.jna.platform.win32.User32.INSTANCE.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, initialStyle | WinUser.WS_EX_TRANSPARENT );
return false;
}
return true;
}, null);
}

Set image on left side of dialog

I created this very simple example for JavaFX alert dialog for JavaFX8u40.
public class MainApp extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception
{
Button create = new Button("Create Alert");
create.setTooltip(new Tooltip("Create an Alert Dialog"));
create.setOnAction(e ->
{
createAlert();
});
primaryStage.setScene(new Scene(create));
primaryStage.show();
stage = primaryStage;
}
protected Alert createAlert()
{
Alert alert = new Alert(AlertType.WARNING);
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
alert.setGraphic(imageView);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
}
I'm interested how I can set the image on the left side of the dialog.
Did someone manage to change the side of the image?
If you have a look at how the header is constructed, you'll find a GridPane node to layout a Label on the left and a StackPane for the icon.
If you want to reverse the cells order by code, you can do it, but it will be overriden every time updateHeaderArea() is called.
My suggestion is using this public API:
dialogPane.setHeader(Node header);
dialogPane.setGraphic(Node graphic);
providing a header with an icon on the left and a label, and a null graphic.
Using the same approach as DialogPane, we could add another GridPane as header:
protected Alert createAlert(){
Alert alert = new Alert(AlertType.WARNING);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
DialogPane dialogPane = alert.getDialogPane();
GridPane grid = new GridPane();
ColumnConstraints graphicColumn = new ColumnConstraints();
graphicColumn.setFillWidth(false);
graphicColumn.setHgrow(Priority.NEVER);
ColumnConstraints textColumn = new ColumnConstraints();
textColumn.setFillWidth(true);
textColumn.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().setAll(graphicColumn, textColumn);
grid.setPadding(new Insets(5));
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
imageView.setFitWidth(64);
imageView.setFitHeight(64);
StackPane stackPane = new StackPane(imageView);
stackPane.setAlignment(Pos.CENTER);
grid.add(stackPane, 0, 0);
Label headerLabel = new Label("Warning");
headerLabel.setWrapText(true);
headerLabel.setAlignment(Pos.CENTER_RIGHT);
headerLabel.setMaxWidth(Double.MAX_VALUE);
headerLabel.setMaxHeight(Double.MAX_VALUE);
grid.add(headerLabel, 1, 0);
dialogPane.setHeader(grid);
dialogPane.setGraphic(null);
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
And this is what you will see:

Resources