Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Installing a different PLAF to a Swing component

Posted on 1998-08-10
12
Medium Priority
?
222 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 900 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 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