-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookAndFeel.java
More file actions
97 lines (68 loc) · 2.73 KB
/
LookAndFeel.java
File metadata and controls
97 lines (68 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JButton;
public class Main extends JFrame {
private JPanel buttonPanel;
public Main() {
buttonPanel = new JPanel();
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo info : infos) {
makeButton(info.getName(), info.getClassName());
}
add(buttonPanel);
pack();
setSize(450,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void makeButton(String name, String className) {
JButton button = new JButton(name);
buttonPanel.add(button);
button.addActionListener(event -> {
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(this);
pack();
} catch (Exception e) {
e.printStackTrace();
}
});
}
public static void main(String[] args) {
new Main();
}
}
/*
description of the code
call the staticUiManagersetLookAndFeel method and give it the name of the look-and-feel class that you want
Then call the static method, SwingUtilities.updateComponentTreeUI to refresh the entire set of components
Supply one component to that method it will find others
Example where you can switch to the motif look-and-feel in your program
String className="com.sun.java.swing.plaf.MofiflookAndFeel"
try{
UIManager.setLookAndFeel(className);
Swingutilites.updateComponentTreeUI(frame);
pack();
}
catch(Exception e){
e.printStackTrace();
}
To mention all installed look-and-feel implemetations call
UIManager.LookAndFeelinfo[] infos=UIManager.getinstalledLookAndFeels();
Using this below code you will g et the name and class name for each look and feel as
String name=infos[i].getName();
String className=infos[i].getClassName();
Here in our program we have used lambda expression to specify the button action
javax.swing.UIManager
static UIManager.LookAndFeelInfo[] getInstalledLookAndFeels()
gets an array of objects that describe the installed look-and-feel-implementations
static setLookAndFeel(String className)
sets the current look-and-feel, using the given class name(such as"javax.swing.plaf.metal.MetalLookAndFeel")
java.swing.UIManager.LookAndFeelInfo
String getName()
returns the display name for the look-and-feel
String getClassName()
retuns the name of the implementation class for look-and-feel
*/