Look and feels java swing

Look and feels java swing

LookAndFeel , as the name implies, encapsulates a look and feel. Beyond installing a look and feel most developers never need to interact directly with LookAndFeel . In general only developers creating a custom look and feel need to concern themselves with this class. Swing is built upon the foundation that each JComponent subclass has an implementation of a specific ComponentUI subclass. The ComponentUI is often referred to as «the ui», «component ui», or «look and feel delegate». The ComponentUI subclass is responsible for providing the look and feel specific functionality of the component. For example, JTree requires an implementation of the ComponentUI subclass TreeUI . The implementation of the specific ComponentUI subclass is provided by the LookAndFeel . Each JComponent subclass identifies the ComponentUI subclass it requires by way of the JComponent method getUIClassID . Each LookAndFeel implementation must provide an implementation of the appropriate ComponentUI subclass by specifying a value for each of Swing’s ui class ids in the UIDefaults object returned from getDefaults . For example, BasicLookAndFeel uses BasicTreeUI as the concrete implementation for TreeUI . This is accomplished by BasicLookAndFeel providing the key-value pair «TreeUI»-«javax.swing.plaf.basic.BasicTreeUI» , in the UIDefaults returned from getDefaults . Refer to UIDefaults.getUI(JComponent) for details on how the implementation of the ComponentUI subclass is obtained. When a LookAndFeel is installed the UIManager does not check that an entry exists for all ui class ids. As such, random exceptions will occur if the current look and feel has not provided a value for a particular ui class id and an instance of the JComponent subclass is created.

Recommendations for Look and Feels

As noted in UIManager each LookAndFeel has the opportunity to provide a set of defaults that are layered in with developer and system defaults. Some of Swing’s components require the look and feel to provide a specific set of defaults. These are documented in the classes that require the specific default.

ComponentUIs and defaults

All ComponentUIs typically need to set various properties on the JComponent the ComponentUI is providing the look and feel for. This is typically done when the ComponentUI is installed on the JComponent . Setting a property should only be done if the developer has not set the property. For non-primitive values it is recommended that the ComponentUI only change the property on the JComponent if the current value is null or implements UIResource . If the current value is null or implements UIResource it indicates the property has not been set by the developer, and the ui is free to change it. For example, BasicButtonUI.installDefaults only changes the font on the JButton if the return value from button.getFont() is null or implements UIResource . On the other hand if button.getFont() returned a non-null value that did not implement UIResource then BasicButtonUI.installDefaults would not change the JButton ‘s font. For primitive values, such as opaque , the method installProperty should be invoked. installProperty only changes the corresponding property if the value has not been changed by the developer. ComponentUI implementations should use the various install methods provided by this class as they handle the necessary checking and install the property using the recommended guidelines.

Читайте также:  Upgrade python in conda

Exceptions

All of the install methods provided by LookAndFeel need to access the defaults if the value of the property being changed is null or a UIResource . For example, installing the font does the following:

JComponent c; Font font = c.getFont(); if (font == null || (font instanceof UIResource))

If the font is null or a UIResource , the defaults table is queried with the key fontKey . All of UIDefault’s get methods throw a NullPointerException if passed in null . As such, unless otherwise noted each of the various install methods of LookAndFeel throw a NullPointerException if the current value is null or a UIResource and the supplied defaults key is null . In addition, unless otherwise specified all of the install methods throw a NullPointerException if a null component is passed in.

Constructor Summary

Method Summary

Источник

Class LookAndFeel

LookAndFeel , as the name implies, encapsulates a look and feel. Beyond installing a look and feel most developers never need to interact directly with LookAndFeel . In general only developers creating a custom look and feel need to concern themselves with this class.

Swing is built upon the foundation that each JComponent subclass has an implementation of a specific ComponentUI subclass. The ComponentUI is often referred to as «the ui», «component ui», or «look and feel delegate». The ComponentUI subclass is responsible for providing the look and feel specific functionality of the component. For example, JTree requires an implementation of the ComponentUI subclass TreeUI . The implementation of the specific ComponentUI subclass is provided by the LookAndFeel . Each JComponent subclass identifies the ComponentUI subclass it requires by way of the JComponent method getUIClassID .

Each LookAndFeel implementation must provide an implementation of the appropriate ComponentUI subclass by specifying a value for each of Swing’s ui class ids in the UIDefaults object returned from getDefaults . For example, BasicLookAndFeel uses BasicTreeUI as the concrete implementation for TreeUI . This is accomplished by BasicLookAndFeel providing the key-value pair «TreeUI»-«javax.swing.plaf.basic.BasicTreeUI» , in the UIDefaults returned from getDefaults . Refer to UIDefaults.getUI(JComponent) for details on how the implementation of the ComponentUI subclass is obtained.

When a LookAndFeel is installed the UIManager does not check that an entry exists for all ui class ids. As such, random exceptions will occur if the current look and feel has not provided a value for a particular ui class id and an instance of the JComponent subclass is created.

Читайте также:  Python sqlalchemy query select

Recommendations for Look and Feels

As noted in UIManager each LookAndFeel has the opportunity to provide a set of defaults that are layered in with developer and system defaults. Some of Swing’s components require the look and feel to provide a specific set of defaults. These are documented in the classes that require the specific default.

ComponentUIs and defaults

All ComponentUIs typically need to set various properties on the JComponent the ComponentUI is providing the look and feel for. This is typically done when the ComponentUI is installed on the JComponent . Setting a property should only be done if the developer has not set the property. For non-primitive values it is recommended that the ComponentUI only change the property on the JComponent if the current value is null or implements UIResource . If the current value is null or implements UIResource it indicates the property has not been set by the developer, and the ui is free to change it. For example, BasicButtonUI.installDefaults only changes the font on the JButton if the return value from button.getFont() is null or implements UIResource . On the other hand if button.getFont() returned a non-null value that did not implement UIResource then BasicButtonUI.installDefaults would not change the JButton ‘s font.

For primitive values, such as opaque , the method installProperty should be invoked. installProperty only changes the corresponding property if the value has not been changed by the developer.

ComponentUI implementations should use the various install methods provided by this class as they handle the necessary checking and install the property using the recommended guidelines.

Exceptions

All of the install methods provided by LookAndFeel need to access the defaults if the value of the property being changed is null or a UIResource . For example, installing the font does the following:

JComponent c; Font font = c.getFont(); if (font == null || (font instanceof UIResource))

If the font is null or a UIResource , the defaults table is queried with the key fontKey . All of UIDefault’s get methods throw a NullPointerException if passed in null . As such, unless otherwise noted each of the various install methods of LookAndFeel throw a NullPointerException if the current value is null or a UIResource and the supplied defaults key is null . In addition, unless otherwise specified all of the install methods throw a NullPointerException if a null component is passed in.

Источник

Java Swing | Look and Feel

Swing is GUI Widget Toolkit for Java. It is an API for providing Graphical User Interface to Java Programs. Unlike AWT, Swing components are written in Java and therefore are platform-independent. Swing provides platform specific Look and Feel and also an option for pluggable Look and Feel, allowing application to have Look and Feel independent of underlying platform.
Initially there were very few options for colors and other settings in Java Swing, that made the entire application look boring and monotonous. With the growth in Java framework, new changes were introduced to make the UI better and thus giving developer opportunity to enhance the look of a Java Swing Application.

Читайте также:  Java debug output to file

“Look” refers to the appearance of GUI widgets and “feel” refers to the way the widgets behave.
Sun’s JRE provides the following L&Fs:

  1. CrossPlatformLookAndFeel: this is the “Java L&F” also known as “Metal” that looks the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the default.
  2. SystemLookAndFeel: here, the application uses the L&F that is default to the system it is running on. The System L&F is determined at runtime, where the application asks the system to return the name of the appropriate L&F.
    For Linux and Solaris, the System L&Fs are “GTK+” if GTK+ 2.2 or later is installed, “Motif” otherwise. For Windows, the System L&F is “Windows”.
  3. Synth: the basis for creating your own look and feel with an XML file.
  4. Multiplexing: a way to have the UI methods delegate to a number of different look and feel implementations at the same time.

We can use UIManager to load the L&F class directly from classpath. For which the code goes like this:

UIManager.setLookAndFeel("fully qualified name of look and feel");

For example, following code changes application Look and Feel to Motif Look And Feel:

UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

We will see different Look and Feel themes with the help of a simple calculator program:

You can also use actual class name of Look And Feel as argument to UIManager.setLookAndFeel(). For example,
// Set cross-platform Java L&F (also called “Metal”)

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
Line 14: f=new JFrame("Motif Look and Feel"); Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

SystemLookAndFeel: Change Line number 14 and 79 to:

Line 14: f=new JFrame("System Look and Feel"); Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

WindowsClassicLookAndFeel: Change Line number 14 and 79 to:

Line 14: f=new JFrame("WindowsClassic Look and Feel"); Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

NimbusLookAndFeel: Change Line number 14 and 79 to:

Line 14: f=new JFrame("Nimbus Look and Feel"); Line 79: UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

Specifying Look And Feel using Command Line Arguments You can specify the Look And Feel by using -D flag at the command line to set the swing.defaultlaf property. Example –
We will run the above code excluding Line 76 and executing following command:

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel Awt

Specifying Look And Feel by editing swing.properties file In this we will edit swing.properties file to set the swing.defaultlaf property. This file is located in lib directory. Here is an example-

swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

There is option for professional themes which one can download and use in the code. Here is the list of professional themes that are available:

  1. Substance
  2. Sea Glass
  3. Info Look And Feel
  4. Pgs Look And Feel
  5. Quaqua Look And Feel
  6. Oyaha
  7. Liquid Look And Feel
  8. JTattoo
  1. https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  2. http://geeknizer.com/best-java-swing-look-and-feel-themes-professional-casual-top-10/

Источник

Оцените статью