?
Solved

setting the look and feel of a java program

Posted on 2003-03-12
21
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 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