ebook img

Java APIs, Extensions and Libraries PDF

826 Pages·2018·9.64 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java APIs, Extensions and Libraries

Java APIs, Extensions and Libraries With JavaFX, JDBC, jmod, jlink, Networking, and the Process API — Second Edition — Kishori Sharan Java APIs, Extensions and Libraries With JavaFX, JDBC, jmod, jlink, Networking, and the Process API Second Edition Kishori Sharan Java APIs, Extensions and Libraries: With JavaFX, JDBC, jmod, jlink, Networking, and the Process API Kishori Sharan Montgomery, Alabama, USA ISBN-13 (pbk): 978-1-4842-3545-4 ISBN-13 (electronic): 978-1-4842-3546-1 https://doi.org/10.1007/978-1-4842-3546-1 Library of Congress Control Number: 2018939410 Copyright © 2018 by Kishori Sharan 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: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. 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 email [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 via the book’s product page, located at www.apress.com/9781484235454. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Contents About the Author ���������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Acknowledgments ��������������������������������������������������������������������������������������������������xxi Introduction ����������������������������������������������������������������������������������������������������������xxiii ■ Chapter 1: Introduction to Swing ��������������������������������������������������������������������������1 What Is Swing? ����������������������������������������������������������������������������������������������������������������2 The Simplest Swing Program �������������������������������������������������������������������������������������������3 Components of a JFrame �������������������������������������������������������������������������������������������������7 Adding Components to a JFrame �������������������������������������������������������������������������������������9 Some Utility Classes �������������������������������������������������������������������������������������������������������13 The Point Class �������������������������������������������������������������������������������������������������������������������������������������13 The Dimension Class ����������������������������������������������������������������������������������������������������������������������������13 The Insets Class ������������������������������������������������������������������������������������������������������������������������������������14 The Rectangle Class �����������������������������������������������������������������������������������������������������������������������������14 Layout Managers ������������������������������������������������������������������������������������������������������������15 FlowLayout �������������������������������������������������������������������������������������������������������������������������������������������16 BorderLayout ����������������������������������������������������������������������������������������������������������������������������������������21 CardLayout��������������������������������������������������������������������������������������������������������������������������������������������24 BoxLayout ���������������������������������������������������������������������������������������������������������������������������������������������26 GridLayout ��������������������������������������������������������������������������������������������������������������������������������������������31 GridBagLayout ��������������������������������������������������������������������������������������������������������������������������������������33 SpringLayout ����������������������������������������������������������������������������������������������������������������������������������������51 GroupLayout �����������������������������������������������������������������������������������������������������������������������������������������59 The null Layout Manager ����������������������������������������������������������������������������������������������������������������������68 iii ■ Contents Creating a Reusable JFrame ������������������������������������������������������������������������������������������70 Event Handling ���������������������������������������������������������������������������������������������������������������72 Handling Mouse Events ��������������������������������������������������������������������������������������������������79 Summary ������������������������������������������������������������������������������������������������������������������������82 ■ Chapter 2: Swing Components ����������������������������������������������������������������������������85 What Is a Swing Component? ����������������������������������������������������������������������������������������85 JButton ���������������������������������������������������������������������������������������������������������������������������90 JPanel�����������������������������������������������������������������������������������������������������������������������������95 JLabel �����������������������������������������������������������������������������������������������������������������������������96 Text Components ������������������������������������������������������������������������������������������������������������97 JTextComponent ���������������������������������������������������������������������������������������������������������������������������������100 JTextField��������������������������������������������������������������������������������������������������������������������������������������������102 JPasswordField ����������������������������������������������������������������������������������������������������������������������������������107 JFormattedTextField ���������������������������������������������������������������������������������������������������������������������������108 JTextArea ��������������������������������������������������������������������������������������������������������������������������������������������111 JEditorPane ����������������������������������������������������������������������������������������������������������������������������������������114 JTextPane �������������������������������������������������������������������������������������������������������������������������������������������119 Validating Text Input �����������������������������������������������������������������������������������������������������127 Making Choices ������������������������������������������������������������������������������������������������������������128 JSpinner �����������������������������������������������������������������������������������������������������������������������137 JScrollBar ���������������������������������������������������������������������������������������������������������������������139 JScrollPane ������������������������������������������������������������������������������������������������������������������140 JProgressBar ����������������������������������������������������������������������������������������������������������������142 JSlider ��������������������������������������������������������������������������������������������������������������������������143 JSeparator ��������������������������������������������������������������������������������������������������������������������145 Menus���������������������������������������������������������������������������������������������������������������������������145 JToolBar������������������������������������������������������������������������������������������������������������������������153 JToolBar Meets the Action Interface �����������������������������������������������������������������������������156 JTable ���������������������������������������������������������������������������������������������������������������������������157 iv ■ Contents JTree �����������������������������������������������������������������������������������������������������������������������������163 JTabbedPane and JSplitPane ���������������������������������������������������������������������������������������169 Custom Dialogs ������������������������������������������������������������������������������������������������������������171 Standard Dialogs ����������������������������������������������������������������������������������������������������������174 File and Color Choosers������������������������������������������������������������������������������������������������181 JFileChooser ���������������������������������������������������������������������������������������������������������������������������������������181 JColorChooser ������������������������������������������������������������������������������������������������������������������������������������185 JWindow �����������������������������������������������������������������������������������������������������������������������186 Working with Colors �����������������������������������������������������������������������������������������������������186 Working with Borders ���������������������������������������������������������������������������������������������������187 Working with Fonts ������������������������������������������������������������������������������������������������������190 Validating Components�������������������������������������������������������������������������������������������������192 Painting Components and Drawing Shapes �����������������������������������������������������������������193 Immediate Painting ������������������������������������������������������������������������������������������������������198 Double Buffering ����������������������������������������������������������������������������������������������������������198 JFrame Revisited ����������������������������������������������������������������������������������������������������������200 Summary ����������������������������������������������������������������������������������������������������������������������202 ■ Chapter 3: Advanced Swing ������������������������������������������������������������������������������205 Using HTML in Swing Components ������������������������������������������������������������������������������206 Threading Model in Swing ��������������������������������������������������������������������������������������������207 Pluggable Look and Feel ����������������������������������������������������������������������������������������������215 Drag and Drop ��������������������������������������������������������������������������������������������������������������221 Multiple Document Interface Application ���������������������������������������������������������������������229 The Toolkit Class �����������������������������������������������������������������������������������������������������������232 Decorating Components Using JLayer �������������������������������������������������������������������������234 Translucent Windows ���������������������������������������������������������������������������������������������������241 Shaped Window ������������������������������������������������������������������������������������������������������������247 Summary ����������������������������������������������������������������������������������������������������������������������250 v ■ Contents ■ Chapter 4: Network Programming ���������������������������������������������������������������������253 What Is Network Programming? ����������������������������������������������������������������������������������253 Network Protocol Suite ������������������������������������������������������������������������������������������������255 IP Addressing Scheme ��������������������������������������������������������������������������������������������������258 IPv4 Addressing Scheme ��������������������������������������������������������������������������������������������������������������������258 IPv6 Addressing Scheme ��������������������������������������������������������������������������������������������������������������������261 Special IP Addresses ����������������������������������������������������������������������������������������������������262 Loopback IP Address ��������������������������������������������������������������������������������������������������������������������������262 Unicast IP Address ������������������������������������������������������������������������������������������������������������������������������263 Multicast IP Address ���������������������������������������������������������������������������������������������������������������������������264 Anycast IP Address �����������������������������������������������������������������������������������������������������������������������������264 Broadcast IP Address ��������������������������������������������������������������������������������������������������������������������������264 Unspecified IP Address �����������������������������������������������������������������������������������������������������������������������265 Port Numbers ���������������������������������������������������������������������������������������������������������������265 Socket API and Client-Server Paradigm �����������������������������������������������������������������������266 The Socket Primitive ���������������������������������������������������������������������������������������������������������������������������268 The Bind Primitive ������������������������������������������������������������������������������������������������������������������������������268 The Listen Primitive ����������������������������������������������������������������������������������������������������������������������������269 The Accept Primitive ���������������������������������������������������������������������������������������������������������������������������269 The Connect Primitive�������������������������������������������������������������������������������������������������������������������������269 The Send/Sendto Primitive �����������������������������������������������������������������������������������������������������������������270 The Receive/ReceiveFrom Primitive ���������������������������������������������������������������������������������������������������270 The Close Primitive �����������������������������������������������������������������������������������������������������������������������������270 Representing a Machine Address ���������������������������������������������������������������������������������270 Representing a Socket Address �����������������������������������������������������������������������������������273 Creating a TCP Server Socket ��������������������������������������������������������������������������������������274 Creating a TCP Client Socket ����������������������������������������������������������������������������������������278 Putting a TCP Server and Clients Together �������������������������������������������������������������������280 vi ■ Contents Working with UDP Sockets �������������������������������������������������������������������������������������������281 Creating a UDP Echo Server �����������������������������������������������������������������������������������������284 A Connected UDP Socket ����������������������������������������������������������������������������������������������288 UDP Multicast Sockets �������������������������������������������������������������������������������������������������289 URI, URL, and URN ��������������������������������������������������������������������������������������������������������292 URI and URL as Java Objects����������������������������������������������������������������������������������������295 Accessing the Contents of a URL ���������������������������������������������������������������������������������299 Non-Blocking Socket Programming �����������������������������������������������������������������������������306 Socket Security Permissions ����������������������������������������������������������������������������������������318 Asynchronous Socket Channels �����������������������������������������������������������������������������������319 Setting Up an Asynchronous Server Socket Channel �������������������������������������������������������������������������320 Setting Up an Asynchronous Client Socket Channel ��������������������������������������������������������������������������327 Putting the Server and the Client Together �����������������������������������������������������������������������������������������330 Datagram-Oriented Socket Channels ���������������������������������������������������������������������������332 Creating the Datagram Channel ���������������������������������������������������������������������������������������������������������332 Setting the Channel Options ���������������������������������������������������������������������������������������������������������������332 Sending Datagrams ����������������������������������������������������������������������������������������������������������������������������334 Multicasting Using Datagram Channels �����������������������������������������������������������������������337 Creating the Datagram Channel ���������������������������������������������������������������������������������������������������������337 Setting the Channel Options ���������������������������������������������������������������������������������������������������������������337 Binding the Channel ���������������������������������������������������������������������������������������������������������������������������337 Setting the Multicast Network Interface ���������������������������������������������������������������������������������������������338 Joining the Multicast Group ����������������������������������������������������������������������������������������������������������������339 Receiving a Message ��������������������������������������������������������������������������������������������������������������������������340 Closing the Channel ����������������������������������������������������������������������������������������������������������������������������340 Further Reading ������������������������������������������������������������������������������������������������������������343 Summary ����������������������������������������������������������������������������������������������������������������������343 vii ■ Contents ■ Chapter 5: JDBC API ������������������������������������������������������������������������������������������347 What Is the JDBC API? ��������������������������������������������������������������������������������������������������348 System Requirements ��������������������������������������������������������������������������������������������������348 Types of JDBC Drivers ��������������������������������������������������������������������������������������������������349 JDBC Native API Driver �����������������������������������������������������������������������������������������������������������������������349 JDBC-Net Driver ���������������������������������������������������������������������������������������������������������������������������������349 JDBC Driver ����������������������������������������������������������������������������������������������������������������������������������������350 A Brief Overview of Apache Derby ��������������������������������������������������������������������������������350 Downloading Derby ����������������������������������������������������������������������������������������������������������������������������350 Installing Derby �����������������������������������������������������������������������������������������������������������������������������������350 Derby Installation Files �����������������������������������������������������������������������������������������������������������������������350 Configuring Derby �������������������������������������������������������������������������������������������������������������������������������351 Running the Derby Server ������������������������������������������������������������������������������������������������������������������351 Creating a Database Table ��������������������������������������������������������������������������������������������355 Oracle Database ���������������������������������������������������������������������������������������������������������������������������������356 Adaptive Server Anywhere Database �������������������������������������������������������������������������������������������������356 SQL Server Database ��������������������������������������������������������������������������������������������������������������������������356 DB2 Database �������������������������������������������������������������������������������������������������������������������������������������356 MySQL Database ��������������������������������������������������������������������������������������������������������������������������������357 Apache Derby Database ���������������������������������������������������������������������������������������������������������������������357 Connecting to a Database ��������������������������������������������������������������������������������������������357 Obtaining the JDBC Driver ������������������������������������������������������������������������������������������������������������������357 Setting Up the Module Path ����������������������������������������������������������������������������������������������������������������358 Registering a JDBC Driver ������������������������������������������������������������������������������������������������������������������358 Constructing a Connection URL ����������������������������������������������������������������������������������������������������������360 Establishing the Database Connection �����������������������������������������������������������������������������������������������364 Setting the Auto-Commit Mode ������������������������������������������������������������������������������������369 Committing and Rolling Back Transactions ������������������������������������������������������������������369 viii ■ Contents Transaction Isolation Level �������������������������������������������������������������������������������������������370 Dirty Read �������������������������������������������������������������������������������������������������������������������������������������������370 Non-Repeatable Read �������������������������������������������������������������������������������������������������������������������������370 Phantom Read ������������������������������������������������������������������������������������������������������������������������������������371 JDBC-Types-to-Java-Types Mapping ���������������������������������������������������������������������������372 Knowing About the Database ���������������������������������������������������������������������������������������375 Executing SQL Statements �������������������������������������������������������������������������������������������377 Results of Executing a SQL Statement �����������������������������������������������������������������������������������������������378 Using the Statement Interface ������������������������������������������������������������������������������������������������������������379 Using the PreparedStatement Interface ���������������������������������������������������������������������������������������������386 CallableStatement Interface ���������������������������������������������������������������������������������������������������������������389 Processing Result Sets �������������������������������������������������������������������������������������������������402 What Is a ResultSet?���������������������������������������������������������������������������������������������������������������������������402 Getting a ResultSet �����������������������������������������������������������������������������������������������������������������������������406 Getting the Number of Rows in a ResultSet ���������������������������������������������������������������������������������������412 Bidirectional Scrollable ResultSets �����������������������������������������������������������������������������������������������������415 Scrolling Through Rows of a ResultSet ����������������������������������������������������������������������������������������������417 Knowing the Cursor Position in a ResultSet ���������������������������������������������������������������������������������������420 Closing a ResultSet �����������������������������������������������������������������������������������������������������������������������������420 Making Changes to a ResultSet �����������������������������������������������������������������������������������421 Inserting a Row Using a ResultSet �����������������������������������������������������������������������������������������������������421 Updating a Row Using a ResultSet �����������������������������������������������������������������������������������������������������423 Deleting a Row Using a ResultSet ������������������������������������������������������������������������������������������������������426 Handling Multiple Results from a Statement����������������������������������������������������������������426 Getting a Result Set from a Stored Procedure �������������������������������������������������������������428 MySQL Database ��������������������������������������������������������������������������������������������������������������������������������429 Adaptive Server Anywhere Database �������������������������������������������������������������������������������������������������429 Oracle Database ���������������������������������������������������������������������������������������������������������������������������������429 SQL Server Database ��������������������������������������������������������������������������������������������������������������������������430 DB2 Database �������������������������������������������������������������������������������������������������������������������������������������430 Apache Derby Database ���������������������������������������������������������������������������������������������������������������������431 ix

Description:
This book completes the Apress Java learning journey and is a comprehensive approach to learning Java APIs, extensions, and modules such as Java EE integration, mobile Java modules, JavaFX, and JDBC. In this book, you'll learn how to build user interfaces with Swing and JavaFX as well as how to writ
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.