Skip to content

Instantly share code, notes, and snippets.

@TabsPH
Created November 25, 2012 08:12
Show Gist options
  • Save TabsPH/4142805 to your computer and use it in GitHub Desktop.
Save TabsPH/4142805 to your computer and use it in GitHub Desktop.
Simple Web Browser
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Alvin Tabontabon
*/
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("WebUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<AnchorPane id="AnchorPane" prefHeight="371.0" prefWidth="431.0" xmlns:fx="http://javafx.com/fxml" fx:controller="WebUIController">
<children>
<AnchorPane id="AnchorPane" layoutX="0.0" layoutY="0.0">
<children>
<AnchorPane id="AnchorPane" layoutX="0.0" layoutY="0.0">
<children>
<GridPane id="GridPane" layoutX="0.0" layoutY="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
</children>
</AnchorPane>
<BorderPane prefHeight="371.0" prefWidth="431.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" />
</center>
<top>
<HBox prefHeight="27.0" prefWidth="431.0">
<children>
<TextField fx:id="txtURL" onAction="#goAction" prefHeight="27.0" prefWidth="382.0" HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="#goAction" prefHeight="27.0" prefWidth="47.0" text="GO" wrapText="false" HBox.hgrow="NEVER" />
</children>
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
/**
*
* @author Alvin Tabontabon
*/
public class WebUIController implements Initializable {
@FXML
TextField txtURL;
@FXML
WebView webView;
private WebEngine webEngine;
@FXML
private void goAction(ActionEvent evt) {
webEngine.load(txtURL.getText().startsWith("http://") ? txtURL.getText() : "http://" + txtURL.getText());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
webEngine = webView.getEngine();
webEngine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
txtURL.setText(newValue);
}
});
txtURL.setText("http://www.google.com");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment