How to set Java LookAndFeel to default WindowsLookAndFeel
My previous post-JOptionPane showMessageDialog I used UIManager to set swing components to default Windows LookAndFeel.
Today am going into details to explain how UIManager works
Today am going into details to explain how UIManager works
How to setLookAndFeel in java
Open your IDE and run below code.
[
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JTextArea; import javax.swing.JButton; import java.awt.Dimension; import javax.swing.UIManager; import java.awt.FlowLayout; public class SetJavaLookAndFeel extends JFrame { public JButton button; public JTextField name,email; public JTextArea area; public SetJavaLookAndFeel() { super("Setting Java LookAndFeel"); this.setPreferredSize(new Dimension(600,200)); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout(FlowLayout.RIGHT)); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } name=new JTextField("Enter your name"); name.setToolTipText("Your firstname or secondname"); email=new JTextField("Enter your email"); email.setToolTipText("Your email"); area=new JTextArea("Enter your message"); area.setToolTipText("Message to send "); button=new JButton("Send"); add(name); add(email); add(area); add(button); pack(); setVisible(true); } public static void main(String args[]) { new SetJavaLookAndFeel(); } }
]
Java LookAndFeel Code Explanation:
This program has graphical User interface that allows user to interact with the program when feeding program with inputs.
Some of the swing components used in this program are
- JTextField
- JTextArea
- JButton
To add ActionListener to JButton read here.
These are but a few swing components that are used to get user input.To Make the look like other programs that are running on windows we use UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel").
These are but a few swing components that are used to get user input.To Make the look like other programs that are running on windows we use UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel").
Other new methods in this program are.
- new Dimension(int i,int j)-this method takes two int variables.Used to create a dimension like rectangle or square
- setPreferredSize()-Its used to set the size of JFrame Window
- add(swing component eg button)-this method is used to add swing components to JFrame window.
- setLocationRelativeTo()-positions JFrame window at the center of the screen.
- setLayout()-used to set default JFrame window.
- pack()-used when using layout managers and you want all the fit JFrame window.Incase you have setSize of JFrame Window not need of using pack() method.
- setVisible(boolean)-makes JFrame Window visible to the screeen.