How do I set the look and feel of my dialog to match what I see in preview

When I preview my dialog I can select the Windows look and feel and get a native Windows 7 look and feel.  How do I do this for my application?  I tried using the following, but the result was what in a C# app would be setting my textboxes to have BorderStyle = BorderStyle.FixedSingle and my buttons to FlatStyle = FlatStyle.Flat; not the windows 7 style I was expecting.


        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

0
3 comments

Hello Dan,

We don't do anything fancy in the preview at all. We simply give you the
list of look & feels from UIManager.getInstalledLookAndFeels() and allow
you to choose one of them. There is nothing Windows specific there.

When I preview my dialog I can select the Windows look and feel and
get a native Windows 7 look and feel.  How do I do this for my
application?  I tried using the following, but the result was what in
a C# app would be setting my textboxes to have BorderStyle =
BorderStyle.FixedSingle and my buttons to FlatStyle = FlatStyle.Flat;
not the windows 7 style I was expecting.

try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookA
ndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();  //To change body of catch statement
use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace();  //To change body of catch statement
use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace();  //To change body of catch statement
use File | Settings | File Templates.
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();  //To change body of catch statement
use File | Settings | File Templates.
}


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I got the 5 LookAndFeel's that show up when I preview my form from UIManager.getInstalledLookAndFeels(), but while passing the class name for Metal works as expected (although since this is the default I don't know if it means anything), passing any of the other four class names results in my form getting the mystery theme mentioned in my original post.


   public static void main(String[] args) {
        JFrame frameMain = new JFrame("My Forml");
        frameMain.setContentPane(new frmDataTranslationTool().frmMyForm);
        frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameMain.pack();

        frameMain.setVisible(true);

        // Set System L&F
        try {
                //This look and feel works
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

                  //None of these do.
//                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");



        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

0

This is nothing IntelliJ IDEA specific, but plain Swing.
If you change the look and feel AFTER you have shown a window (Frame, Dialog, ....) you have to update those windows with the new look and feel.
Either set the look and feel before you create any Window, or update the existing windows. A generic way of doing this and also getting all dialogs the system may have shown is:

            for (Window window : Window.getWindows())

            {
                SwingUtilities.updateComponentTreeUI(window);
            }

0

Please sign in to leave a comment.