Solved

Installing a different PLAF to a Swing component

Posted on 1998-08-10
12
197 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:mjenkins
Comment Utility
For individual components use the JComponent setUI() method.

example:

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


0
 

Author Comment

by:mwalker
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mwalker
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now