Chapter 6
Introduction to GUI Programming
Computer users today expect to interact with their computers using a graphical user interface (GUI), and Java can be used to write sophisticated GUI programs.
GUI programs differ from traditional "straight-through" programs that you have encountered in the first few chapters of this book. One big difference is that GUI programs are event-driven. That is, user actions such as clicking on a button or pressing a key on the keyboard generate events, and the program must respond to these events as they occur.
Event-driven programming builds on all the skills you have learned in the first five chapters of this text. You need to be able to write the methods that respond to events. Inside those methods, you are doing the kind of programming-in-the-small that was covered in Chapter 2 and Chapter 3. And of course, objects are everywhere in GUI programming. Events are objects. Colors and fonts are objects. GUI components such as buttons and menus are objects. Events are handled by instance methods contained in objects. In Java, GUI programming is object-oriented programming. The purpose of this chapter is, as much as anything, to give you some experience with a large-scale object-oriented API.
Java has had several "toolkits" for building GUI program. This textbook uses JavaFX as its toolkit. The previous version used the Swing GUI toolkit, and if you are interested in learning Swing rather than JavaFX, you should consult Chapter 6 and Chapter 13 in that version, which is still available at http://math.hws.edu/javanotes7.
This chapter is just an introduction to JavaFX, but it covers the essential features of GUI programming in enough detail to write some interesting programs. The discussion of JavaFX will continue in Chapter 13 with more details and with more advanced techniques, but complete coverage of JavaFX would require an entire book of its own.
Note that JavaFX is no longer being distributed as part of the Java Development Kit. For information about how to obtain JavaFX and how to compile and run programs that use it, see Section 2.6.
Contents of Chapter 6:
- Section 1: A Basic JavaFX Application
- Section 2: Some Basic Classes
- Section 3: Basic Events
- Section 4: Basic Controls
- Section 5: Basic Layout
- Section 6: Complete Programs
- Programming Exercises
- Quiz on This Chapter