Installing a different PLAF to a Swing component

I'm writing an applet that installs the system's PLAF using

   UIManager.setLookAndFeel
     (UIManager.getSystemLookAndFeelClassName());

I'd like to have another component install a different L&F (e.g. the "Metal" PLAF).  Can someone give me an example explaining how this is done.  I have the latest version of Swing installed.

Thanks.
mwalkerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mjenkinsConnect With a Mentor Commented:
Here's an easy way to set L&F for a whole app:

public class MyClass
{
    static
    {
        try
        {
            UIManager.setLookAndFeel( "my.lf.MyLookAndFeel" );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }

    public static void main( String args[] )

.

}
0
 
awilkinsCommented:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFee()) should set the default L&F to the JL&F, "metal."  For other L&Fs, you can use UIManager.setLookAndFeel(otherLookAndFeel), where otherLookAndFeel is some class that extends com.sun.java.swing.LookAndFeel.  As the documentation for the jdk1.2beta4 does not list any subclasses of the LookAndFeel class, this probably means that you will have to create a new class yourself if you want a L&F totally separate from both metal and the system L&F.

    -- Alexander Wilkins
0
 
mjenkinsCommented:
More specifically for Metal:

UIManager.setLookAndFeel( "com.sun.java.swing.plaf.metal.MetalLookAndFeel" );

Sorry, I cut and paste the original from some test code I have sitting around. The I realized that it may not be apparent where the Metal L&F classes are...

Hope this clarifies the above.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
mjenkinsCommented:
For individual components use the JComponent setUI() method.

example:

JTree myTree = new JTree();
myTree.setUI( new MetalTreeUI() );


0
 
mwalkerAuthor Commented:
I had a problem doing mjenkins' solution using a subclass of a JInternalFrame.  I get an exception when I try to add it to the the JDesktop.  Here's what I did (BTW, I meant I wanted to use the "Cross Platform" UI):

import com.sun.java.swing.plaf.multi.MultiInternalFrameUI;



public class MyInternalFrame extends JInternalFrame
{
  public MyInternalFrame() {
    super();
    setUI(new MultiInternalFrameUI());
    .
    .
    .
  }
}

Then this object gets added to the JDesktop pane.  Any ideas?

Thanks.
0
 
mjenkinsCommented:
Here is an entire example of setting the L&F for an internal frame. You still need to check the constructor for each L&F type to make sure you pass the required parameters, in this case the JInternalFrame itself:

import com.sun.java.swing.*;
import com.sun.java.swing.plaf.metal.*;

public class Foo
        extends JInternalFrame
{
        public static void main( String args[] )
        {
                JFrame f = new JFrame( "Foo" );
                f.getContentPane().add( new Foo() );
                f.setSize( 400,400 );
                f.setVisible( true );
        }

        public Foo( )
        {
                setUI( new MetalInternalFrameUI( this ) );
        }
}

I hope this clarifies the solution for you. If not, let me know.
0
 
mwalkerAuthor Commented:
I hate to be the bearer of bad news, I got a java.lang.NullPointerException when I did this.  Here's the output of my debugger:

java.lang.NullPointerException at com.sun.java.swing.plaf.metal.MetalLookAndFeel.getPrimaryControlHighlight(MetalLookAndFeel.java:638)
   at com.sun.java.swing.plaf.metal.MetalInternalFrameTitlePane.<init>(MetalInternalFrameTitlePane.java:67)
   at com.sun.java.swing.plaf.metal.MetalInternalFrameUI.createNorthPane(MetalInternalFrameUI.java:114)
   at com.sun.java.swing.plaf.basic.BasicInternalFrameUI.installUI(BasicInternalFrameUI.java:81)
   at com.sun.java.swing.plaf.metal.MetalInternalFrameUI.installUI(MetalInternalFrameUI.java:75)
   at com.sun.java.swing.JComponent.setUI(JComponent.java:252)
   at com.sun.java.swing.JInternalFrame.setUI(JInternalFrame.java:281)
   at com.systemarts.caa.Applet.CAARptViewer.<init>(CAARptViewer.java:58)
   at com.systemarts.caa.Applet.IndInfoRpt.<init>(IndInfoRpt.java:45)
   at com.systemarts.caa.Applet.CAAProgressPanel.createReport(CAAProgressPanel.java:319)
   at com.systemarts.caa.Applet.CAAProgressPanel.access$6(CAAProgressPanel.java:313)
   at com.systemarts.caa.Applet.CAAProgressPanel$3.propertyChange(CAAProgressPanel.java:290)
   at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:103)
   at com.systemarts.caa.report.Report.setHint(Report.java:80)
   at com.systemarts.caa.report.Report.fillReport(Report.java:241)
   at com.systemarts.caa.report.Report.run(Report.java:267)
   at com.systemarts.caa.report.Billing.run(Billing.java:271)
   at java.lang.Thread.run(Thread.java:474)
-------
My class works fine before I added the setUI() method.
0
 
mjenkinsCommented:
Here is the same example using a JDesktopPane:

import com.sun.java.swing.*;
import com.sun.java.swing.plaf.metal.*;
 
public class Foo
        extends JInternalFrame
{
        public static void main( String args[] )
        {
                JFrame f = new JFrame( "Foo" );
                JDesktopPane desk = new JDesktopPane();
                Foo foo = new Foo();
                f.getContentPane().add( desk );
                desk.add( foo );
                desk.getDesktopManager().activateFrame( foo );
                f.setSize( 400,400 );
                f.setVisible( true );
        }
 
        public Foo( )
        {
                setSize( 200,200 );
                setUI( new MetalInternalFrameUI( this ) );
        }
}

You'll notice that there is no big differences in using the L&F no matter where you use it.
0
 
mjenkinsCommented:
Oops, we cross posted...Which version of the JDK and Swing are you using? Could you send me your source off-line? As you can see by my example, the basic code works fine. I would need to see your code to determine if there is a usage problem. If you want me to look at it, let me know and I will post my email address.
0
 
mjenkinsCommented:
Here it is in applet form:

AFoo.html
<head><title>Applet</title></head>
<body> <applet code=AFoo width=500 height=300>
</applet>
</body>
</html>

AFoo.java:
import java.applet.*;
import com.sun.java.swing.*;
import com.sun.java.swing.plaf.metal.*;

public class AFoo
        extends Applet
{
        public void init( )
        {
                setLayout( new java.awt.BorderLayout() );
                JDesktopPane desk = new JDesktopPane();
                Foo foo = new Foo();
                add( desk );
                desk.add( foo );
                desk.getDesktopManager().activateFrame( foo );
                setVisible( true );
        }
}


Foo.java:
import com.sun.java.swing.*;
import com.sun.java.swing.plaf.metal.*;

public class Foo
        extends JInternalFrame
{
        public static void main( String args[] )
        {
                JFrame f = new JFrame( "Foo" );
                JDesktopPane desk = new JDesktopPane();
                Foo foo = new Foo();
                f.getContentPane().add( desk );
                desk.add( foo );
                desk.getDesktopManager().activateFrame( foo );
                f.setSize( 400,400 );
                f.setVisible( true );
        }

        public Foo( )
        {
                setSize( 200,200 );
                setUI( new MetalInternalFrameUI( this ) );
        }
}


This should demonstrate the calls as used in an applet since that seems to be your intent. The above was compiled using JDK 1.1.6 (Solaris & Linux & NT) and run using the Appletviewer on all three platforms. I am using version 1.0.2 of Swing. Let me know if you need further assistance.
0
 
mwalkerAuthor Commented:
I just installed the version 1.1 of Swing.  I tried the call to setUI after calling setSize() to no avail.  Basically, I'm doing the same thing as your example.

0
 
mjenkinsCommented:
It's the "Basically" part that concerns me. Could you please try to run my example AS-IS on your system? I've tried it with swing 1.1 Beta as well and it runs fine. Without seeing your code, there is no way to tell if you are doing something that could cause a problem. If you have a problem running my exact example (just cut and paste it into three files), then there is a problem with your system.

I'm trying very hard to help you, but I need your cooperation in order to fix this thing. After all, your original question was "I'd like to have another component install a different L&F (e.g. the "Metal" PLAF).  Can someone give me an example explaining how this is done." I've done that. I am trying to go above and beyond by helping you isolate your specific example -- but you need to help me by trying what I send you.
0
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.

All Courses

From novice to tech pro — start learning today.