Skip to content

Instantly share code, notes, and snippets.

@james-d
james-d / SerializedEdgeDemo.java
Created December 2, 2015 16:29
Demo of how to serialize JavaFX data models containing non-serializable JavaFX properties.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@james-d
james-d / EditCell.java
Created February 13, 2015 03:18
(Fairly) reusable edit cell that commits on loss of focus on the text field. Overriding the commitEdit(...) method is difficult to do without relying on knowing the default implementation, which I had to do here. The test code includes a key handler on the table that initiates editing on a key press.
import javafx.event.Event;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
@james-d
james-d / DataAccessor.java
Created May 11, 2014 01:31
JavaFX client code for the Laboratory Information Management System demo. The key here is that the controller references a client-side data accessor, which uses a Jersey client to communicate with the web-based REST service.
package edu.marshall.genomics.lims.client;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@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 {
@james-d
james-d / PlutoExplorer.java
Last active September 6, 2023 11:42
Zoomable and Pannable JavaFX ImageView Example
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
@james-d
james-d / BindingImageSizeDemo.java
Last active August 14, 2023 15:47
Demo of why it is bad to use bindings to make a responsive image container. Here adding padding to the container holding the image (which can be done with an external stylesheet) causes the image to grow indefinitely.
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
@james-d
james-d / RectangleDrawing.java
Last active June 27, 2023 19:50
Demo of event handling and consuming events. Double click to add a rectangle. Selected rectangles have a gold border. Drag a rectangle to move it; drag on empty space to select.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
@james-d
james-d / Book.fxml
Last active June 27, 2023 19:40
Demo of a MVC-like approach to managing view switching in JavaFX. Contains three views: a login screen, and two other pages. The model holds a user and a "browse state". A view manager determines the current view based on values in the model. The FXML controllers/presenters have a reference to a shared model whose properties they update.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
spacing="20"
@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 / CalendarView.java
Last active May 28, 2023 07:14
Simple demo of creating a appointment-calendar like view (similar to the default calendar view in Google Calendar), in JavaFX 8. Just manages selection of time slots in the grid.
package calendarview;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;