views:

271

answers:

4

I’m developing a swing application in which I have an option to change the Look and feel of the application on click of a button. Now my problem is when I click the button to change the theme it’s not properly updating the L&F of my app, say my previous theme is “noire” and I choose “MCWin” after it, but the style of the noire theme is still there

Here is sample working code:

package whiteboard;

    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.WindowConstants;



    public class DiscussionBoard extends JFrame implements ComponentListener,ActionListener {   

        // Variables declaration
        private JMenuItem audioMenuItem;
        private JMenuItem boardMenuItem;
        private JMenuItem exitMenuItem;
        private JMenuItem clientsMenuItem;
        private JMenuItem acryl;
        private JMenuItem hifi;
        private JMenuItem aero;
        private JMenuItem aluminium;
        private JMenuItem bernstein;
        private JMenuItem   fast;
        private JMenuItem graphite;
        private JMenuItem luna;
        private JMenuItem mcwin;

        private JMenuItem noire;
        private JMenuItem smart;
        private JMenuBar boardMenuBar;

        private JMenuItem messengerMenuItem;
        private JMenu openMenu;
        private JMenu saveMenu;
        private JMenu themesMenu;
        private JMenuItem saveMessengerMenuItem;
        private JMenuItem saveWhiteboardMenuItem;
        private JMenu userMenu;


        JLayeredPane layerpane; 


        /** Creates new form discussionBoard 
         * @param connection */
        public DiscussionBoard() 
        {       

            initComponents();   
            setLocationRelativeTo(null);
            addComponentListener(this);

        }

        private void initComponents() {

            boardMenuBar = new JMenuBar();
            openMenu = new JMenu();
            themesMenu = new JMenu();
            messengerMenuItem = new JMenuItem();
            boardMenuItem = new JMenuItem();
            audioMenuItem = new JMenuItem();
            saveMenu = new JMenu();
            saveMessengerMenuItem = new JMenuItem();
            saveWhiteboardMenuItem = new JMenuItem();
            userMenu = new JMenu();
            clientsMenuItem = new JMenuItem();
            exitMenuItem = new JMenuItem();

            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setLayout(new GridBagLayout());
            setResizable(false);
            setTitle("Discussion Board");

            openMenu.setText("Open");
            saveMenu.setText("Save");
            themesMenu.setText("Themes");
            acryl = new JMenuItem("Acryl");
            hifi = new JMenuItem("HiFi");
            aero = new JMenuItem("Aero");
            aluminium = new JMenuItem("Aluminium");
            bernstein = new JMenuItem("Bernstein");
            fast = new JMenuItem("Fast");
            graphite = new JMenuItem("Graphite");
            luna = new JMenuItem("Luna");
            mcwin = new JMenuItem("MCwin");     
            noire = new JMenuItem("Noire");
            smart = new JMenuItem("Smart");


            hifi.addActionListener(this);
            acryl.addActionListener(this);
            aero.addActionListener(this);
            aluminium.addActionListener(this);
            bernstein.addActionListener(this);
            fast.addActionListener(this);
            graphite.addActionListener(this);
            luna.addActionListener(this);
            mcwin.addActionListener(this);      
            noire.addActionListener(this);
            smart.addActionListener(this);

            messengerMenuItem.setText("Messenger");     
            openMenu.add(messengerMenuItem);        
            openMenu.add(boardMenuItem);
            audioMenuItem.setText("Audio Messenger");
            openMenu.add(audioMenuItem);
            exitMenuItem.setText("Exit");
            exitMenuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    exitMenuItemActionPerformed(evt);
                }
            });
            openMenu.add(exitMenuItem);

            boardMenuBar.add(openMenu);     

            saveMessengerMenuItem.setText("Messenger");

            saveMenu.add(saveMessengerMenuItem);

            saveWhiteboardMenuItem.setText("Whiteboard");       

            saveMenu.add(saveWhiteboardMenuItem);

            boardMenuBar.add(saveMenu);

            userMenu.setText("Users");
            clientsMenuItem.setText("Current Session");

            userMenu.add(clientsMenuItem);      
            themesMenu.add(acryl);
            themesMenu.add(hifi);       
            themesMenu.add(aero);
            themesMenu.add(aluminium);
            themesMenu.add(bernstein);
            themesMenu.add(fast);
            themesMenu.add(graphite);
            themesMenu.add(luna);
            themesMenu.add(mcwin);      
            themesMenu.add(noire);
            themesMenu.add(smart);
            boardMenuBar.add(userMenu);
            boardMenuBar.add(themesMenu);
            saveMessengerMenuItem.setEnabled(false);
            saveWhiteboardMenuItem.setEnabled(false);

            setJMenuBar(boardMenuBar);
            setSize(1024, 740);     
            setVisible(true);
        }

        protected void exitMenuItemActionPerformed(ActionEvent evt) {
            System.exit(0);     
        }
        @Override
        public void componentHidden(ComponentEvent arg0) {

        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent arg0) {

        }

        @Override
        public void componentShown(ComponentEvent arg0) {

        }

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            try 
            {

                if(e.getSource()==hifi)
                {
                        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(getRootPane());
                        UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
                        enableTheme();
                        hifi.setEnabled(false);
                }
                else if(e.getSource()==acryl)
                {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
                    enableTheme();
                    acryl.setEnabled(false);
                }
                else if(e.getSource()==aero)
                {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
                    enableTheme();
                    aero.setEnabled(false);
                }
                else if(e.getSource()==aluminium)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
                    enableTheme();
                    aluminium.setEnabled(false);
                }
                else if(e.getSource()==bernstein)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
                    enableTheme();
                    bernstein.setEnabled(false);
                }
                else if(e.getSource()==fast)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel");
                    enableTheme();
                    fast.setEnabled(false);
                }
                else if(e.getSource()==graphite)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.graphite.GraphiteLookAndFeel");
                    enableTheme();
                    graphite.setEnabled(false);
                }
                else if(e.getSource()==luna)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
                    enableTheme();
                    luna.setEnabled(false);
                }
                else if(e.getSource()==mcwin)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
                    enableTheme();
                    mcwin.setEnabled(false);
                }

                else if(e.getSource()==noire)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
                    enableTheme();
                    noire.setEnabled(false);
                }
                else if(e.getSource()==smart)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
                    enableTheme();
                    smart.setEnabled(false);
                }
                SwingUtilities.updateComponentTreeUI(getRootPane());    
            }  
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        private void enableTheme()
        {
            acryl.setEnabled(true);
            hifi.setEnabled(true);      
            aero.setEnabled(true);
            aluminium.setEnabled(true);
            bernstein.setEnabled(true);
            fast.setEnabled(true);
            graphite.setEnabled(true);
            luna.setEnabled(true);
            mcwin.setEnabled(true);     
            noire.setEnabled(true);
            smart.setEnabled(true);
        }

        public static void main(String []ar)
        {
            try 
            {
                UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");            
            } 
            catch (Exception e) 
            {           
                e.printStackTrace();
            }
            new DiscussionBoard();
        }
    }

What’s the problem here? why its not getting updated?

There is a demo application here which is exactly doing what i want but i cant get a clear idea of it.

Edit: Sorry, forgot to specify the library file, here it is

A: 

Hey swift; trying to run your code, but it throws errors both on startup, and when I try to change themes (so any time it tries to load a theme). Does this happen on your machine or am I missing something?

Either way thanks for the link to the demo app, that is sexy.

goggin13
@goggin13: This does not actually look like an answer, consider adding a comment instead.
Peter Lang
@Peter Lang: I thought that just after seeing yours; I am new here, and I get an "Add Comment" link under my own answers, and under answers to my questions, but not under any arbitrary question. I think I do not have enough reputation to comment yet? Sorry for the misplaced post though. Either way, you asked the same question, so I will remove mine
goggin13
One needs >50 in rep for leaving comments on questions.
aioobe
Library link added to the question, plz add it to your class path. sorry
swift
@goggin13: Well, welcome! I know that's annoying, just try to keep participating to reach the necessary reputation - I think I can help you a little :)
Peter Lang
@Peter Lang: Haha, thanks! I'll try to wheedle out my n00b misplaced responses!
goggin13
A: 

I'm not sure what's causing this, but have you had a look at the updateUI-method?

From the API docs:

Resets the UI property with a value from the current look and feel.

aioobe
What updateUI-method can do here? i cant get you
swift
I'm not sure what it does exactly. I just spotted the connection between the description in the API, your problem, and that the call was missing in your code.
aioobe
A: 

I tried your example and it just worked fine:

alt text


I tried it from commandline with the following files (DiscussionBoard.java in the directory whiteboard):

JTattooDemo.jar
whiteboard/DiscussionBoard.java

javac whiteboard/DiscussionBoard.java
java -cp ".;JTattooDemo.jar" whiteboard/DiscussionBoard

Note that the JTattoo is only free for non commercial use.

Peter Lang
it wont show you any error, but the problem is it wont update the theme properly, try selecting the theme randomly, atleast 10 themes, it will not look how it is supposed to be. see the screenshots in the following linkhttp://www.jtattoo.net/ScreenShots.html
swift
Ya peter i am not using the theme for commercial app
swift
Peter Lang
+1  A: 

Move the SwingUtilities.updateComponentTreeUI(getRootPane()); call to after the calls that set up the LnF. It calls updateUI on all components in the tree below the passed component. Right now, you are updating the UIs then changing them.

Devon_C_Miller