Skip to content

Instantly share code, notes, and snippets.

@bqcuong
Last active October 5, 2020 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bqcuong/c57e5cfd20681bcbd22e0815987c77e9 to your computer and use it in GitHub Desktop.
Save bqcuong/c57e5cfd20681bcbd22e0815987c77e9 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Dictionary extends Application {
private static final String DATA_FILE_PATH = "E_V.txt";
private static final String FXML_FILE_PATH = "Dictionary_Main.fxml";
private static final String SPLITTING_CHARACTERS = "<html>";
private Map<String, Word> data = new HashMap<>();
@FXML
private ListView<String> listView;
@FXML
private WebView definitionView;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
FileInputStream fis = new FileInputStream(FXML_FILE_PATH);
AnchorPane root = fxmlLoader.load(fis);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Dictionary Demonstration");
primaryStage.show();
// init components
initComponents(scene);
// read word list from E_V.txt
readData();
// load word list to the ListView
loadWordList();
}
public void initComponents(Scene scene) {
this.definitionView = (WebView) scene.lookup("#definitionView");
this.listView = (ListView<String>) scene.lookup("#listView");
Dictionary context = this;
this.listView.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
Word selectedWord = data.get(newValue.trim());
String definition = selectedWord.getDef();
context.definitionView.getEngine().loadContent(definition, "text/html");
}
);
}
public void loadWordList() {
this.listView.getItems().addAll(data.keySet());
}
public void readData() throws IOException {
FileReader fis = new FileReader(DATA_FILE_PATH);
BufferedReader br = new BufferedReader(fis);
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(SPLITTING_CHARACTERS);
String word = parts[0];
String definition = SPLITTING_CHARACTERS + parts[1];
Word wordObj = new Word(word, definition);
data.put(word, wordObj);
}
}
}
class Word {
private String word;
private String def;
public Word(String word, String def) {
this.word = word;
this.def = def;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getDef() {
return def;
}
public void setDef(String def) {
this.def = def;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Dictionary">
<children>
<VBox layoutX="-2.0" layoutY="-2.0" prefHeight="406.0" prefWidth="600.0" stylesheets="@main.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextField>
<VBox.margin>
<Insets left="10.0" right="10.0" top="20.0" />
</VBox.margin>
</TextField>
<HBox prefHeight="356.0" prefWidth="600.0">
<children>
<ListView fx:id="listView" prefHeight="200.0" prefWidth="200.0" />
<WebView fx:id="definitionView" prefHeight="353.0" prefWidth="404.0" />
</children>
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment