The Definitive Guide to SWT and JFace ROB WARNER WITH ROBERT HARRIS APress Media, LLC The Definitive Guide to SWT and JFace Copyright © 2004 by Rob Warner with Robert Harris Originally published by Apress in 2004 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN 978-1-59059-325-7 ISBN 978-1-4302-0686-6 (eBook) DOI 10.1007/978-1-4302-0686-6 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Gabor Liptak Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, John Franklin, Jason Gilmore, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole LeClerc Copy Editors: Susannah Pfalzer, Kim Wimpsett Production Manager: Karl Brooks Production Editor: Ellie Fountain Compositor: Linda Weidemann, Wolf Creek Press Proofreader: Patrick Vincent Indexer: Kevin Broccoli Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski In the United States: phone 1-800-SPRINGER, email orders@springer-ny. com, or visit http: 1l www. springer -ny. com. Outside the United States: fax +49 6221 345229, email orders@springer. de, or visit http: I lwww. springer. de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email info@apress .com, or visit http: I lwww. apress. com. The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http: I /www.apress.com in the Downloads section. To my son Tyson, age 10, who proclaimed this to be ''some dumb computer book about Java, divided by 27." -ROB WARNER For Charlie, Mallory, Alison, and mom. -ROBERT HARRIS Contents at a Glance About the Authors ................................................ xiii About the Technical Reviewer .................................... xiv Acknowledgments .................................................... xv Introduction ...................................................... xvii Part I Getting Ready Chapter 1 Evolution of Java GUis ............................. .1 Chapter 2 Getting Started with Eclipse ....................... 7 Part II Using SWT Chapter 3 Your First SWT Application ....................... .2 9 Chapter 4 Layouts ..............................................4 9 Chapter 5 Widgets ..............................................9 7 Chapter 6 Events ............................................. .1 51 Chapter 7 Dialogs ............................................ .1 69 Chapter 8 Advanced Controls ................................. .2 13 Chapter 9 The Custom Controls .............................. .2 79 Chapter 10 Graphics ........................................... .3 69 Chapter 11 Displaying and Editing Text ..................... .435 Chapter 12 Advanced Topics ................................... .497 Part III Using JFace Chapter 13 Your First JFace Application .................... .5 43 Chapter 14 Creating Viewers .................................. .5 51 Chapter 15 JFace Dialogs ..................................... .6 07 Chapter 16 User Interaction ...................................6 49 v Chapter 17 Using Preferences .................................. 701 Chapter 18 Editing Text ....................................... 739 Chapter 19 Miscellaneous Helper Classes ..................... 773 Chapter 20 Creating Wizards ................................... 793 Index ............................................................. .8 25 Contents About the Authors ................................................ xiii About the Technical Reviewer .................................... xiv Acknowledgments .................................................... xv Introduction ...................................................... xvii Part I Getting Ready Chapter 1 Evolution of Java GUis ......................... 1 AWT .................................................................. .1 Swing ............................................................... .2 SWT .................................................................. .5 JFace ............................................................... .5 Summary ............................................................. .6 Chapter 2 Getting Started with Eclipse ................. 7 Installing Eclipse ..................................................8 Creating Your First Program ...................................... 10 Including the SWT Libraries ......................................1 8 Getting Help .......................................................2 2 Alternatives to Eclipse ...........................................2 3 Summary ............................................................ .2 5 Part II Using SWT Chapter 3 Your First SWT Application ................... 29 "Hello, World" in SWT ............................................ .2 9 Understanding the Design Behind SWT .............................3 4 Understanding the Display Object ................................3 8 Understanding the Shell Object .................................. .42 The SWT Class-constants and Methods ............................ .45 Summary .............................................................4 7 vii Contents Chapter 4 Layouts ............................................. 49 Understanding Layouts ............................................ .5 0 Using FillLayout ...................................................5 0 Using RowLayout ................................................... .5 4 Using GridLayout .................................................. .5 8 Using FormLayout ...................................................6 6 Using StackLayout .................................................. 78 Creating Your Own Layout .........................................8 1 Not Using a Layout ................................................9 1 GUI Builders for SWT ..............................................9 3 Summary .............................................................9 5 Chapter 5 Widgets ............................................. 97 Introducing Widget ................................................9 7 Introducing Control ...............................................9 8 Introducing Label ................................................ .1 02 Introducing Button .............................................. .1 05 Introducing Text ..................................................1 08 Introducing List ................................................. .1 13 Introducing Combo .................................................1 17 Introducing Slider .............................................. .1 21 Introducing Group ................................................ .1 24 Introducing ScrollBar ........................................... .1 27 Introducing ProgressBar ......................................... .1 30 Introducing Menus .................................................1 34 Summary ........................................................... .1 49 Chapter 6 Events ............................................. 151 Understanding Untyped vs. Typed Listeners .................... .1 51 Using SelectionListener and Disposelistener ...................1 55 Using Controllistener ............................................1 57 Using Focus listener ............................................. .1 60 Using MouseListener, MouseMoveListener, and MouseTracklistener ....................................... .1 62 Using Several Listeners ......................................... .1 64 Summary ........................................................... .1 68 viii Contents Chapter 1 Dialogs ............................................ 169 Using the Dialogs ............................................... .1 69 Displaying Messages ............................................. .1 71 Choosing a Color ..................................................1 79 Browsing Directories ............................................ .1 84 Selecting Files for Open or Save ...............................1 89 Choosing a Font .................................................. .1 98 Creating Your Own Dialogs ...................................... .204 Summary ........................................................... .2 11 Chapter 8 Advanced Controls .............................. 213 Decorations ...................................................... .2 13 Tabs .............................................................. .2 19 Toolbars .......................................................... .2 27 Coolbars .......................................................... .2 42 Sashes ............................................................ .2 50 Tables ............................................................ .2 55 Trees ............................................................. .2 69 Combining Advanced Controls ............................. ........2 75 Summary ........................................................... .2 77 Chapter 9 The Custom Controls ........................... 279 Introducing Busyindicator ........................................2 80 Introducing CCombo .............................................. .283 Introducing Clabel .............................................. .286 Introducing CTabFolder .......................................... .2 95 Introducing TableTree ............................................3 05 Introducing Control Editors ............................. ........3 12 Introducing TableCursor ..........................................3 34 Introducing Popuplist ........................................... .3 39 Introducing SashForm .............................................3 43 Introducing ScrolledComposite .................................. .3 52 Introducing ViewForm .............................................3 58 Creating a Usable Example ...................................... .3 66 Summary ........................................................... .3 68 ix Contents Chapter 10 Graphics .......................................... 369 Drawing Shapes ................................................... .3 70 Drawing Text ..................................................... .3 90 Drawing Images ................................................... .4 18 Understanding Device ............................................ .431 Summary ........................................................... .4 34 Chapter 11 Displaying and Editing Text ................. 435 Using the StyledText API ........................................ 435 Using a LineStylelistener ..................................... .. 460 Using a LineBackgroundlistener ................................. .470 Adding Complexity ................................................ .473 Summary ........................................................... .4 96 Chapter 12 Advanced Topics ................................. 497 Dragging and Dropping ........................................... .497 Printing .......................................................... .5 06 Web Browsing ..................................................... .5 17 Digging into Programs ............................................5 36 Summary ........................................................... .5 40 Part III Using JFace Chapter 13 Your First JFace Application ............... 543 Greeting the World with JFace .................................. .5 44 Understanding the Relationship Between SWT and JFace ........5 47 Understanding the ApplicationWindow Class .....................5 48 A Word on WindowManagers ....................................... .5 49 Summary ........................................................... .5 50 Chapter 14 Creating Viewers ................................ 551 Tree Viewers ..................................................... .5 51 List Viewers ..................................................... .5 72 Table Viewers .................................................... .5 77 Cell Editors ......................................................5 92 Summary ........................................................... .6 06 X