━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 25. WebView

1. Main.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
 
 
<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <WebView fx:id="webView" layoutX="14.0" layoutY="14.0" prefHeight="528.0" prefWidth="766.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="1.0" />
      <Button layoutX="14.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn1" text="Load" />
      <Button layoutX="117.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn2" text="Javascript" />
      <Button layoutX="254.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn3" text="Html" />
      <Button layoutX="353.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn4" text="Reload" />
   </children>
</AnchorPane>
 
cs


3. MainController.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
 
public class MainController implements Initializable{
 
    @FXML private WebView webView;
    
    private WebEngine engine;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        engine = webView.getEngine();
        
    }
    
    public void btn1(ActionEvent event){
        engine.load("https://www.google.com");
    }
    
    public void btn2(ActionEvent event){
        engine.executeScript("window.location = \"http://easyonlineconverter.com//\";");
    }
 
    public void btn3(ActionEvent event){
        engine.loadContent("<html><body><h1>Hello World</h1></body></html>");
    }
 
    public void btn4(ActionEvent event){
        engine.reload();
    }
 
 
}
 
cs



'Java > JavaFx' 카테고리의 다른 글

JavaFX 27. Event handler for a Pie Chart  (0) 2016.08.29
JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
JavaFX 22. RadioButton  (0) 2016.08.25
COMMENT