13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

372Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProFigure 16.5 Dialog for creating a new subaccountthan <strong>on</strong>ce. We could throw away the dialog (or let it get garbage-collected) bydeclaring it internal to the acti<strong>on</strong>Performed() method. Then <strong>on</strong> each butt<strong>on</strong>press the dialog would need to be recreated. Well, it’s slower to do it that way,and for a butt<strong>on</strong> click we want quick resp<strong>on</strong>se—so we keep it around from <strong>on</strong>euse to the next. When the user closes the dialog, all that really does is makes itinvisible; to reuse it, we make it visible again.Notice, too, that in either case—creating the dialog or making it visible—c<strong>on</strong>troldoes not return to our method until the user has dismissed thedialog. That’s because it’s a modal dialog, <strong>on</strong>e that allows no other interacti<strong>on</strong>with the applicati<strong>on</strong> until the user has resp<strong>on</strong>ded to this dialog.The dialog is dismissed (finished, ended, put away) simply by making itno l<strong>on</strong>ger visible. For example:73 dialog.setVisible(false); // go awayNew to our applicati<strong>on</strong>, in AcctDialog, is the JTextField. On lines 22and 23 we declare two of them, <strong>on</strong>e for the account name and the other for theamount.22 nameField = new JTextField(25);23 amntField = new JTextField(9);The size that we pass in to the c<strong>on</strong>structor is the number of characters; itsets a maximum for that field, but also gives a clue to some layout managers asto how big the field needs to be.Speaking of layout managers, we use a few here, including a BoxLayout,to format the butt<strong>on</strong>s relative to each other; a BorderLayout, to hold theoverall dialog; and a newer layout manager, the SpringLayout, which is newas of Java 1.4. The Swing Tutorial provides a handy utility class for dealing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!