Results 1 to 3 of 3
  1. #1
    Elidor is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Menu Look and Feel

    Hello, I'm still learning swing but I don't understand why my app won't update my look and feel. Here is my code:

    Java Code:
    import java.awt.EventQueue;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    
    
    public class FancyCalc extends Calculator
    {
        private JMenuBar menuBar;
        private JMenu file;
        private JMenu looks;
        
        public FancyCalc()
        {
            super();
            
            menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            
            file = new JMenu("File");
            looks = new JMenu("Looks");
            menuBar.add(file);
            file.add(looks);
            
            UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
            for (UIManager.LookAndFeelInfo info : infos)
                makeMenuItem(info.getName(), info.getClassName());
            
            pack();
        }
        
        /**
         * Specifically it makes look menuItems but that can be changed later
         */
        public void makeMenuItem(String label, final String plafName)
        {
            JMenuItem mItem = new JMenuItem(label);
            looks.add(mItem);
            
            looks.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        try
                        {
                            UIManager.setLookAndFeel(plafName);
                            SwingUtilities.updateComponentTreeUI(FancyCalc.this);
                            pack();
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
        }
        
        public static void main(String [] args)
        {
            EventQueue.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        JFrame frame = new FancyCalc();
                        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                        frame.setVisible(true);
                    }
                });
        }
    }
    Any help would be greatly appreciated. Let me know if I should post the base class if that is where the problem lies.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Menu Look and Feel

    You need to add the ActionListener to the JMenuItem, not the JMenu.

  3. #3
    Elidor is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Menu Look and Feel

    Quote Originally Posted by camickr View Post
    You need to add the ActionListener to the JMenuItem, not the JMenu.
    Thanks, sorry for wasting your time on such a dumb mistake.

Similar Threads

  1. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  2. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  3. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  4. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

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