Solved

Installing a different PLAF to a Swing component

Posted on 1998-08-10
12
204 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
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 58
Way to decrease size of apk file 9 67
servlet example 17 31
Android development question 2 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

770 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