100% found this document useful (2 votes)
169 views47 pages

Instant Download Frontend Development With JavaFX and Kotlin: Build State-of-the-Art Kotlin GUI Applications Peter Späth PDF All Chapter

Frontend

Uploaded by

toufekhagrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
169 views47 pages

Instant Download Frontend Development With JavaFX and Kotlin: Build State-of-the-Art Kotlin GUI Applications Peter Späth PDF All Chapter

Frontend

Uploaded by

toufekhagrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Get ebook downloads in full at ebookmeta.

com

Frontend Development with JavaFX and Kotlin: Build


State-of-the-Art Kotlin GUI Applications Peter
Späth

https://ebookmeta.com/product/frontend-development-with-
javafx-and-kotlin-build-state-of-the-art-kotlin-gui-
applications-peter-spath/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://ebookmeta.com


Frontend
Development with
JavaFX and Kotlin
Build State-of-the-Art Kotlin
GUI Applications

Peter Späth
Frontend Development with JavaFX and Kotlin
Peter Späth

Frontend Development
with JavaFX and Kotlin
Build State-of-the-Art Kotlin GUI Applications
Peter Späth
Leipzig, Sachsen, Germany

ISBN-13 (pbk): 978-1-4842-9716-2 ISBN-13 (electronic): 978-1-4842-9717-9


https://doi.org/10.1007/978-1-4842-9717-9

Copyright © 2023 by Peter Späth


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned,
specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in
any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by
similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of
a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the
trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such,
is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors
nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher
makes no warranty, express or implied, with respect to the material contained herein.

Managing Director, Apress Media LLC: Welmoed Spahr


Acquisitions Editor: Celestin Suresh John
Development Editor: Laura Berendson
Coordinating Editor: Mark Powers

Cover designed by eStudioCalamar


Cover image by Jerryyaar Designer on Pixabay (www.pixabay.com)

Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-
800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media,
LLC is a California LLC, and the sole member (owner) is Springer Science + Business Media Finance Inc. (SSBM Finance
Inc). SSBM Finance Inc. is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights,
please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also
available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/
bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://
github.com/Apress). For more detailed information, please visit https://www.apress.com/gp/services/source-code.

Paper in this product is recyclable


Contents

1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Gradle for JavaFX and Kotlin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
A HelloWorld Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Setting Up for Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Setting Up for IntelliJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Kotlin and Java Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
A Note About Kotlin Utilities for JavaFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
A Note About FXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
A Note About Downloading JavaFX Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Build Setup for This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Why you Should use Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
One-Way and Two-Way Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Custom Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
About Observable Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3 Stages and Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
About Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Using Stages and the Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Dialog-Like Stages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
The JavaFX Application Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
About Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Position and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Mnemonic and Accelerators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Node Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Snapshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Fill and Other Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Mouse Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

v
vi Contents

Mouse Drag Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52


Gestures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
StackPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
VBox and HBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
FlowPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
GridPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
TilePane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
BorderPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
AnchorPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Styling Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Adding Stylesheets to the Whole Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Adding Stylesheets to Individual Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
JavaFX CSS Selectors for Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
JavaFX CSS Properties for Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5 Visual Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Node Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Image Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Text Fields and Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Action Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Button Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Checkboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Miscellaneous Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Control Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Scroll Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Accordions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Tab Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Split Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Styling Visual Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6 Lists and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Lists with ListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Tables with TableView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Trees with TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Contents vii

7 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
What Events Are and Event Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Event Handlers and Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Drag and Drop Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8 Effects and Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
About Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Animating Your Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Timeline Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
9 Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The JavaFX Concurrency Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
About Kotlin Coroutines for JavaFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
About the Author

Peter Späth graduated in 2002 as a physicist and soon afterward became an IT consultant, mainly for
Java-related projects. In 2016, he decided to concentrate on writing books on various aspects, but with
a main focus on software development. With two books about graphics and sound processing, three
books on Android app development, and a couple of books about Java, Jakarta EE, and Kotlin, Peter
continues his effort in writing software development-related literature.

ix
About the Technical Reviewer

Massimo Nardone has more than 25 years of experience in security,


web and mobile development, cloud, and IT architecture. His true
IT passions are security and Android. He has been programming
and teaching how to program with Android, Perl, PHP, Java, VB,
Python, C/C++, and MySQL for more than 20 years. He holds a
Master of Science degree in Computing Science from the University
of Salerno, Italy. He has worked as a CISO, CSO, security executive,
IoT executive, project manager, software engineer, research engineer,
chief security architect, PCI/SCADA auditor, and senior lead IT
security/cloud/SCADA architect for many years. His technical skills
include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl,
web and mobile development, MongoDB, D3, Joomla, Couchbase,
C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch,
and more. He worked as visiting lecturer and supervisor for exercises
at the Networking Laboratory of the Helsinki University of Technol-
ogy (Aalto University). He holds four international patents (PKI, SIP,
SAML, and Proxy areas). He is currently working for Cognizant as
head of cyber security and CISO to help both internally and externally
with clients in areas of information and cyber security, like strategy,
planning, processes, policies, procedures, governance, awareness,
and so forth. In June 2017 he became a permanent member of the
ISACA Finland Chapter Board.
Massimo has reviewed more than 45 IT books for different
publishing companies and is the coauthor of Pro Spring Security:
Securing Spring Framework 5 and Boot 2-based Java Applications
(Apress, 2019), Beginning EJB in Java EE 8 (Apress, 2018), Pro
JPA 2 in Java EE 8 (Apress, 2018), and Pro Android Games (Apress,
2015).

xi
Introduction

Building elegant and highly responsible, responsive, and stable Java client applications (fat clients)
is a highly acceptable approach if security considerations or network availability speaks against
web applications, or maintaining servers and server applications lies out of scope for your project.
Additionally, using Kotlin as a programming language boosts code expressiveness and maintainability,
allowing for a development yielding a clean code approach.
The book introduces JavaFX as a frontend technology and from the very beginning focuses
on using Kotlin instead of Java for coding the program artifacts. Many listings and code snippets
accompany the text, readily allowing for a hands-on learning style.

The Book’s Targeted Audience

The book is for low- to mid-level Java or Kotlin developers with or without JavaFX experience,
wishing to learn how to build JavaFX applications with Kotlin.
The readers will in the end be able to use Kotlin as a language for building basic to moderately
advanced and elaborated apps targeting JavaFX.
Any experience in using JavaFX and frontend coding is not a requirement for reading the book.
Being a Kotlin expert is not necessary either, but having read introductory-level books or studied
online resources is surely helpful. The online documentation of Kotlin and JavaFX also provides
valuable resources you can use as a reference while reading this book.

Source Code

All source code shown or referred to in this book can be found at github.com/apress/frontend-
development-javafx-kotlin.

How to Read This Book

This book should be read sequentially to get the most benefit from it. Of course, you can skip one
or the other chapter if you already gained knowledge elsewhere. Taking its introductory nature, the
book is not meant to present a reference fully covering each and every aspect of Kotlin frontend
programming or JavaFX, so also consulting the online documentation at
https://openjfx.io/
https://openjfx.io/javadoc/19/
https://kotlinlang.org/docs/home.html

xiii
xiv Introduction

while you are reading the book certainly is not a bad idea.
The book is split up into nine chapters. Chapter 1 gives a general introduction and presents hello
world-style programs for Gradle, Eclipse, and IntelliJ.
Chapter 2 talks about using properties as data holders and addresses one- and two-way binding
techniques for connecting controls and data in your program.
Chapter 3 introduces stages and scenes, which serve as primordial containers for visual artifacts.
Chapter 4 talks about containers and ways to lay out and style your scenes.
Chapter 5 handles nodes and controls including styling. These aspects usually constitute the biggest
part of your project work speaking of time budget.
Chapter 6 presents lists and tables, which are particularly important for enterprise-level projects.
Chapter 7 is for summarizing and deepening our knowledge about event handling in JavaFX. This
also includes drag and drop procedures.
Chapter 8 introduces effects and animation, improving user experience and giving your programs
some eye candies.
As a prospect, Chapter 9 briefly introduces concurrency techniques, giving you a starting point for
handling background processing needs.
Getting Started
1

In this chapter, we give a brief introduction to using JavaFX and Kotlin together, and we create “Hello
World”–style projects for the command line, for Eclipse, and for IntelliJ IDEA.

Introduction

JavaFX is the dedicated fat client (desktop application) GUI toolkit for current Java releases. It is
the replacement and successor of the venerable Java Swing technology. This switch happened around
2010, and since then JavaFX has been constantly improved and extended. With JREs up to version
JDK 9, JavaFX was part of the Java distribution—with JDK 11 and later, it has to be installed
separately.
The following features describe JavaFX:

. Built-in controls: Labels, editable text fields, buttons, combo boxes, checkboxes, radio buttons,
menu bars, scrollbars, accordion, tabs, canvas (for drawing shapes and figures), color picker, pag-
ination, 3D graphics (games, science, product presentation), WebView (presenting and interacting
with web contents), dialogs, sliders, spinners, progress bars
. Lists, tables, trees
. Built-in layouts: AnchorPane (anchoring nodes to one of the edges or to the center point),
BorderPane (placing nodes at bottom, top, right, left, center), FlowPane (placing nodes consec-
utively and wrapping at the boundaries), TilePane (same as FlowPane, but with all cells the same
size), GridPane (placing nodes in a grid with cell sizes dynamically calculated and on demand
spanning several rows and columns), VBox (placing nodes in columns), HBox (placing nodes in
rows), StackPane (placing nodes in an overlay fashion)
. Animation (fade, fill, stroke, translate, rotate, scale, . . . ), effects (glow, blend, bloom, blur,
reflection, sepia, shadow, lighting)
. Nodes stylable via CSS
. Some built-in chart widgets
. Flexible and concise data binding via observable properties
. Descriptive layouting via FXML
. Module support (for JDK 9+)

© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023 1
P. Späth, Frontend Development with JavaFX and Kotlin,
https://doi.org/10.1007/978-1-4842-9717-9_1
2 1 Getting Started

. Graphics transformations and coordinate systems


. Media APIs
. Java Swing interoperability
. Comes as a set of JAR modules and native libraries
. An external Scene Builder for graphically creating scenes
. Printing API

In this book, we describe a subset of these features, giving you a starting point for your own
projects.
Using Kotlin as a programming language instead of Java gives a boost to your coding experience.
Just to give you an example, consider a button with a click handler. In Java, you’d write
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

(255 characters) The very same code written in Kotlin reads


val btn = Button().apply {
text = "Say 'Hello World'"
setOnAction { _ ->
println("Hello World!")
}
}

With 142 characters, this is more than 40% shorter than the Java variant! And besides being shorter,
it is also more expressive and by that easier to understand and easier to maintain.
Using some sufficiently nonobtrusive utility functions, this can even be further reduced to 81
characters in size:
val btn = Button("Say 'Hello World'") {
println("Hello World!")
}

This works by Kotlin’s ability to dynamically add additional constructors to classes.

Gradle for JavaFX and Kotlin

As a build tool, we use Gradle from https://gradle.org/. It is highly flexible, works on any operating
system that provides a Java installation, and by means of plugins or preinstalled components can be
operated from many IDEs.
I first describe the CLI mode for running Gradle builds. This is how you would use it in a server
environment, but it also serves as a good starting point if you want to learn how to use Gradle inside
an IDE workflow.
If not already present, get and install a version 17 JDK. Throughout the book, we will be using
OpenJDK 17, but if chances are good you can also take Oracle’s supported JDK 17 or a higher version
from either Oracle or https://openjdk.org/ without any problems possibly coming up.
Gradle for JavaFX and Kotlin 3

Note Using Oracle’s JDK 17 or higher requires buying a license if you plan to use it for a longer
term; see www.oracle.com/java/.

As a next step, fetch Gradle from https://gradle.org. In this book, we use version 7.6 from https://
gradle.org/next-steps/?version=7.6&format=bin. In order to announce Java to Gradle, either make
sure java and javac (with .bat extension on Windows) are in your PATH, or you have the
environment variable JAVA_HOME point to your JDK installation folder (recommended). To simplify
using Gradle, you can also put GRADLE-INST-DIR/bin (with GRADLE-INST-DIR pointing to your
Gradle folder), or GRADLE-INST-DIR\bin for Windows, on the path.

Note In Linux, environment variables like PATH or JAVA_HOME get set via
export PATH=/bin:/usr/bin:/path/to/my/gradle/bin.
In Windows, you must use the system settings dialog.

In order to check your Gradle installation, in a terminal enter


gradle -version

or, if Gradle is not in the path:


/path/to/gradle -version (Linux)
C:\path\to\gradle.bat -version (Windows)

The output of the command should be similar to


---------------------------------------------------------
Gradle 7.6
---------------------------------------------------------

Build time: 2022-11-25 13:35:10 UTC


Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin: 1.7.10
Groovy: 3.0.13
Ant: Apache Ant(TM) version 1.10.11 compiled on
July 10 2021
JVM: 17.0.1 (Oracle Corporation 17.0.1+12-39)
OS: Linux 5.15.0-56-generic amd64

Important is the “JVM:” line. The Kotlin version shown does not mean you would not be able to build
applications running under a different Kotlin version—it just tells it is using Kotlin 1.7.10 for its own
purposes.
Next, create a project folder anywhere on your system. For our example project, we call it
HelloWorld. Change into that folder:
cd /path/to/HelloWorld (Linux)
chdir C:\path\to\HelloWorld (Windows)

In order to initialize the Gradle project, enter (one line)


gradle init --dsl groovy --incubating
--insecure-protocol ALLOW --package book.kotlinfx
--project-name kotlinfx --test-framework kotlintest
--type kotlin-application

You can also enter just gradle init, but then you will subsequently be asked for project
coordinates inside the terminal.
4 1 Getting Started

The “init” task creates a simple scaffold project which consists of a main project described by
file settings.gradle and a subproject called “app” in the accordingly named subfolder. The
application can be run by just entering either of
gradle app:run
gradle run

The second variant is possible, because there is just one subproject. By the way, you can list all
possible tasks via gradle tasks or gradle tasks --all, and entering gradle help shows
more info.
Did you notice that two executable files gradlew and gradlew.bat and a folder gradle were
created? This is the Gradle Wrapper, and it is a Gradle installation on its own, and you can henceforth
use it to build the project. Just use gradlew from the wrapper instead of gradle from the Gradle
distribution. You can even delete the main Gradle installation folder at this time, if you like.
It is now time to add JavaFX to the project. In Gradle, the build.gradle file is the main
configuration file for the build process. You can find it inside the app subproject inside the app folder.
Open the file inside a text editor, and inside the plugins { . . . } section, add
plugins {
...
id 'org.openjfx.javafxplugin' version '0.0.13'
}

This plugin adds almost all that is necessary to add JavaFX to a Java or Kotlin project. Kotlin
capabilities were already added during gradle init. We however still need to make sure that
Kotlin compiles for JDK 17 and that JavaFX uses version 19 and allows for using the modules
“javafx.controls” and “javafx.graphics”. For that aim, add at the end of build.gradle
compileKotlin {
kotlinOptions {
suppressWarnings = true
jvmTarget = "17"
}
}
javafx {
version = "19"
modules("javafx.controls", "javafx.graphics")
}

Note JavaFX is separated into different modules. The modules “javafx.base”, “javafx.controls”, and
“javafx.graphics” are essential to almost any JavaFX application. Because both the controls and the
graphics module require the base module, the latter gets implicitly included in any build and can be
omitted from the modules list. For more details, see https://openjfx.io/javadoc/19/

In the next section, we code our little “Hello World” JavaFX with Kotlin application.

A HelloWorld Project

The scaffold project built via gradle init just prints “Hello World!” on the console if run. As a
starter JavaFX project, we instead want to show a little window with a button on it reacting to press
events. To do so, replace the contents of
app/src/main/kotlin/book/kotlinfx/App.kt
Other documents randomly have
different content
The Project Gutenberg eBook of Harry Muir
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: Harry Muir


A story of Scottish life, vol. 1 (of 3)

Author: Mrs. Oliphant

Release date: January 14, 2024 [eBook #72715]

Language: English

Original publication: London: Hurst and Blackett, 1853

Credits: Susan Skinner, Eleni Christofaki and the Online


Distributed Proofreading Team at
https://www.pgdp.net (This file was produced from
images generously made available by The Internet
Archive)

*** START OF THE PROJECT GUTENBERG EBOOK HARRY MUIR ***


Transcriber’s note

Variable spelling and hyphenation have been retained. Minor punctuation


inconsistencies have been silently repaired. A list of the changes made can be
found at the end of the book.
HARRY MUIR, Vol. I
HARRY MUIR.

A STORY OF SCOTTISH LIFE.

BY THE AUTHOR OF
“PASSAGES IN THE LIFE OF MRS. MARGARET MAITLAND,”
“MERKLAND,” “ADAM GRAEME,” &C.

“God pardon thee! yet let me wonder, Harry,


At thy affections....
The hope and expectation of thy time
Is ruined; and the soul of every man,
Prophetically, does forethink thy fall.”

KING HENRY IV.

IN THREE VOLUMES.
VOL. I.

LONDON:
HURST AND BLACKETT, PUBLISHERS,
SUCCESSORS TO HENRY COLBURN,
13, GREAT MARLBOROUGH STREET.

1853.
LONDON:
Printed by Schulze and Co., 13, Poland Street.
HARRY MUIR.
CHAPTER I.
“Housekeeping youth have ever homely wits.”

two gentlemen of verona.

“And this is the pillar that Rob Roy hid behind, the Sabbath day that
he warned the young English gentleman in the kirk. It’s the very
place itsel. Here was the pulpit—and the seats were a’ here, and this
is the pillar that hid Rob Roy.”
A party of young men were in the crypt of Glasgow cathedral—the
little sleek, humble-looking man, who very unobtrusively acted as
Cicerone, was pointing out to them the notability, with these words.
One of the visitors turned away with a grave smile, and leaving his
companions, began to wander slowly down one of the long black
aisles. The dim withdrawing vistas—the pillars with their floral
chaplets—the singular grace and majesty of those dark and
ponderous arches—impressed him with very different associations.
The young man’s smile, slightly scornful at first, melted as he
reached the lower end, and looking up through this grand avenue,
saw the little knot of dim figures in the distance. He was glad to
escape from their laughter, and unsuitable merriment. These noble
old cloisters were too grave and solemn, to have their stillness so
invaded.
But he was not suffered long to remain uninterrupted in his
contemplative mood. “What ails Cuthbert?” said one of the younger
of the party, a lad in the transition state between boy and man. “See
to him down yonder at the very end, like a craw in the mist—I say,
Cuthbert!”
As the piping shrill voice called out his name at its highest pitch,
the young man began slowly to advance again. The lad came
forward to meet him. “What are you smiling at—what did you go
away for?”
“I was smiling at myself, John,” answered the accused.
John was curious. “What for?”
“For thinking there were things more interesting here, than the
pillar that hid Rob Roy. Come along—never mind. Where are they all
bound for, now?”
They were bound for a very dissimilar place—no other than the
crowded Broomielaw, where John’s brothers were bent upon
showing their Edinburgh cousin, Cuthbert Charteris, and an English
stranger who accompanied them, one or two fine ships belonging to
“the house” then in port. These young men were the sons of a
prosperous merchant, all of them already in harness in the office,
and beginning to make private ventures on their own behalf. There
were three of them—Richard, Alick, and John Buchanan; the two
elder had reached the full dignity of young manhood, and rejoiced in
mighty whiskers, which John, poor fellow, could only covet intensely,
and cultivate with all his might; but even John had begun to have
the shrewd man of business engrafted on the boy, and was
sometimes precociously calculating, and commercial—sometimes
disagreeably swaggering and loud—though not unfrequently simple,
foolish, and generous, as better became his years.
“I say, Cuthbert,” said the communicative John, as he swung his
arm through his grave cousin’s, and followed his gay brothers on the
way to the river, “did you ever see Harry Muir? Dick says he’s going
to make him come and dine with us to-night.”
“And who is Harry Muir?” asked Charteris.
“Oh, he’s nobody—only a clerk in the office you know—but you
never saw such a clever chap. He can sing anything you like. He’s a
grand singer. And when Harry’s in a good humour, you should just
hear him with the fellows in the office. My father looks out of his
own room sometimes to see what’s the row, and there’s Gilchrist
sucking his pen, and Macauley and Alick close down over their
books, writing for a race, and Muir quite cool, and looking as
innocent as can be. You should just see them, and see how puzzled
my father is, when he finds that there’s no row at all!”
“And in such emergencies, how do you behave yourself, Johnnie?”
“Johnnie! I wish you’d just mind that I’m not a boy now.”
“Jack, then! Will that please you, young man,” said Charteris,
smiling.
“Me? I behave the best way I can,” said the mollified John. “The
best plan is, to set to working, and never let on that you hear the
door open; but we like to get him among a lot of us when there’s
nobody in the way; and you’ll just see to-night, Cuthbert, what a
grand fellow he is for fun.”
Cuthbert did not look very much delighted. “And when is this
famous dinner to be?” he asked. “Is Dick to entertain us at home?”
Master John burst into a great laugh. “Man, Cuthbert, what a
simple fellow you are! You don’t think my mother would ask Harry
Muir to dine.”
“And why not, my boy?” asked the Edinburgh advocate.
“Why not! Man, is that the way you do in the east country? He’s
only a clerk, and everybody knows you Edinburgh folk are as proud
as proud can be. Would you ask your clerk to dine with you?”
“I don’t possess such an appendage, Sir John,” said the briefless
barrister, “except it be a little scrubby boy like what you were the
last time I was west here—and he certainly would need some
brushing up. So he’s not a gentleman, this wit of yours? He would
not be presentable in the drawing-room?”
“Hum! I don’t know,” said honest John, hesitating. “He looks quite
as well as Dick or Alick, or that Liverpool man there.” The lad drew
himself up and arranged his neckcloth complacently. “There’s
handsomer men, to be sure; but I think Muir’s better looking than
any of you, Cuthbert.”
Charteris laughed: “Is he not well-bred, then?”
“Oh yes, he can behave himself well enough. He’s got a way of his
own, you know; but then he’s a clerk.”
“And so are you, Jack, my man,” said Charteris.
“Oh yes, but there’s a difference. He’s got no money—and more
than that,” said the juvenile merchant, “he’s got no enterprise,
Cuthbert. There’s Alick, he had a share in a plan, sending out a lot of
things to San Francisco on a venture, just when the news came
about the gold, you know, and he cleared a hundred pounds; that’s
the way to do. But then, that fellow Muir, he never tries a thing; and
worse than that, he went away and married somebody last year, and
he had three sisters before, and them all living with him. Just think
of that. Four women all dragging a young man down when he might
be rising in the world. Isn’t it awful?”
“A very serious burden,” said Charteris, smiling, “but what is his
salary, John?”
“His salary’s sixty pounds; my father gives very good salaries. He’s
just a clerk, you know. The cashier has two hundred.”
“Sixty pounds! and five people live on sixty pounds!” said the
lawyer.
“And they’ve got a baby,” said John, solemnly.
It was the climax; there was no more said.
The respectable firm of George Buchanan and Sons had its office in
a dingy business street near the Exchange. The early darkness of
the February night had almost blotted out the high sombre houses
opposite, except for the gleaming gas-light streaming from office
windows in irregular patches from garret to basement. It was not a
very busy time, and at five o’clock the clerks were preparing to leave
the office.
“I say, Muir,” cried Richard Buchanan, bursting in hastily, “come and
dine with us.”
Charteris was behind. The famous Harry Muir was certainly
handsome—very much better looking than any other of the party,
and had a fine, sparkling, joyous, intelligent face—but the lines of it
had everything in them but firmness.
“Not to-night,” said the clerk, “you must not ask me to-night.”
“Why not to-night?” said the young master. “Come along now,
Harry. Do be a good fellow. Why it’s just to-night of all nights that
we want you. There’s my cousin Charteris, and there’s an
Englishman; and we’re all as flat as the Clyde. Come along, Muir,
don’t disoblige us.”
“I am very sorry,” said Muir, “but I can’t stay in town to-night. Let
me off to-night; I will be more obedient next time.”
“He wants to get home to nurse his wife,” said Buchanan, with a
sneer.
“My wife is quite well,” answered Harry, with a quick flush of anger;
“she does not need my nursing, Mr. Buchanan.”
“Mr. Buchanan! don’t be ill-natured, Harry—come along.”
“No, no; I cannot go to-night. I don’t think I can stay to-night,” said
the brilliant facile clerk.
The entreaties continued a little longer; the resistance became
feebler and more feeble, and at last, stipulating that he was to leave
them early, the genius of the counting-house consented.
“Harry, my man, send a message to your wife,” said a grave snuffy
person, who enjoyed the two hundred pounds a year of which John
had boasted, and was cashier to the Messrs. Buchanan.
Harry wavered a moment. “Where is the boy?”
“Perhaps she’ll come for you, Harry,” suggested the malicious
Buchanan.
The poor clerk threw down, angrily, the pen he had taken up, and
lifted his hat. In another minute, with quickly recovered gaiety, they
went out in a band to the adjacent square where they were to dine.
“There’s the makings of a capital man in that lad, and there’s the
makings of a blackguard,” said the grave Mr. Gilchrist, shaking his
head ruefully, and taking a pinch of snuff; “it’ll be a hard race—
which of them will win?”
The dinner in George’s-square went off very well, and the young
clerk, as he warmed, dazzled the little company; he was only a clerk
—they were inclined to patronize him at other times—but now the
unmistakeable, undesired, pre-eminence, which these young men
yielded to their poor companion, was a noticeable thing. The matter
of ambition now, was, who should seem most intimate with—who
should most attract the attention of the brilliant clerk.
Cuthbert Charteris was a more completely educated man than any
other of the party. The thorough literary training will not ally itself to
the commercial, as it seems. None of the young merchants had time
for the long discipline and athletic mental exercises of the student.
They were all making money before they should have been well
emancipated from the school-room—all independent men, when
they should have been boys—and the contrast was marked enough.
There was a good deal of boisterousness in their enjoyment, and
they were enjoying themselves heartily, while Cuthbert, getting very
weary, felt himself only preserved from utter impatience of their
mirth by the interest with which the stranger inspired him—this poor,
clever, facile Harry Muir.
The quick mind of this young man seemed to have attained
somehow to the results of education without the training and
discipline which form so principal a part of it. He seemed to have
been a desultory reader, a devourer of everything which came in his
way, and while the Buchanans knew few books beyond the serial
literature of the time, Harry threw delicate allusions about him,
which it seemed he made only for his own enjoyment, since the
arrows flew most innocently over the heads of all the rest. Threads
of connection with those great thoughts which form the common
country of imaginative minds, ideas radiating out from the centre of
these, like the lessening circles in the water—the student Cuthbert
heard and understood, and wondered—the Buchanans applauded,
and did not understand.
One of them at last proposed to go to the theatre—the rest chimed
in eagerly. Cuthbert, anxious to have the evening concluded as soon
as possible, and resolving to seek no more of the delectable society
of his young cousins except at home, where they were tolerable,
remonstrated only to be laughed at and overpowered. The grown-
up, mature, educated man resigned himself to their boyish guidance
very wearily—and what would their wit do now?
He said he would go home—he took up his hat, and played
hesitatingly with his gloves. He was excited with the company, the
applause, and a little with the wine, and was permitting himself to
parley with the tempter.
“Come along, Muir, it’s only for once; let us just have this one
night.”
“No, no.” The noes grew faint; the hesitation increased. He
consented again.
And so, louder and more boisterous than before, they again
entered the busy streets. John Buchanan was a good deal inclined to
be obstreperous. It was all that Cuthbert could manage to keep him
within bounds.
They had reached the Trongate, and Cuthbert stopped his young
companion a moment to look down the long gleaming line of the
crowded street. It had been wet in the morning, and the brilliant
light from the shop windows glistened in the wet causeway in long
lines, and the shifting groups of passengers went and came,
ceaselessly, and the hum and din of the great thoroughfare was
softened by the gloom and brightened by the light of traffic that
illuminated all.
“What are you looking at? See they’re all away across the street.
What’s the good of glowering down the Trongate? Man, Cuthbert,
how slow you are,” said John Buchanan, dragging the loiterer on.
There was a crowd on the opposite side which had absorbed the
others. Cuthbert and John crossed over.
The accident which attracted the crowd was a very common one—
an overtasked horse, wearied with the long day’s labour, had
stumbled and fallen; and now, the weight of the cart to which it was
attached having been removed, was making convulsive plunges in
the effort to rise. The carters, and the kindred class who are always
to be found ready in such small emergencies, were leaping aside
themselves, and pressing back the lookers on, as the poor animal
struck out his great weary limbs, endeavouring to raise himself from
the ground.
Suddenly there was a shrill cry—“The wean—look at the wean; the
brute’s fit’ll kill the wean.”
John Buchanan had pushed his way into the crowd, dragging with
him the reluctant Cuthbert—and there indeed, close to the great
hoofs of the prostrate animal, stood one of those little pale,
careworn, withered children whom one sees only in the streets of
great cities, and oftenest only at this unwholesome hour of night.
But the acuteness peculiar to the class seemed to have forsaken the
very little wrinkled old man of the Trongate. He was standing where
the next plunge would inevitably throw him down, with the strange
scared look which is not fear, common to children in great peril,
upon his small white puckered face. Again the panting horse threw
out his hoofs in another convulsive exertion. The child was down.
A shadow shot across the light. There were several cries of women.
The child was thrown into somebody’s arms uninjured. The horse
was on its feet, and a man, indistinctly seen in the midst of the
eager crowd, struggled ineffectually to raise himself from the
ground, where he had fallen.
“I am hurt a little,” said the voice of Harry Muir. “Never mind, it is
not much, I dare say. Some of you help me up.”
There was a rush to assist him; a burst of eager inquiries.
“I got a blow from the hoof; ah! I can’t tell what it is,” gasped the
young man, over whose face the pallor of deadly sickness was
stealing. He could not stand. They carried him—these rough strong
men, so gently—with his friends crowding about him, to the nearest
surgeon’s. Everybody was sympathetic; every one interested. But
Harry Muir’s head had sunk upon his breast, and the fight had gone
from his eyes. He was conscious of nothing but pain.
The accident was a serious one; his leg was broken.
CHAPTER II.

“He sent me hither, stranger as I am,


To tell this story.”

as you like it.

“Cuthbert,” said Richard Buchanan, “do, like a good fellow, go and


tell his wife.”
“Do you not see, man, that a stranger would alarm her more? Why
make me the messenger? You say she knows you, Dick.”
“Ay, she knows him,” said the second brother, “but she does not
know him for any good. You see, Cuthbert, Dick’s always enticing
poor Muir away—as he did to-night—and the wife wouldn’t flatter
him if he went up now.”
“I don’t care a straw for the wife,” said Richard angrily. “It’s yon
grim sister Martha, and that white-faced monkey of a girl. I say,
Cuthbert—you needn’t go in, and they don’t know you—do go before
and tell them he’s coming. I’ll come up with him myself in the noddy
—just to oblige me, Cuthbert, will you go?”
“He lives in Port Dundas-road, it’s not very far. John will show you
where it is,” urged Alick.
Cuthbert consented to go; and the obstreperous John was very
much subdued, and very ready to accompany his cousin to poor
Muir’s house. It was now nearly ten o’clock. The young men were all
greatly concerned, and in an inner room poor Harry was getting his
leg examined, and looking so deadly sick and pale as to alarm both
surgeon and friends. It was his temperament, so finely organized, as
to feel either pain or pleasure far more exquisitely than is the
common lot.
“What will you say to them? Man, Cuthbert, are you not feared?”
asked John.
“Why should I be feared? I am very sorry for her, poor woman—but
is she such a fury, this wife?”
“It’s not the wife, it’s his eldest sister. Dick went home with Muir
one night when he was’nt quite able to take care of himself, and I
can tell you Dick was feared.”
“Dick was to blame—I do not feel that I am,” said Charteris; “but
why was he afraid?—did she say so much to him?”
“She did’nt say anything to him; but you know they say she’s awful
passionate, and she’s a great deal older than Harry; and she’s just
been like his mother. They’re always so strict, these old maids—and
Miss Muir’s an old maid.”
“Wait, then, till I see, John,” said Cuthbert; “don’t try to intimidate
me.”
“Yonder’s the house,” said John.
They had just passed a great quarry, across which the dome of
some large building loomed dark against the sky. Then there was a
field raised high above the road, with green grass waving over the
copestone of a high wall, and at the end of the field stood a solitary
house. A house of some pretension, for it boasted its street-door,
and was “self-contained;” and albeit the ground-floor on either side
was occupied by two not very ambitious shops, the upper flat looked
substantial and respectable, although decayed.
They were on the opposite side—the street was very quiet, and
their steps and voices echoed through it, so clearly that the loud
John sank into whispering and felt himself guilty. The light of a very
pale moon was shining into one of the windows. Looking up,
Cuthbert saw some one watching them—eagerly pressing against
the dark dull panes; as they crossed the street, the face suddenly
disappeared.
“That’s one of them,” whispered John. “Isn’t it awful that a poor
fellow can’t be out a little late, but these women are watching for
him that way?”
Cuthbert did not answer. He was thinking of “these women,” and of
their watching, rather than of the poor fellow who was the object of
it.
They had not time to knock, when the door was opened wide to
them, and a pale girl’s face looked out eagerly. She shrank back at
once with a look of blank disappointment which touched Cuthbert’s
heart, “I—I beg your pardon—I thought it was my brother.”
“Your brother will be here very soon. He has done a very brave
thing to-night, and has had a slight accident in consequence. I beg
you will not be alarmed,” said Cuthbert hastily.
“Oh! come in, sir, come in,” said the young sister. “A very brave
thing.” She repeated it again and again, under her breath.
“There’s the noddy,” whispered John, as he lingered behind. “I’ll
wait and help him in.”
The door admitted into a long paved passage, terminating in a little
damp “green.” John Buchanan remained at the door, while Cuthbert
followed the steps of his eager conductor, through the passage, and
up an “outside stair,” into the house. She seemed very eager, and
only looking round to see that he followed her, ran into a little
parlour.
“Harry is coming. He has been helping somebody, and has hurt
himself, Martha; the gentleman will tell you,” exclaimed poor Harry’s
anxious advocate, placing herself beside the chair where sat a tall
faded woman, sternly composed and quiet.
“Is Harry hurt?” cried another younger and prettier person, who
occupied the seat of honour by the fireside.
“He has done a very brave thing;” Cuthbert heard it whispered
earnestly, into the elder sister’s ear.
He told them the story. The little wife was excited and nervous—
she began to cry. The sister Martha sat firmly in her chair, her stern
face moved and melting. The younger girl stood behind, with her
arm round her sister, and her bright tearful face turned towards
Charteris. “Our Harry—our poor Harry! it was this that kept him,
Martha—and he saved the child.”
“What shall we do? Will he be lame?” sobbed the little wife.
The grave Martha suddenly rose from her chair as the faint sound
of wheels reached them. “He is here. Rose, make the room ready for
him, poor fellow. Do not let him see you crying, Agnes. Come to the
door, and meet him.”
They went away hastily, leaving Charteris still in the room. Rose
vanished by another door into an inner apartment. They were
overmuch excited and anxious to remember the courtesy due to a
stranger; and the stranger, for his part, was too much interested to
leave them until he had seen how the sufferer bore his removal.
“Rose,” said a very small voice, “has Harry come home?—Rose!”
Charteris looked round him a good deal puzzled, for there was no
visible owner of the little voice. There certainly was a cradle in a
corner, but nothing able to speak could inhabit that.
“Rose!”
There was no answer. Then there followed a faint rustling, and then
a third door opened, and a little head in a white nightcap, looked out
with a pair of bewildered dark eyes, and suddenly shrank in again,
when it found the room in possession of a stranger. The stranger
smiled at his own somewhat strange position, and began to move
towards the door—but suddenly the cradle gave sound of life, and a
lusty baby voice began to cry. They were carrying the baby’s father
then, into the house. The good-humoured Cuthbert rocked the
cradle.
Poor Harry was still very pale, though the surgeon who
accompanied him was as tender of him as the most delicate nurse,
and the strong young arms of the Buchanans carried the patient like
a child. They made their escape immediately, however,—but divided
between sympathy for the family, and a consciousness of his own
somewhat ridiculous position, Cuthbert stood at his post, rocking the
refractory cradle. They all passed into the inner apartment. He was
alone again.
It was a very plain parlour, and various articles of feminine work
were scattered about the room; some small garment for the sleeping
baby lay on the ground, where it had fallen from the young mother’s
hand; on the table, where Martha had been sitting, was a piece of
fine embroidery, stretched on two small hoops which fitted closely
into each other. She had been engaged in filling up the buds and
blossoms of those embroidered flowers with a species of fine
needlework, peculiar to Glasgow and its dependent provinces.
Another hoop, and another piece of delicate work, remained where
Rose had left it. The sisters of the poor clerk maintained themselves
so.
The baby voice had ceased. Groans of low pain were coming from
the inner room. Cuthbert felt that he did wrong to wait, and turned
again towards the door—but just then Miss Muir entered the parlour.
“The doctor thinks he will do well,” said Martha. “To-night I can
hardly thank you. But he is everything to us all—poor Harry!—and
to-night you will excuse us. We can think of nothing but himself.
Come again, and let us thank you?”
“I will come in the morning,” said Cuthbert, “not to be thanked, but
to hear how he is. Good night.”
She went with him to the door, gravely and calmly: when she had
shut it upon him, she stood still, alone in the dark, to press her
hands against her heart. Again—again!—so long she had hoped that
this facile temper would be steadied, that this poor brilliant
wandering star would be fixed in his proper orbit. So often, so
drearily, as her hopes had sunk into that blank of pain. Poor Harry! it
was all they could say of him. When others praised the gay wit, the
happy temper, the quick intelligence, those to whom he was dearest,
could only say, poor Harry! for the good and pleasant gifts he had,
made the bitterness of their grief only the deeper. Their pride in him
aggravated their shame. Darkest and saddest of all domestic
calamities these women, to whom he was so very dear, could not
trust the man in whom all their hopes and wishes centred. He had
not lost their affection—it seemed only the more surely to yearn over
and cling to him, for his faults—but he had lost their confidence.
They could not believe him: they could not rely upon word or
resolution of his. When Harry was an hour later than his usual time
of home-coming, Martha grew rigid in her chair, her strong heart
beating so loud that almost she could not hear those footsteps in the
street for which she watched with silent eagerness; and the work fell
from the hands of the young wife, and Rose stole away, pale and
agitated, into the inner room, to watch at the window in the
darkness; and even the little sister—the child—was moved with the
indefinite dread and melancholy which is the grief of childhood.
There were many grave people who would have smiled at poor
Harry’s sins, and counted them light and venial, but so did not these.
To lose confidence in those who are most dear to us, to be able no
longer to trust word or vow—it is the climax of womanish misery,—a
calamity terrible to bear!
And Martha Muir, under this discipline, was growing old. Morning
after morning there had been a rebound of eager hope, only to be
utterly cast down when the night fell. She had had something of the
mother’s pride in him—had transferred to Harry the natural ambition,
the eager hopes and wishes, which for herself had all faded with her
fading prime—and now, she who had so strong a will, so resolute a
mind, to see this man with all his gifts, and the free scope he had to
exercise them, sinking, falling, tarnishing with mean sins, the lustre
and glory of his youth. Poor Harry! his stern sad sister said nothing
more of blame—but as she turned again along the damp passage,
and up the stairs, the heart within her sank into the depths. She
pressed her hands upon it. Strange sympathy between the frame
and the spirit, which makes it no image to say that there is a weight
upon the heart!
“Martha, has Harry come home,” said the little sister, standing in
her white night-dress at the door of the small bed-closet which
opened from their parlour. The child’s eyes were bright and wide
open, as if, in her compulsory solitude in the closet, she had been
steadily fixing them to keep herself awake. “When I looked out I saw
a gentleman. And where’s Rose and Agnes, Martha. Is Harry no
weel?”
“You must go to bed, Violet,” said Martha. “Poor Harry has got a
broken leg. He was in the Trongate to-night with the Buchanans,
and saved a child’s life—but you cannot see him to-night—the doctor
is with him just now, poor fellow; go to bed—you shall see him to-
morrow.”
Little Violet began to cry, and the dark bewildered wide open eyes
looked up inquiringly into Martha’s face. Violet knew that Harry did
not need to be in the Trongate with the Buchanans, and that they all
waited for him very long before they would take their humble cup of
tea.
“He will not be able to go out for a long time, Violet—and he saved
the bairn’s life,” said Martha, as she put her little sister into the dark
closet bed, which she herself and Rose shared, “and you must not
cry—rather be thankful that the little boy’s mother has not lost him,
Lettie, and ask God to bless poor Harry—poor Harry! do you know
you should always think of him, Violet, when you pray?”
“And so I do, Martha,” said little Violet, looking up through her tears
as she clung to her elder sister, the only mother she had ever
known.
“Then you must let me go to him now, poor fellow,” said Martha.
“Hush! he will hear you crying—lie still, Lettie, and fall asleep.”
One of Violet’s tears rested on Martha’s faded cheek—other tears
came as she wiped it away. “Poor bairn—poor bairn,” said the elder
sister, “I might be her mother—and so I am.”
When she entered the sick-room, the surgeon was just preparing to
leave it. He had set the broken bone, and done all that could be
done to give his patient ease. Harry, greatly exhausted, and deadly
pale, was lying quiet, not strong enough to express even his
suffering by more than a faint groan—and his wife and Rose
watched anxiously beside him. But Harry’s mind was very much at
ease, and tranquil. His accident covered triumphantly any error he
had committed, and his anxious attendants were tranquil and
satisfied too—for who could think of Harry’s fault or weakness, when
Harry’s generous bravery had brought him so much pain. They were
content to believe—and they did believe, poor eager loving hearts!
that no one else could have been so daring—no one else had so little
thought of personal safety—and were saying, with tears in their
eyes, what a providence it was for the child and its mother, that “our
Harry,” and no other, was there to rescue it.
“I am to sit up with him, Martha,” said the little wife.
“But there is the baby, Agnes,” said Rose; “you must let me sit up
with Harry.”
“You must go away, both of you, and sleep,” said Martha. “Hush,
speak low! I cannot trust any of you, bairns—I must watch him
myself. No, little matron, not you. I must take care of my boy myself
—my poor Harry!”
These words so often said—expressing so much love, so much grief
—they were echoed in the hearts of all.
Poor Harry! but his conscience did not smite him to-night: only his
heart melted into tenderness for those who were so very tender of
him, and involuntarily there came into his mind, gentle thoughts of
all he would do for them, when he was well again; for Harry never
feared for himself.
They left his wife with him for a short time, and returned to the
fireside of the little parlour—it was Saturday night, and some of their
delicate work had to be finished, if possible, before the twelve
o’clock bell should begin the Sabbath-day.
They were but lodgers in this house. The mistress of it, a decayed
widow—strong, in her ancient gentility—had three daughters, who
maintained themselves and an idle brother by the same work which
occupied the Muirs. The collars and cuffs and handkerchiefs of richer
women, embroidered by other workers, principally in Ayr and
Ayrshire, were given out at warehouses in Glasgow, to the Muirs and
Rodgers, and multitudes of other such, to be “opened,” as they
called it—which “opening” meant filling up the centre of the
embroidered flowers with delicate open-work in a variety of
“stitches” innumerable. Very expert, and very industrious workers at
this, could, in busy times, earn as much as ten weekly shillings—and
thus it was that Martha and Rose Muir supported themselves and
their little sister, and were no burden on the scanty means of Harry.
“Well, Martha?” said Rose, breathlessly, as the door of the inner
room closed upon the little wife.
Martha could not lift up her eyes to meet her sister’s. “Well, my
dear?”
“I am sure,” said Rose, “I am sure, you are quite satisfied to-night.”
“Surely, surely,” said the less hopeful sister—a sigh bursting, in spite
of her, out of her heavy heart.
“Surely, surely—what do you mean, Martha?” said the dissatisfied
Rose. “Poor Harry! you are surely pleased with him to-night.”
“I said so, Rose,” said Martha. “Poor Harry!”
The younger sister did not speak for a moment—then she put her
work away and covered her face with her hands.
“You will never trust him—you will never trust Harry, Martha!”
Martha sighed. “I will trust God, Rose.”
Rose Muir dried her eyes, and took up her work again—there was
nothing to be said after that.
Martha was rocking the cradle softly with her foot; and Martha,
mother-like, was fain to divert the younger heart, and make it lighter
than her own. “Our poor wee Harry,” she said with a smile. “Did you
see what a strange nurse he had to-night?”
“Was it the gentleman?” said Rose; “did you say anything to him,
Martha—he would think us very ungrateful.”
“I can trust the person who rocks our cradle,” said Martha. “He is
coming back to-morrow to be thanked.”
“On Sabbath-day!”
“It is charity to come to Harry,” said Martha. “Poor Harry, how every
one likes him!”
Their eyes were becoming wet again—it was a relief to hear a quiet
knock at the parlour door.
The visitor was the younger Miss Rodger—a large, soft, clumsy,
good-humoured girl, with a pleasant comely face. She wore a
broken-down faded gown, which had once been very gay, and a little
woollen shawl, put on unevenly, over her plump shoulders, and her
hair in its enclosure of curl-papers for the night; ends of thread were
clinging to the fringes of the shawl, and the young lady was tugging
it over her shoulders, conscious of deficiencies below; but the good-
humoured offer to “take the wean,” or do anything that might be
needed, covered the eccentricities of Miss Aggie’s general house
dress and appearance. The precious child was not entrusted to her,
but the hoyden’s visit enlivened the sisters, and immediately after,
they finished their work, and Martha saw Rose and Agnes prepare
for rest, and then took her own place noiselessly by her brother’s
bedside.
CHAPTER III.

“How still and peaceful is the Sabbath morn!—


The pale mechanic now has room to breathe.”

graham.

Early on the following morning, Cuthbert Charteris, after a long


walk from his uncle’s house, presented himself at Harry Muir’s door.
The street was very still and Sabbath-like. Some young workmen, in
suits of snowy moleskin, stood grouped about the corner of the
Cowcaddens, enjoying the sunshine, and some few who were of the
more respectable Church-going class, and could not spend the after-
part of the day in such a manner, were returning from early walks.
There were very few shadows, however, to break the quiet
undisturbed sunshine of the usually crowded street.
The blinds were all drawn down in Mrs. Rodger’s respectable house
—all except one in the little parlour of the Muirs. The outer door
stood ajar—it was generally so during the day—and as Cuthbert
proceeded up the stairs, the grave doleful voice of some one reading
aloud struck on his ear. This, and the closely-veiled windows, made
him somewhat apprehensive—and he quickened his pace in
solicitude for the sufferer.
The door of the house was opened to him by a little slipshod
pseudo-Irish girl, who held the very unenviable situation of servant
to Mrs. Rodger. The door opened into a large airy lobby, at the
further end of which was Harry Muir’s little parlour; but Cuthbert’s
attention was drawn to another open door, through which he had a
glimpse of a large kitchen, with various figures, in strange dishabille,
pursuing various occupations in it—one engaged about her toilette—
one preparing breakfast—and another trying to smooth out with her

You might also like