Swing
  • Swing is part of JFC (Java Foundation Classes) library. JFC includes other features for GUI programming.
  • All cultural preferences, collectively known as locale, include (but aren't limited to): language, currency formatting, time and date formatting, and numeric formatting.
  • Swing provides three generally useful top-level container classes: JFrame, JDialog, and JApplet.
  • Each top-level container has a content pane that contains the visible components in that top-level container's GUI.
  • You can optionally add a menu bar to a top-level container.
  • We can use html in swing: button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
  • Although Swing's model architecture is sometimes referred to as a Model-View-Controller (MVC) design, it really isn't. Swing components are generally implemented so that the view and controller are indivisible, implemented by a single UI object provided by the look and feel. The Swing model architecture is more accurately described as a separable model architecture. If you're interested in learning more about the Swing model architecture, see A Swing Architecture Overview, an article in The Swing Connection.

Concurrency in swing

Existing threads around a swing application:

  1. Initial threads, the threads that execute initial application code. These are the threads that invoke main/start/init method. The initial threads launch the first GUI task on the event dispatch thread where the rest of the GUI initialized.
  2. The event dispatch thread, where all event-handling code is executed. Most code that interacts with the Swing framework must also execute on this thread. Most Swing object methods are not "thread safe"; invoking them from multiple threads risks thread interference or memory consistency errors.
  3. Worker threads, also known as background threads, where time-consuming background tasks are executed. Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker. Long running tasks can be done using this abstract class.

The programmer does not need to provide code that explicitly creates these threads. They are provided by the runtime or the Swing framework. The programmer's job is to utilize these threads to create a responsive, maintainable Swing program.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License