Java Substance Look and Feel Example
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.
"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:
- 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.
- 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". - Synth: the basis for creating your own look and feel with an XML file.
- 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");
import
javax.swing.UIManager;
public
class
MainClass {
public
static
void
main(String[] a)
{
UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
for
(UIManager.LookAndFeelInfo look : looks) {
System.out.println(look.getClassName());
}
}
}
We will see different Look and Feel themes with the help of a simple calculator program:
- CrossPlatformLookAndFeel:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.UIManager;
import
javax.swing.JFrame;
class
Awt
implements
ActionListener {
JFrame f;
JButton addbut, subbut, mulbut, divbut, b5;
JTextField t1, t2, t3;
JLabel l, l1;
Awt()
{
f =
new
JFrame(
"Cross Platform Look and Feel"
);
t1 =
new
JTextField(
" "
);
t2 =
new
JTextField(
" "
);
t3 =
new
JTextField(
" "
);
addbut =
new
JButton(
"Add"
);
subbut =
new
JButton(
"Sub"
);
mulbut =
new
JButton(
"Mul"
);
divbut =
new
JButton(
"Div"
);
l =
new
JLabel();
l1 =
new
JLabel();
}
public
void
awt1()
{
f.setLayout(
new
GridLayout(
3
,
2
));
f.setVisible(
true
);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(addbut);
f.add(subbut);
f.add(mulbut);
f.add(divbut);
f.add(l);
f.add(l1);
addbut.addActionListener(
this
);
subbut.addActionListener(
this
);
mulbut.addActionListener(
this
);
divbut.addActionListener(
this
);
f.pack();
}
public
void
actionPerformed(ActionEvent e)
{
String s =
new
String(e.getActionCommand());
l.setText(s);
if
((s).equals(
"Add"
)) {
int
a = Integer.parseInt(t1.getText());
int
b = Integer.parseInt(t2.getText());
Integer c = a + b;
t3.setText(c.toString());
}
else
if
((s).equals(
"Sub"
)) {
int
a = Integer.parseInt(t1.getText());
int
b = Integer.parseInt(t2.getText());
Integer c = a - b;
t3.setText(c.toString());
}
else
if
((s).equals(
"Mul"
)) {
int
a = Integer.parseInt(t1.getText());
int
b = Integer.parseInt(t2.getText());
Integer c = a * b;
t3.setText(c.toString());
}
}
public
static
void
main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch
(Exception e) {
System.out.println(
"Look and Feel not set"
);
}
Awt a =
new
Awt();
a.awt1();
}
}
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");
- MotifLookAndFeel: Just change Line number 14 and 79 to:
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
Themes
Themes were introduced to change colors and fonts of default Java Metal Look And Feel. A constant can be initialised to one of 3 values:
- Default Metal
- Ocean
- Test
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:
- Substance
- Sea Glass
- Info Look And Feel
- Pgs Look And Feel
- Quaqua Look And Feel
- Oyaha
- Liquid Look And Feel
- JTattoo
References:
- https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- http://geeknizer.com/best-java-swing-look-and-feel-themes-professional-casual-top-10/
Source: https://www.geeksforgeeks.org/java-swing-look-feel/
0 Response to "Java Substance Look and Feel Example"
Post a Comment