Skip to content
Anthony Christe edited this page Oct 16, 2013 · 6 revisions

GUI Frameworks

  • AWT - basic widgets and windows
  • Swing - provides native look and feel, platform independent, advanced widgets (tabs, scroll panes, tables, trees)
  • JavaFX - rich internet applications across platforms (sound familiar?)
  • SWT - started at IBM, now maintained by Eclipse, non-portable using JNI, better performance?

Building GUIs with Swing

Top Level Containers

  • Using Top Level Containers
  • To appear on screen, every component must be part of a GUI hierarchy
  • Each component can be contained only once
  • Menu bars can be added to top level containers
  • JFrame is generally used as top level container
  • Most of the GUI work is done in children containers such as JPanels

JComponents

  • JComponents are the building blocks of a Swing application
  • Provides UI widgets (buttons, text areas, scroll bars, tabs, menus, trees, progress bars, etc)

JPanels

  • JPanels provide an empty canvas to place JComponents onto
  • It's possible to include as many JPanels in an application as you need
  • JPanels can be embedded into other JPanels
  • JComponents can be added manually to a JPanels by x, y location
  • JComponents can be sized manually on a JPanel
  • But.... it's better to let Swing handle the layout of components, but how?

Layout Managers

  • Let Swing handle the layout of your components
  • Allows for automatic resizing of windows
  • Sub-components can also automatically be resized
  • Visual Guide to Swing Layouts
Flow Layout
  • Flow Layout Tutorial
  • Default layout manager for JPanels
  • Components are laid out in a single row
  • when the row is out of room, move on to next row
  • Simplest to use / least flexible
  • Generally used as part of a larger layout strategy
Box Layout
  • Box Layout Tutorial
  • Lays out items in a single column or single row
  • Alignment can be specfied
  • Generally used as part of a larger layout strategy
Grid Layout
  • Grid Layout Tutorial
  • Define the number of rows and columns in your JPanel
  • Add each component to the next available location (first by row, then my column)
  • Useful for when you want a grid (spreadsheet, calculator buttons, minesweeper, etc)
Border Layout
  • Border Layout Tutorial
  • Can add a SINGLE component to any of the cardinal directions (N, S, E, W) or center
  • Generally used to create the overall layout of your program
  • Often used for adding JPanels with different layout managers
  • I personally find that the use of border layout with the above layouts is enough to make a fairly complex and usable user interface

Basic Examples

Action Listeners
  • Action Listeners wait for events to be generated from gui componented (button click, tab change, etc)

  • Depending on the event that is triggered, action handlers can handle different events separately

  • Action listeners can be implemented as an anonymous class (as above) or implemented as an interface

  • Basic GUI with ActionListener

A Bit More Useful

A Bit More Fun

More Resources

Clone this wiki locally