Ask Questions?

View Latest Questions



GUI Tips
Posted on: July 22, 2006 at 12:00 AM
Beginning of list of GUI tips -- needs much more

Java Notes

GUI Tips

[Beginning of list of GUI tips -- needs much more]

Program structure

  • main can be in any class, but it's often simplest to understand if it's in a separate class.
  • main should do very little work -- typically this means just create a window (JFrame) and display it.
  • A graphics panel should be a subclass of JPanel and override paintComponent.
  • A graphics panel may be declared inside an the enclosing panel or window class so that it may use the instance variables of the outer class.

Events and communication

After the GUI is constructed, the program stops execution until the user does something that causes listener to called.
  • Instance variables are the way a listener typically communications changes.
  • Listener methods (eg, actionPerformed) may be mixed in with the GUI construction code. This makes the code hard to understand. Define methods that these listeners call and put those methods in one place, so the GUI construction can be separated from the GUI execution.

Building the GUI

  • Do not add elements to a layout after the GUI has already been started. If you want to display a different layout of components, use a JTabbedPane or CardLayout.
Copyleft 2003 Fred Swartz MIT License, Last update: 2003-05-13