Accessing the this Reference in an Object of a Top-Level Class From a Nested Class

When you execute this app and click one of its buttons, notice that the message dialog that appears is centered over the app’s window. This occurs because the call to JOptionPane method showMessageDialog (lines 44–45 of Fig. I.6) uses ButtonFrame.this rather than null as the first argument. When this argument is not null, it represents the so-called parent GUI component of the message dialog (in this case the app window is the parent component) and enables the dialog to be centered over that component when the dialog is displayed. ButtonFrame.this represents the this reference of the object of top-level class ButtonFrame.

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.