Java - Setting a GUI to native L&F-VBForums
Results 1 to 2 of 2

Thread: Java - Setting a GUI to native L&F

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,415

    Java - Setting a GUI to native L&F

    I whipped this up to show how one can set their GUI to use the underlying OS look and feel instead of the platform-neutral Java look & feel. The following example is pretty striaght forward but i will point out the relevant code snippets with bold. There are various methods contained within the javax.swing.UIManager class that are used in th code below and one from the javax.swing.SwingUtilities class. I will just list the relevant methods. I wont get into what exceptions these methods throw for that you can check out suns docs!

    http://java.sun.com/j2se/1.4.2/docs/...UIManager.html

    public static String getSystemLookAndFeelClassName()

    This method is used to get the underlying name of the look and feel that the OS uses.

    public static void setLookAndFeel(String classname);

    Well set the look and feel! Duh

    public static void updateComponentTreeUI(Component c)

    Ask each node in the tree to updateUI() -- that is, to initialize its UI property with the current look and feel.
    Code:
     import java.awt.*; 
     import javax.swing.*; 
     import java.awt.event.*;
    
     public class LF{
      public static void main(String[] args){
       JFrame jf = new JFrame("Look and Feel");
       JPanel jbp = new JPanel();
       JPanel jlp = new JPanel(); 
       
       Container c = jf.getContentPane(); 
       JButton jb = new JButton("Change Look And Feel");
       JLabel jl = new JLabel(UIManager.getLookAndFeel().toString());
       
       jb.addActionListener(new LookAndFeelL(jf,jl)); 
       jbp.add(jb);
       jlp.add(jl);
     
       c.add(jbp,BorderLayout.NORTH);
       c.add(jlp,BorderLayout.SOUTH);
       c.add(new JFileChooser(),BorderLayout.CENTER);    
       jf.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent evt){
           System.exit(0);
         } 
       });
       jf.setSize(700,450);
       jf.setVisible(true);
      }
     }
    
     class LookAndFeelL implements ActionListener{
      private JFrame jf; 
      private JLabel jl; 
      private boolean  toggle; 
      public LookAndFeelL(JFrame jf, JLabel jl){
         this.jf = jf; 
         this.jl = jl; 
      }
      public void actionPerformed(ActionEvent evt){
        try{
        if(!toggle){
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
          SwingUtilities.updateComponentTreeUI(jf);
          jl.setText(UIManager.getSystemLookAndFeelClassName());
           toggle = true; 
        }else{
         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
          SwingUtilities.updateComponentTreeUI(jf);
           jl.setText(UIManager.getLookAndFeel().toString()); 
           toggle = false;
        }
         }catch(UnsupportedLookAndFeelException uslf){
          System.err.println(uslf);
          }catch(Exception e){
           System.err.println(e);
         } 
        } 
       }

  2. #2
    Registered User
    Join Date
    Aug 2015
    Posts
    1

    Re: Java - Setting a GUI to native L&F

    to initialize its UI property with the current look and feel.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width