-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigationBar.java
More file actions
141 lines (121 loc) · 3.67 KB
/
Copy pathNavigationBar.java
File metadata and controls
141 lines (121 loc) · 3.67 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.File;
import java.net.MalformedURLException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
public class NavigationBar extends HBox
{
// Create the FileChooser
private FileChooser fileChooser = new FileChooser();
public NavigationBar(WebView webView, String homePageUrl, boolean goToHomePage)
{
// Set Spacing
this.setSpacing(4);
// Set the Style-properties of the Navigation Bar
this.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Create the Label
Label label = new Label("History:");
// Configure the FileChooser
fileChooser.setTitle("Open Web Content");
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("HTML Files", "*.html", "*.htm"));
// Create the WebEngine
WebEngine webEngine = webView.getEngine();
// Create the TextField
TextField pageUrl = new TextField();
// Create the Buttons
Button refreshButton = new Button("Refresh");
Button goButton = new Button("Go");
Button homeButton = new Button("Home");
Button openButton = new Button("Open");
// Let the TextField grow horizontallly
HBox.setHgrow(pageUrl, Priority.ALWAYS);
// Add an ActionListener to navigate to the entered URL
pageUrl.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
webEngine.load(pageUrl.getText());
}
});
// Update the stage title when a new web page title is available
webEngine.locationProperty().addListener(new ChangeListener<String>()
{
public void changed(ObservableValue<? extends String> ov,
final String oldvalue, final String newvalue)
{
// Set the Title of the Stage
pageUrl.setText(newvalue);
}
});
// Add an ActionListener for the Refresh Button
refreshButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
webEngine.reload();
}
});
// Add an ActionListener for the Go Button
goButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
webEngine.load(pageUrl.getText());
}
});
// Add an ActionListener for the Home Button
homeButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
webEngine.load(homePageUrl);
}
});
// Add an ActionListener for the Open Button
openButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
File selectedFile = fileChooser.showOpenDialog(webView.getScene().getWindow());
if (selectedFile != null)
{
try
{
webEngine.load(selectedFile.toURI().toURL().toExternalForm());
}
catch(MalformedURLException ex)
{
ex.printStackTrace();
}
}
}
});
// Add the Children to the Navigation Bar
this.getChildren().addAll(label, pageUrl,goButton, refreshButton, homeButton, openButton);
if (goToHomePage)
{
// Load the URL
webEngine.load(homePageUrl);
}
}
}