
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement JShell using JavaFX in Java 9?n
JShell is an interactive tool used to implement sample expressions. We can implement JShell programmatically using JavaFX application then we need to import a few packages in the java program listed below
import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet;
In the below example, implemented a sample Java FX application. We will enter different values in the text field and press the "eval" button. It will display values with corresponding data types in a list.
Example
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import java.util.List; import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet; public class JShellFXTest extends Application { @Override public void start(Stage primaryStage) throws Exception { JShell shell = JShell.builder().build(); TextField textField = new TextField(); Button evalButton = new Button("eval"); ListView<String> listView = new ListView<>(); evalButton.setOnAction(e -> { List<SnippetEvent> events = shell.eval(textField.getText()); events.stream().map(event -> convert(event)).filter(s -> s != null).forEach(s -> listView.getItems().add(s)); }); BorderPane pane = new BorderPane(); pane.setTop(new HBox(textField, evalButton)); pane.setCenter(listView); Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.show(); } public static String convert(SnippetEvent e) { if(e.snippet() instanceof VarSnippet) { return ((VarSnippet) e.snippet()).typeName() + " " + ((VarSnippet) e.snippet()).name() + " " + e.value(); } return null; } public static void main(String[] args) { launch(); } }
Output
- Related Articles
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- How to debug JShell in Java 9?
- Using Variables in JShell of Java 9
- How to implement JavaFX event handling using lambda in Java? \n

Advertisements