Skip to content

Instantly share code, notes, and snippets.

@james-d
james-d / AnimationTimerTest.java
Last active May 28, 2023 08:02
Experiment to see how smooth animation is in JavaFX using an AnimationTimer to update the view on each pulse. This simulates 300 balls of various sizes bouncing (with perfect elasticity) off each other and the edges of the Pane in which they're contained. It could easily be extended to a simulation where the pressure on each wall was measured by…
import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static javafx.scene.paint.Color.BLACK;
import static javafx.scene.paint.Color.BLUE;
import static javafx.scene.paint.Color.BROWN;
import static javafx.scene.paint.Color.GREEN;
import static javafx.scene.paint.Color.PINK;
import static javafx.scene.paint.Color.RED;
@james-d
james-d / ExpandableTableRow.java
Last active September 12, 2018 12:10
Attempt to create a JavaFX TableView with expandable rows, as in the OTN discussion at https://community.oracle.com/thread/2619951. This doesn't work...
import java.util.Arrays;
import java.util.concurrent.Callable;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableValue;
@james-d
james-d / AnimatedPropertiesTest.java
Created January 17, 2014 15:08
Some simple examples of animations with a Button
import java.util.concurrent.Callable;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
@james-d
james-d / TableWithSelectBinding.java
Last active December 6, 2022 08:15
The Bindings.select*(...) family of methods use try-catch to implement null checking of "intermediate" properties. This is highly inefficient, as demonstrated by this example. Sort by the "Zip" column to see the problem.
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
@james-d
james-d / Clock.java
Last active August 29, 2015 13:57
Java 8 Clock with convenient string bindings for "xxx ago" labels, with an example.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javafx.application.Application;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleStringProperty;
import static java.time.Month.*;
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
@james-d
james-d / LinkedComboBoxTable.java
Created March 26, 2014 22:37
TableView, where the contents of a combo box used to edit values in one column (county) depend on the value selected in another column (state). To test, type an arbitrary string in the text box at the bottom and press enter. Double-click the cells to bring up the combo boxes.This is pretty tricky: if anyone can suggest an easier method for this,…
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
@james-d
james-d / ExpandableTextArea.java
Created March 30, 2014 13:39
Experiment with creating a reusable ExpandableTextArea that grows/shrinks according to the text inside it. This is a bit of a hack: uses a lookup and an AnimationTimer to execute the lookup when the css styles have been applied.
package expandabletextarea;
import java.util.concurrent.Callable;
import javafx.animation.AnimationTimer;
import javafx.beans.binding.Bindings;
import javafx.scene.Node;
import javafx.scene.control.TextArea;
public class ExpandableTextArea extends TextArea {