Simple FlashCard App in JavaFX

Free Starter Classes under MIT License

package application;
	
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Logger;

import file.FileUtil;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class FlashCardsApp extends Application {
	private static Logger logger = Logger.getLogger(FlashCardsApp.class.getName());
	
	private int currentTerm=0;
	
	@Override
	public void start(Stage primaryStage) {
		try {
			BorderPane root = new BorderPane();
			Scene scene = new Scene(root,400,400);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
			VBox vbox = new VBox();
			root.getChildren().add(vbox);
			
			logger.info("Working path: "+FileUtil.getWorkingPath());
			String economicTerms = FileUtil.readFileAsString("src\\main\\resources\\economicsTerms.txt");			
			
			String lines[] = economicTerms.split("\n");
			
			HashMap<String, String> termDefinitionMap = new HashMap<>();
			
			for (int i=0; i<lines.length; i++) {
				String elements[] = lines[i].split(" - ");
				termDefinitionMap.put(elements[0], elements[1]);
			}
			
			termDefinitionMap.forEach((term,def) -> {
				logger.info("\nTerm: "+term+"\n"+def+"\n");
			});
						
			Set<String> keys = termDefinitionMap.keySet();
			String keyArray[] = keys.toArray(new String[0]);
			final Label termLabel = new Label("Term: "+keyArray[currentTerm]);
			final Label definitionLabel = new Label("Definition: "+termDefinitionMap.get(keyArray[currentTerm]));
			
			
			Button button = new Button("Next");
			button.setOnAction((event) -> {
				if (currentTerm>=keyArray.length-1) {
					currentTerm=0;
				} else {
					currentTerm++;
				}
				termLabel.setText("Term: "+keyArray[currentTerm]);
				definitionLabel.setText("Definition: "+termDefinitionMap.get(keyArray[currentTerm]));				
			});
			
			button.setMinWidth(200);
			termLabel.setMinWidth(200);
			definitionLabel.setMinWidth(400);
			vbox.getChildren().addAll(button,termLabel,definitionLabel);
			vbox.autosize();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}

Example txt file to use with, can add more terms as desired

Interest - a sum paid or charged for the use of money or for borrowing money
Gross Domestic Product - gross national product excluding payments on foreign investments.
Liquidity - the ability or ease with which assets can be converted into cash

Utility class needed to read file, also MIT License

package file;

import java.awt.image.RenderedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

public class FileUtil {
	private static Logger logger = Logger.getLogger(FileUtil.class.toString());
	
	public static String getWorkingPath() {
		File file = new File("");
		String path = file.getAbsolutePath().toString();
		return path;
	}
	
	public static String readFileAsString(String fileName) throws FileNotFoundException, IOException {
		final StringWriter stringWriter = new StringWriter();
		
		File file = new File(fileName);
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		stringWriter.append(new String(bis.readAllBytes()));
		bis.close();
		
		return stringWriter.toString();
	}
	
	public static void writeFile(String fileName, String fileAsString) throws IOException {
		File file = new File(fileName);
		FileWriter fileWriter = new FileWriter(file);
		fileWriter.write(fileAsString);
		fileWriter.close();
		
		logger.info("Wrote to "+fileName+" with "+fileAsString.getBytes().length+" bytes.");
	}
	
	public static void writeBitmap(RenderedImage renderedImage, String fileName) throws IOException {
		File file = new File(fileName);		
		ImageIO.write(renderedImage, "BMP", file);
	}
}

Definitions from www.dictionary.com

Compiled using Maven and Eclipse IDE, different setup might be required

My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>FlashCardsApp</groupId>
  <artifactId>FlashCardsApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>18</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
  	<dependency>
  		<groupId>FileUtilityLibrary</groupId>
  		<artifactId>FileUtilityLibrary</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>EncouragementGenerator</groupId>
      <artifactId>EncouragementGenerator</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
	<dependency>
	    <groupId>org.openjfx</groupId>
	    <artifactId>javafx-controls</artifactId>
	    <version>19</version>
	</dependency>
  </dependencies>
</project>

My module-info.java

module FlashCardsApp {
	requires javafx.controls;
	requires java.logging;
	requires FileUtilityLibrary;
	
	opens application to javafx.graphics, javafx.fxml;
}

I am storing the FileUtil in another library called FileUtilityLibrary I am installing using Maven.

Liabilities factor into Liquidity factor into Throughput

Gross Domestic Exercise Investments

Walking and Exercise is Valuable, clear Sky is Valuable, not a given

Published by techinfodebug

Flex and Java Developer, Christian, Art, Music, Video, and Vlogging

Leave a comment