Tutorial by Examples: d

Buttons fire action events when they are activated (e.g. clicked, a keybinding for the button is pressed, ...). button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); ...
Buttons can have a graphic. graphic can be any JavaFX node, like a ProgressBar button.setGraphic(new ProgressBar(-1)); An ImageView button.setGraphic(new ImageView("images/icon.png")); Or even another button button.setGraphic(new Button("Nested button"));
Don't just use Optional.get() since that may throw NoSuchElementException. The Optional.orElse(T) and Optional.orElseGet(Supplier<? extends T>) methods provide a way to supply a default value in case the Optional is empty. String value = "something"; return Optional.ofNullable(v...
When a Java class overrides the equals method, it should override the hashCode method as well. As defined in the method's contract: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, ...
By default, WebView does not implement JavaScript alert dialogs, ie. alert() will do nothing. In order to make you need to firstly enable JavaScript (obviously..), and then set a WebChromeClient to handle requests for alert dialogs from the page: webView.setWebChromeClient(new WebChromeClient() { ...
Since Java 6, the recommended way to access an SQL-based database in Java is via the JDBC(Java DataBase Connectivity) API. This API comes in two packagages: java.sql and javax.sql. JDBC defines database interactions in terms of Connections and Drivers. A Driver interacts with the database, and pr...
Once we've gotten the Connection, we will mostly use it to create Statement objects. Statements represent a single SQL transaction; they are used to execute a query, and retrieve the results (if any). Let's look at some examples: public void useConnection() throws SQLException{ Connection ...
Enum can be considered to be syntax sugar for a sealed class that is instantiated only a number of times known at compile-time to define a set of constants. A simple enum to list the different seasons would be declared as follows: public enum Season { WINTER, SPRING, SUMMER, FA...
An enum can contain a method, just like any class. To see how this works, we'll declare an enum like this: public enum Direction { NORTH, SOUTH, EAST, WEST; } Let's have a method that returns the enum in the opposite direction: public enum Direction { NORTH, SOUTH, EAST, WEST; ...
String getText(String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); //add headers to the connection, or check the status if desired.. // handle error response code it occurs int responseCode = conn.getResponseCod...
One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this: A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things: An XML file to define the av...
Scanner scanner = new Scanner(System.in); //Scanner obj to read System input String inputTaken = new String(); while (true) { String input = scanner.nextLine(); // reading one line of input if (input.matches("\\s+")) // if it matches spaces/tabs, stop reading b...
Scanner scanner = null; try { scanner = new Scanner(new File("Names.txt")); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (Exception e) { System.err.println("Exception occurred!"); } finally { if (scanner != nul...
Sometimes the same functionality has to be written for different kinds of inputs. At that time, one can use the same method name with a different set of parameters. Each different set of parameters is known as a method signature. As seen per the example, a single method can have multiple signatures....
Needed imports: import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; This code will create a clip and play it continuously once started: Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new URL(filename))); clip.start(); clip.loop(Clip.LOOP_CO...
Pagination p = new Pagination(10); Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> { int pos = (p.getCurrentPageIndex()+1) % p.getPageCount(); p.setCurrentPageIndex(pos); })); fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE); fiveSecondsWon...
wait() and notify() work in tandem – when one thread calls wait() on an object, that thread will block until another thread calls notify() or notifyAll() on that same object. (See Also: wait()/notify() ) package com.example.examples.object; import java.util.concurrent.atomic.AtomicBoolean; p...
MIDI files can be played by using several classes from the javax.sound.midi package. A Sequencer performs playback of the MIDI file, and many of its methods can be used to set playback controls such as loop count, tempo, track muting, and others. General playback of MIDI data can be done in this wa...
In your build.gradle file of main module(app), define your minimum and target version number. android { //the version of sdk source used to compile your project compileSdkVersion 23 defaultConfig { //the minimum sdk version required by device to run your app minSd...
Colors are usually stored in a resource file named colors.xml in the /res/values/ folder. They are defined by <color> elements: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <c...

Page 11 of 691