Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

setting the look and feel of a java program

Posted on 2003-03-12
21
Medium Priority
?
292 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:pronane
  • 11
  • 7
  • 3
21 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8120494
0
 
LVL 1

Author Comment

by:pronane
ID: 8120675
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
 
LVL 35

Expert Comment

by:girionis
ID: 8120760
 What is the error?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:pronane
ID: 8120792
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
 
LVL 1

Author Comment

by:pronane
ID: 8121676
Just checked your profile girionis, good to see you like heavy metal ;)  Nothing like a bit of priest or sabbath!
0
 
LVL 1

Expert Comment

by:sct75
ID: 8122194
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
 
LVL 35

Expert Comment

by:girionis
ID: 8122639
 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
 
LVL 1

Author Comment

by:pronane
ID: 8127716
ya I got it working already!  sorry should have posted yesterday, figured just after my second post!!
0
 
LVL 1

Author Comment

by:pronane
ID: 8128882
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
 
LVL 1

Author Comment

by:pronane
ID: 8128891
sorry taht should be when i want to go back to the metal look after clicking either the motif or windows look!
0
 
LVL 35

Expert Comment

by:girionis
ID: 8128956
 What's the error?
0
 
LVL 1

Author Comment

by:pronane
ID: 8129104
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
 
LVL 35

Expert Comment

by:girionis
ID: 8129232
 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
 
LVL 1

Author Comment

by:pronane
ID: 8129365
no good point ;)

error setting native look and feeljava.lang.ClassNotFoundException: com.sun.java
.swing.plaf.metal.MetalLookAndFeel
0
 
LVL 35

Expert Comment

by:girionis
ID: 8130760
 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
 
LVL 1

Expert Comment

by:sct75
ID: 8132566
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
 
LVL 1

Author Comment

by:pronane
ID: 8136847
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
 
LVL 1

Author Comment

by:pronane
ID: 8136860
the other ones work fine like motif and windows:

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

com.sun.java.swing.plaf.motif.MotifLookAndFeel
0
 
LVL 35

Expert Comment

by:girionis
ID: 8137108
Try sct75 solution. The class you want is indeed under the javax package.
0
 
LVL 1

Accepted Solution

by:
sct75 earned 60 total points
ID: 8160510
pronane, did you give it a try to use "javax.swing.plaf.metal.MetalLookAndFeel" for your problem?
0
 
LVL 1

Author Comment

by:pronane
ID: 8181234
ya sorry it worked, though i dont use that for the others, i will award the points now, thanks again.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question