setting the look and feel of a java program

Sorry about the little amount of points but its a real easy question.  I am trying to set the look and feel from when the program is running how to i do this i.e
I can get it to work from the start like the following:

System.out.println("Please enter the look and feel you want: (Windows, Motif, Metal) ");
          String lookAndFeel = Console.readString();
         
          Menu window = new Menu(lookAndFeel);
        window.setTitle("MenuLookDemo");
        window.setSize(450, 260);
        window.setVisible(true);

but if i try to set the look and feel when a butotn is clicked within the actionperformed method it wont reset the look and fell with the following:

try
          {
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          }catch(Exception e)
          {
               System.out.println("error setting native look and feel");
          }

I have seen this done from when the program is running how can I do that so that it will just recreate the look and feel without actually creatng a new window, so that stored data will still be present.

Thanks a lot,

Paul
LVL 1
pronaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
0
pronaneAuthor Commented:
Damn this was the site I learned it from and never noticed that before!  It gives me an error though.

UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();



frame should be the class that your extending right, i.e if you want to update the whole frame.  So I frame with this but it gives me an error, this obviouslly cant be done so what do I do instead?
0
girionisCommented:
 What is the error?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

pronaneAuthor Commented:
Sorry I meant to post it earlier:

Test.java:492: updateComponentTreeUI(java.awt.Component) in javax.swing.SwingUti
lities cannot be applied to (Test.MenuListener)
                                        SwingUtilities.updateComponentTreeUI(thi
s);
                                                      ^
Test.java:493: cannot resolve symbol
symbol  : method pack  ()
location: class Test.MenuListener
                                        this.pack();
                                        ^
2 errors
0
pronaneAuthor Commented:
Just checked your profile girionis, good to see you like heavy metal ;)  Nothing like a bit of priest or sabbath!
0
sct75Commented:
Your error means that you pass in the wrong object type.
SwingUtilities.updateComponentTreeUI() expects a component descendents and there should be at least a frame descendent to be able to call "pack()" function.

Using "this" to represent your current class instance.

If it is not an descendent of Frame and/or Component, you should not use it directly to call both methods above.
0
girionisCommented:
 Glad to know you like metal too pronane :-)

  As sct75 suggested your "this" variable represents your class. If you class does not extend/implenent a Component subclass then you cannot pass it as argument on the updateComponentTreeUI(). Instead you should pass as argument a class of type "component" (or any of its subclasses).
0
pronaneAuthor Commented:
ya I got it working already!  sorry should have posted yesterday, figured just after my second post!!
0
pronaneAuthor Commented:
one strange thing is it will set the look and feel for motif and windows, but if i want to go back to motif then (when i click the mouse button) it throws an exception and prints an error creating native look and feel to the console, any ideas?
0
pronaneAuthor Commented:
sorry taht should be when i want to go back to the metal look after clicking either the motif or windows look!
0
girionisCommented:
 What's the error?
0
pronaneAuthor Commented:
i have it catching an exception so that if it doesnt set the look and feel it prints to the console error setting look and feel, there is no error, ist just doesnt set it and prints this that to the screen here is how i have it implemeneted though:

else if(source == met)
               {
                         //Test.setNativeLookAndFeel();
                         System.out.println("hello");
                   
                    try
                    {
                         UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MetalLookAndFeel");
                         SwingUtilities.updateComponentTreeUI(myParentFrame);
                         //myParentFrame.pack();

                    }catch(Exception e)
                    {
                         System.out.println("error setting native look and feel");
                    }
       
               }

0
girionisCommented:
 Well if we have no error then we cannot find fidn a solution, can we? Change that:

System.out.println("error setting native look and feel");

  to

System.out.println("error setting native look and feel " + e);

  and let us know of the error.
0
pronaneAuthor Commented:
no good point ;)

error setting native look and feeljava.lang.ClassNotFoundException: com.sun.java
.swing.plaf.metal.MetalLookAndFeel
0
girionisCommented:
 It's missing the MetalLookAndFeel class. Are you sure you have it in your classpath? Have you jarred up your application? Which version of Java you are using?
0
sct75Commented:
From pronane's code,
"com.sun.java.swing.plaf.motif.MetalLookAndFeel" is not right. The correct full class name should be "javax.swing.plaf.metal.MetalLookAndFeel".

If you insist using the class from com.sun.... pacakge, you need to make sure it is in your classpath.

0
pronaneAuthor Commented:
well it must be in my classpath if i can set it to that on startup? because on startup it doesnt throw an exception eventhough I know that metal is the default look
0
pronaneAuthor Commented:
the other ones work fine like motif and windows:

com.sun.java.swing.plaf.windows.WindowsLookAndFeel

com.sun.java.swing.plaf.motif.MotifLookAndFeel
0
girionisCommented:
Try sct75 solution. The class you want is indeed under the javax package.
0
sct75Commented:
pronane, did you give it a try to use "javax.swing.plaf.metal.MetalLookAndFeel" for your problem?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pronaneAuthor Commented:
ya sorry it worked, though i dont use that for the others, i will award the points now, thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.