Pluggable Look-and-Feel
We mentioned before that Swing’s peerless components can easily change their appearance, like master spies or thespians. Generally, different kinds of components have appearances that are similar in some way. For example, they probably use the same font and the same basic color scheme. The collection of appearances for different components is called a look-and-feel (L&F).
Part of the job of designing a GUI for an operating system is designing the L&F. MacOS, therefore, has its own distinctive L&F, as does Windows. Java 2 offers not one, not two, but three different L&F schemes for Swing components. If you’re adept at graphic design, you can write your own L&F schemes and easily convince Swing to use them. This chameleon-like ability to change appearance is called pluggable look-and-feel , sometimes abbreviated PLAF.
Seeing is believing. Here’s an example that creates a handful of Swing components. Menu items allow you to change the L&F dynamically, as the application is running:
//file: QuickChange.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class QuickChange extends JFrame { public QuickChange( ) { super("QuickChange v1.0"); createUI( ); setVisible(true); } protected void createUI( ) { setSize(300, 200); setLocation(200, 200); // create a simple File menu JMenu file = new JMenu("File", true); JMenuItem quit = new JMenuItem("Quit"); file.add(quit); quit.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access