New to JavaProgramming Center
Java Platform Overview |
Getting Started |
Learning Paths
![]()
Contents Do inner classes have direct access to instance variables and methods of the outer class?
An inner class has the special privilege of unlimited access to its enclosing class' members, even
if
they're declared The next section shows you how to implement this functionality using inner classes.
Inner Classes
Inner classes are defined within a class. In this case, the enclosing class is the
The
In the
case of this For example: private class GUIEventHandler implements ActionListener
Because the itemStateChanged(ItemEvent e) This method is invoked when an item has been selected or deselected, and performs the operations that need to occur when an item is selected or deselected: When a check box is selected, a check appears in the box. When deselected, the check disappears. To add to this, the Diver Data pane check box text turns blue when a check box is selected, and returns to the default color black when deselected.
Before delving into the pseudo code to plan the functionality, declare the inner class within the
Pseudo code List what should happen when the check boxes are selected or deselected: If a check box is selected:
Or else if the box is deselected:
You can see at a glance that an
To find out which check box fired an event, and then change the text for that box to blue, you use the
So you only have to be concerned with boxes that are selected, call the
Because
you aren't instantiating the JCheckBox source = (JCheckBox) e.getSource();
This line of code calls
Next, the e.getStateChange() == ItemEvent.SELECTED Now you can adjust your pseudo code more accurately with some real code:
JCheckBox source = (JCheckBox) e.getSource();
if ( e.getStateChange() == ItemEvent.SELECTED )
source.setForeground(Color.blue);
else
source.setForeground(Color.black);
Assigning and casting returned objects, then using the if/else statement allows you to write instruction for any check box that returns an object. The GUI is separate then from the functionality so you can add or remove more check boxes without having to change the code for the funcitonality.
Now you have an inner class that provides the functionality of the input components, while the enclosing class creates the GUI features that are presented to the user. If you need to change the functionality, you edit the inner class without affecting the enclosing class. Likewise, if you change the enclosing the class. the inner class remains unaffected, unless you add components that also need functionality added. Because the inner class only works with an instantiation of the enclosing class, it is protected from other classes accessing it. Using inner classes also makes it easier to read the code. You know to work on the enclosing class for GUI features, or the inner class for functionality. At this point, you have a program that displays labels and images, and provides text fields and check boxes for user input. When a user types data into the fields and presses Return or clicks the enter button, the data displays to the screen. There remains a problem: Once the application is closed, the data is lost. To save data, you need to write the information to files or a database. Part 4 teaches Java I/O basics that includes reading from and writing to files. SummaryPart 3 of the Dive Log tutorial reviewed Java programming concepts from Part 1 and Part 2 and introduced new ones. User Input Objects
Encapsulation and Access Control
Event Handling Basics
The Dive Log application classes serve as introductory examples to Java programming. It is not a comprehensive guide to the Java programming language, but instead as an example of an application that teaches basic Java programming concepts. The concepts are repeated in subsequent Dive Log tutorial parts, as each class that makes up the tabbed panes is defined. The Dive Log tutorial series covers more about methods, objects, and constructors in addition to creating other Swing GUI components and functionality. Each Dive Log class introduces new ideas as well as repeats what has been presented in earlier parts of the tutorial. In addition, each class representing a tabbed pane gets progressively more complex in terms of features and programming concepts. Part 4: Using pull-down menus, adding scrollbars to a text area, and reading from files and writing to files. About the AuthorDana Nourie is a JDC staff writer. She enjoys exploring the Java platform and creating interactive web applications using servlets and JavaServer Pages technologies, such as the JDC Quizzes, Learning Paths and Step-by-Step pages in the New to Java Programming Center. She is also a certified scuba diver and enjoys exploring the kelp forests and colorful tube anemone-covered floor of the Monterey Bay. Reader FeedbackTell us what you think of this tutorial.
Have a question about Java programming? Use Java Online Support.
| ||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||