Solved

Installing a different PLAF to a Swing component

Posted on 1998-08-10
12
209 Views
Last Modified: 2013-12-29
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.
0
Comment
Question by:mwalker
[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
  • 8
  • 3
12 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 300 total points
ID: 1232283
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
 

Expert Comment

by:awilkins
ID: 1232284
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232285
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
Technology Partners: 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 3

Expert Comment

by:mjenkins
ID: 1232286
For individual components use the JComponent setUI() method.

example:

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


0
 

Author Comment

by:mwalker
ID: 1232287
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232288
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
 

Author Comment

by:mwalker
ID: 1232289
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232290
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232291
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232292
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
 

Author Comment

by:mwalker
ID: 1232293
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1232294
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
difference between sorce folder and folder in eclipise 3 42
printf performancy 11 84
passing enum to a method 4 35
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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